[Plugin] balance: Changed model

* serialize Transaction with reversal and original Transaction IDs
This commit is contained in:
Ferdinand Thiessen 2021-01-29 01:08:31 +01:00
parent 7c0d609b80
commit 7ccae7e888
2 changed files with 7 additions and 15 deletions

View File

@ -139,9 +139,7 @@ def set_lifetime(token, current_session, **kwargs):
raise Forbidden raise Forbidden
try: try:
lifetime = request.get_json()["value"] lifetime = request.get_json()["value"]
logger.debug( logger.debug(f"set lifetime >{lifetime}< to access token >{token}<")
"set lifetime {{ {} }} to access token {{ {} }}".format(lifetime, token)
)
sessionController.set_lifetime(session, lifetime) sessionController.set_lifetime(session, lifetime)
return jsonify(sessionController.get_session(token, current_session._user)) return jsonify(sessionController.get_session(token, current_session._user))
except (KeyError, TypeError): except (KeyError, TypeError):

View File

@ -13,25 +13,24 @@ class Transaction(db.Model, ModelSerializeMixin):
_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)
_reversal_id = db.Column("reversal_id", db.Integer, db.ForeignKey("balance_transaction.id"))
# Public and exported member # Public and exported member
id: int = db.Column("id", db.Integer, primary_key=True) id: int = db.Column("id", db.Integer, primary_key=True)
time: datetime = db.Column(UtcDateTime, nullable=False, default=UtcDateTime.current_utc) 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) amount: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False)
reversal: Optional["Transaction"] = db.relationship( reversal_id: int = db.Column(db.Integer, db.ForeignKey("balance_transaction.id"))
"Transaction", uselist=False, post_update=True, foreign_keys=[_reversal_id]
)
# Dummy properties used for JSON serialization (userid instead of full user) # Dummy properties used for JSON serialization (userid instead of full user)
sender_id: Optional[str] = "" sender_id: Optional[str] = ""
receiver_id: Optional[str] = "" receiver_id: Optional[str] = ""
author_id: Optional[str] = "" author_id: Optional[str] = ""
original_id: Optional[int] = -1
# Not exported relationships just in backend only # Not exported relationships just in backend only
sender_: User = db.relationship("User", foreign_keys=[_sender_id]) sender_: User = db.relationship("User", foreign_keys=[_sender_id])
receiver_: User = db.relationship("User", foreign_keys=[_receiver_id]) receiver_: User = db.relationship("User", foreign_keys=[_receiver_id])
author_: User = db.relationship("User", foreign_keys=[_author_id]) author_: User = db.relationship("User", foreign_keys=[_author_id])
original_ = db.relationship("Transaction", uselist=False, backref=db.backref("reversal_", remote_side=[id]))
@property @property
def sender_id(self): def sender_id(self):
@ -45,11 +44,6 @@ class Transaction(db.Model, ModelSerializeMixin):
def author_id(self): def author_id(self):
return self.author_.userid return self.author_.userid
# Override to prevent circular dependencies (endless JSON) @property
def serialize(self): def original_id(self):
d = super().serialize() return self.original_.id if self.original_ else None
if d["reversal"]:
d["reversal"].reversal = None
else:
d["reversal"] = None
return d