[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
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):

View File

@ -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