From 7ccae7e888731d6010e505360eca4e6bd4ed02ee Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 01:08:31 +0100 Subject: [PATCH] [Plugin] balance: Changed model * serialize Transaction with reversal and original Transaction IDs --- flaschengeist/plugins/auth/__init__.py | 4 +--- flaschengeist/plugins/balance/models.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/flaschengeist/plugins/auth/__init__.py b/flaschengeist/plugins/auth/__init__.py index 853902c..a3fc44d 100644 --- a/flaschengeist/plugins/auth/__init__.py +++ b/flaschengeist/plugins/auth/__init__.py @@ -139,9 +139,7 @@ def set_lifetime(token, current_session, **kwargs): raise Forbidden try: lifetime = request.get_json()["value"] - logger.debug( - "set lifetime {{ {} }} to access token {{ {} }}".format(lifetime, token) - ) + logger.debug(f"set lifetime >{lifetime}< to access token >{token}<") sessionController.set_lifetime(session, lifetime) return jsonify(sessionController.get_session(token, current_session._user)) except (KeyError, TypeError): diff --git a/flaschengeist/plugins/balance/models.py b/flaschengeist/plugins/balance/models.py index 811124a..e9f1d02 100644 --- a/flaschengeist/plugins/balance/models.py +++ b/flaschengeist/plugins/balance/models.py @@ -13,25 +13,24 @@ class Transaction(db.Model, ModelSerializeMixin): _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) - _reversal_id = db.Column("reversal_id", db.Integer, db.ForeignKey("balance_transaction.id")) # Public and exported member id: int = db.Column("id", db.Integer, primary_key=True) time: datetime = db.Column(UtcDateTime, nullable=False, default=UtcDateTime.current_utc) amount: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) - reversal: Optional["Transaction"] = db.relationship( - "Transaction", uselist=False, post_update=True, foreign_keys=[_reversal_id] - ) + reversal_id: int = db.Column(db.Integer, db.ForeignKey("balance_transaction.id")) # Dummy properties used for JSON serialization (userid instead of full user) sender_id: Optional[str] = "" receiver_id: Optional[str] = "" author_id: Optional[str] = "" + original_id: Optional[int] = -1 # 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]) + original_ = db.relationship("Transaction", uselist=False, backref=db.backref("reversal_", remote_side=[id])) @property def sender_id(self): @@ -45,11 +44,6 @@ class Transaction(db.Model, ModelSerializeMixin): def author_id(self): return self.author_.userid - # Override to prevent circular dependencies (endless JSON) - def serialize(self): - d = super().serialize() - if d["reversal"]: - d["reversal"].reversal = None - else: - d["reversal"] = None - return d + @property + def original_id(self): + return self.original_.id if self.original_ else None