mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-05 20:51:49 +02:00
refactoring models _to_serialize property
This commit is contained in:
parent
620596e32c
commit
50eabdeb97
2 changed files with 33 additions and 15 deletions
|
@ -13,11 +13,6 @@ db = SQLAlchemy()
|
|||
|
||||
class Project(db.Model):
|
||||
|
||||
_to_serialize = (
|
||||
"id", "name", "contact_email", "members", "active_members",
|
||||
"balance"
|
||||
)
|
||||
|
||||
id = db.Column(db.String(64), primary_key=True)
|
||||
|
||||
name = db.Column(db.UnicodeText)
|
||||
|
@ -25,6 +20,17 @@ class Project(db.Model):
|
|||
contact_email = db.Column(db.String(128))
|
||||
members = db.relationship("Person", backref="project")
|
||||
|
||||
@property
|
||||
def _to_serialize(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"contact_email": self.contact_email,
|
||||
"members": self.members,
|
||||
"active_members": self.active_members,
|
||||
"balance": self.balance,
|
||||
}
|
||||
|
||||
@property
|
||||
def active_members(self):
|
||||
return [m for m in self.members if m.activated]
|
||||
|
@ -276,8 +282,6 @@ class Person(db.Model):
|
|||
|
||||
query_class = PersonQuery
|
||||
|
||||
_to_serialize = ("id", "name", "weight", "activated")
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
project_id = db.Column(db.String(64), db.ForeignKey("project.id"))
|
||||
bills = db.relationship("Bill", backref="payer")
|
||||
|
@ -286,6 +290,15 @@ class Person(db.Model):
|
|||
weight = db.Column(db.Float, default=1)
|
||||
activated = db.Column(db.Boolean, default=True)
|
||||
|
||||
@property
|
||||
def _to_serialize(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"weight": self.weight,
|
||||
"activated": self.activated,
|
||||
}
|
||||
|
||||
def has_bills(self):
|
||||
"""return if the user do have bills or not"""
|
||||
bills_as_ower_number = db.session.query(billowers)\
|
||||
|
@ -330,9 +343,6 @@ class Bill(db.Model):
|
|||
|
||||
query_class = BillQuery
|
||||
|
||||
_to_serialize = ("id", "payer_id", "owers", "amount", "date",
|
||||
"creation_date", "what")
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
|
||||
payer_id = db.Column(db.Integer, db.ForeignKey("person.id"))
|
||||
|
@ -345,6 +355,18 @@ class Bill(db.Model):
|
|||
|
||||
archive = db.Column(db.Integer, db.ForeignKey("archive.id"))
|
||||
|
||||
@property
|
||||
def _to_serialize(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"payer_id": self.payer_id,
|
||||
"owers": self.owers,
|
||||
"amount": self.amount,
|
||||
"date": self.date,
|
||||
"creation_date": self.creation_date,
|
||||
"what": self.what,
|
||||
}
|
||||
|
||||
def pay_each(self):
|
||||
"""Compute what each share has to pay"""
|
||||
if self.owers:
|
||||
|
|
|
@ -192,11 +192,7 @@ class IhmJSONEncoder(JSONEncoder):
|
|||
Taken from the deprecated flask-rest package."""
|
||||
def default(self, o):
|
||||
if hasattr(o, "_to_serialize"):
|
||||
# build up the object
|
||||
data = {}
|
||||
for attr in o._to_serialize:
|
||||
data[attr] = getattr(o, attr)
|
||||
return data
|
||||
return o._to_serialize
|
||||
elif hasattr(o, "isoformat"):
|
||||
return o.isoformat()
|
||||
else:
|
||||
|
|
Loading…
Reference in a new issue