1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00
ihatemoney_ynh/budget/models.py
2011-07-23 20:36:13 +02:00

54 lines
1.6 KiB
Python

from datetime import datetime
from flaskext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
# define models
class Project(db.Model):
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")
def __repr__(self):
return "<Project %s>" % self.name
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey("project.id"))
bills = db.relationship("Bill", backref="payer")
name = db.Column(db.UnicodeText)
# activated = db.Column(db.Boolean, default=True)
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):
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)
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]))