2011-07-31 23:55:18 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
from datetime import datetime
|
2011-09-13 22:58:53 +02:00
|
|
|
from flaskext.sqlalchemy import SQLAlchemy, BaseQuery
|
|
|
|
from flask import g
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-09-13 18:15:07 +02:00
|
|
|
from sqlalchemy import orm
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
db = SQLAlchemy()
|
|
|
|
|
|
|
|
# define models
|
|
|
|
class Project(db.Model):
|
2011-09-13 11:27:36 +02:00
|
|
|
|
|
|
|
_to_serialize = ("id", "name", "password", "contact_email",
|
2011-10-18 23:26:13 +02:00
|
|
|
"members", "active_members", "balance")
|
2011-09-13 11:27:36 +02:00
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
id = db.Column(db.String, primary_key=True)
|
|
|
|
|
|
|
|
name = db.Column(db.UnicodeText)
|
|
|
|
password = db.Column(db.String)
|
|
|
|
contact_email = db.Column(db.String)
|
|
|
|
members = db.relationship("Person", backref="project")
|
|
|
|
|
2011-07-31 23:55:18 +02:00
|
|
|
@property
|
|
|
|
def active_members(self):
|
|
|
|
return [m for m in self.members if m.activated]
|
|
|
|
|
2011-10-18 23:26:13 +02:00
|
|
|
@property
|
|
|
|
def balance(self):
|
2011-07-31 23:55:18 +02:00
|
|
|
|
|
|
|
balances, should_pay, should_receive = defaultdict(int), defaultdict(int), defaultdict(int)
|
|
|
|
|
|
|
|
# for each person
|
|
|
|
for person in self.members:
|
|
|
|
# get the list of bills he has to pay
|
|
|
|
bills = Bill.query.filter(Bill.owers.contains(person))
|
|
|
|
for bill in bills.all():
|
|
|
|
if person != bill.payer:
|
|
|
|
should_pay[person] += bill.pay_each()
|
|
|
|
should_receive[bill.payer] += bill.pay_each()
|
|
|
|
|
|
|
|
for person in self.members:
|
2011-10-18 23:26:13 +02:00
|
|
|
balances[person.id] = round(should_receive[person] - should_pay[person], 2)
|
2011-07-31 23:55:18 +02:00
|
|
|
|
|
|
|
return balances
|
|
|
|
|
2011-12-03 22:25:19 +01:00
|
|
|
def has_bills(self):
|
|
|
|
"""return if the project do have bills or not"""
|
|
|
|
return self.get_bills().count() != 0
|
|
|
|
|
2011-09-09 19:57:28 +02:00
|
|
|
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.
|
|
|
|
"""
|
2011-10-08 13:22:18 +02:00
|
|
|
person = Person.query.get(member_id, self)
|
|
|
|
if not person.has_bills():
|
|
|
|
db.session.delete(person)
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
person.activated = False
|
|
|
|
db.session.commit()
|
2011-09-09 19:57:28 +02:00
|
|
|
return person
|
|
|
|
|
2011-11-02 12:16:01 +01:00
|
|
|
def remove_project(self):
|
|
|
|
db.session.delete(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Project %s>" % self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Person(db.Model):
|
2011-09-13 11:27:36 +02:00
|
|
|
|
2011-09-13 22:58:53 +02:00
|
|
|
class PersonQuery(BaseQuery):
|
|
|
|
def get_by_name(self, name, project):
|
|
|
|
return Person.query.filter(Person.name == name)\
|
|
|
|
.filter(Project.id == project.id).one()
|
|
|
|
|
|
|
|
def get(self, id, project=None):
|
|
|
|
if not project:
|
|
|
|
project = g.project
|
|
|
|
return Person.query.filter(Person.id == id)\
|
|
|
|
.filter(Project.id == project.id).one()
|
|
|
|
|
|
|
|
|
|
|
|
query_class = PersonQuery
|
|
|
|
|
2011-09-13 11:27:36 +02:00
|
|
|
_to_serialize = ("id", "name", "activated")
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2011-10-13 21:27:56 +02:00
|
|
|
project_id = db.Column(db.String, db.ForeignKey("project.id"))
|
2011-07-23 18:45:40 +02:00
|
|
|
bills = db.relationship("Bill", backref="payer")
|
|
|
|
|
|
|
|
name = db.Column(db.UnicodeText)
|
2011-07-31 23:55:18 +02:00
|
|
|
activated = db.Column(db.Boolean, default=True)
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-10 17:49:35 +02:00
|
|
|
def has_bills(self):
|
2011-10-18 17:45:24 +02:00
|
|
|
"""return if the user do have bills or not"""
|
|
|
|
bills_as_ower_number = db.session.query(billowers)\
|
|
|
|
.filter(billowers.columns.get("bill_id") == self.id)\
|
2011-08-09 19:34:46 +02:00
|
|
|
.count()
|
|
|
|
return bills_as_ower_number != 0 or len(self.bills) != 0
|
|
|
|
|
2011-07-31 00:41:28 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Person %s for project %s>" % (self.name, self.project.name)
|
|
|
|
|
|
|
|
# We need to manually define a join table for m2m relations
|
|
|
|
billowers = db.Table('billowers',
|
|
|
|
db.Column('bill_id', db.Integer, db.ForeignKey('bill.id')),
|
|
|
|
db.Column('person_id', db.Integer, db.ForeignKey('person.id')),
|
|
|
|
)
|
|
|
|
|
|
|
|
class Bill(db.Model):
|
2011-09-13 18:15:07 +02:00
|
|
|
|
2011-09-13 22:58:53 +02:00
|
|
|
class BillQuery(BaseQuery):
|
2011-09-13 18:15:07 +02:00
|
|
|
|
|
|
|
def get(self, project, id):
|
|
|
|
try:
|
|
|
|
return self.join(Person, Project)\
|
|
|
|
.filter(Bill.payer_id == Person.id)\
|
|
|
|
.filter(Person.project_id == Project.id)\
|
|
|
|
.filter(Project.id == project.id)\
|
|
|
|
.filter(Bill.id == id).one()
|
|
|
|
except orm.exc.NoResultFound:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def delete(self, project, id):
|
|
|
|
bill = self.get(project, id)
|
|
|
|
if bill:
|
|
|
|
db.session.delete(bill)
|
|
|
|
return bill
|
|
|
|
|
|
|
|
query_class = BillQuery
|
|
|
|
|
|
|
|
_to_serialize = ("id", "payer_id", "owers", "amount", "date", "what")
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
payer_id = db.Column(db.Integer, db.ForeignKey("person.id"))
|
|
|
|
owers = db.relationship(Person, secondary=billowers)
|
|
|
|
|
|
|
|
amount = db.Column(db.Float)
|
|
|
|
date = db.Column(db.Date, default=datetime.now)
|
|
|
|
what = db.Column(db.UnicodeText)
|
|
|
|
|
2011-09-09 19:14:19 +02:00
|
|
|
archive = db.Column(db.Integer, db.ForeignKey("archive.id"))
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
def pay_each(self):
|
|
|
|
"""Compute what each person has to pay"""
|
|
|
|
return round(self.amount / len(self.owers), 2)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Bill of %s from %s for %s>" % (self.amount,
|
|
|
|
self.payer, ", ".join([o.name for o in self.owers]))
|
|
|
|
|
2011-09-09 19:14:19 +02:00
|
|
|
class Archive(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2011-10-13 21:27:56 +02:00
|
|
|
project_id = db.Column(db.String, db.ForeignKey("project.id"))
|
2011-09-09 19:14:19 +02:00
|
|
|
name = db.Column(db.UnicodeText)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def start_date(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
|
|
|
def end_date(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Archive>"
|