From d439cd93c8d9fc9d6d7622219808f341e8a5a332 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 1 Nov 2020 18:38:53 +0100 Subject: [PATCH] [Plugin] balance: Enhanced the model by adding serialization members --- .git-blame-ignore-revs | 1 + flaschengeist/plugins/balance/models.py | 26 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 2ee1cc3..36f331f 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,2 +1,3 @@ # Migrate code style to Black 41e60425a96eeddb45f3a9f9bfc5ef491ce8e071 +36c4027c5dd1c631bf6ca84890b6ad9c916e1888 diff --git a/flaschengeist/plugins/balance/models.py b/flaschengeist/plugins/balance/models.py index 4a967d5..5219751 100644 --- a/flaschengeist/plugins/balance/models.py +++ b/flaschengeist/plugins/balance/models.py @@ -8,15 +8,33 @@ from flaschengeist.models import ModelSerializeMixin, UtcDateTime class Transaction(db.Model, ModelSerializeMixin): __tablename__ = "balance_transaction" + id: int = db.Column("id", db.Integer, primary_key=True) time: datetime = db.Column(UtcDateTime, nullable=False, default=datetime.now(tz=timezone.utc)) amount: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) + # Dummy properties used for JSON serialization (userid instead of full user) + sender_id: str = "" + receiver_id: str = "" + author_id: str = "" + + # Protected foreign key properties _receiver_id = db.Column("receiver_id", db.Integer, db.ForeignKey("user.id")) _sender_id = db.Column("sender_id", db.Integer, db.ForeignKey("user.id")) _author_id = db.Column("author_id", db.Integer, db.ForeignKey("user.id"), nullable=False) - sender: User = db.relationship("User", foreign_keys=[_sender_id]) - receiver: User = db.relationship("User", foreign_keys=[_receiver_id]) - author: User = db.relationship("User", foreign_keys=[_author_id]) + # Not exported relationships just in backend only + sender_: User = db.relationship("User", foreign_keys=[_sender_id]) + receiver_: User = db.relationship("User", foreign_keys=[_receiver_id]) + author_: User = db.relationship("User", foreign_keys=[_author_id]) - _id = db.Column("id", db.Integer, primary_key=True) + @property + def sender_id(self): + return self.sender_.userid + + @property + def receiver_id(self): + return self.receiver_.userid + + @property + def author_id(self): + return self.author_.userid