From 45dc6edacbc9cb771fd038cdc719fd953c1a6771 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Fri, 9 Sep 2011 19:57:28 +0200 Subject: Move some logic to the models and add comments. --- budget/models.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'budget/models.py') diff --git a/budget/models.py b/budget/models.py index 08e46ae..8feaccb 100644 --- a/budget/models.py +++ b/budget/models.py @@ -36,6 +36,33 @@ class Project(db.Model): return balances + def get_bills(self): + """Return the list of bills related to this project""" + return Bill.query.join(Person, Project)\ + .filter(Bill.payer_id == Person.id)\ + .filter(Person.project_id == Project.id)\ + .filter(Project.id == self.id)\ + .order_by(Bill.date.desc()) + + def remove_member(self, member_id): + """Remove a member from the project. + + If the member is not bound to a bill, then he is deleted, otherwise + he is only deactivated. + + This method returns the status DELETED or DEACTIVATED regarding the + changes made. + """ + person = Person.query.get_or_404(member_id) + if person.project == self: + if not person.has_bills(): + db.session.delete(person) + db.session.commit() + else: + person.activated = False + db.session.commit() + return person + def __repr__(self): return "" % self.name -- cgit v1.1