[Plugin] balance: Enhanced the model by adding serialization members

This commit is contained in:
Ferdinand Thiessen 2020-11-01 18:38:53 +01:00
parent 36c4027c5d
commit d439cd93c8
2 changed files with 23 additions and 4 deletions

View File

@ -1,2 +1,3 @@
# Migrate code style to Black # Migrate code style to Black
41e60425a96eeddb45f3a9f9bfc5ef491ce8e071 41e60425a96eeddb45f3a9f9bfc5ef491ce8e071
36c4027c5dd1c631bf6ca84890b6ad9c916e1888

View File

@ -8,15 +8,33 @@ from flaschengeist.models import ModelSerializeMixin, UtcDateTime
class Transaction(db.Model, ModelSerializeMixin): class Transaction(db.Model, ModelSerializeMixin):
__tablename__ = "balance_transaction" __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)) 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) 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")) _receiver_id = db.Column("receiver_id", db.Integer, db.ForeignKey("user.id"))
_sender_id = db.Column("sender_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) _author_id = db.Column("author_id", db.Integer, db.ForeignKey("user.id"), nullable=False)
sender: User = db.relationship("User", foreign_keys=[_sender_id]) # Not exported relationships just in backend only
receiver: User = db.relationship("User", foreign_keys=[_receiver_id]) sender_: User = db.relationship("User", foreign_keys=[_sender_id])
author: User = db.relationship("User", foreign_keys=[_author_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