aboutsummaryrefslogtreecommitdiff
path: root/budget/models.py
diff options
context:
space:
mode:
authorAlexis Metaireau <alexis@notmyidea.org>2011-07-23 18:45:40 +0200
committerAlexis Metaireau <alexis@notmyidea.org>2011-07-23 18:45:40 +0200
commit5e63a5034b841b90b8a8b2b5bc39cbb088e9803d (patch)
tree82c43d068ec782abd31864e998f09782c9b67be3 /budget/models.py
parent4fcaf7d7ec583b794c14597d50abc89ef96450c9 (diff)
downloadihatemoney-mirror-5e63a5034b841b90b8a8b2b5bc39cbb088e9803d.zip
ihatemoney-mirror-5e63a5034b841b90b8a8b2b5bc39cbb088e9803d.tar.gz
ihatemoney-mirror-5e63a5034b841b90b8a8b2b5bc39cbb088e9803d.tar.bz2
Split the logic into different python modules:
* web.py contains the controllers (also called views) + url definitions * models.py contains the models * forms.py contains the forms * utils.py contains a set of utility fonctions to ease the dev. process
Diffstat (limited to 'budget/models.py')
-rw-r--r--budget/models.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/budget/models.py b/budget/models.py
new file mode 100644
index 0000000..27e30b3
--- /dev/null
+++ b/budget/models.py
@@ -0,0 +1,54 @@
+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)
+ status = db.Column(db.Boolean)
+
+ 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]))
+
+