/
model.py
140 lines (98 loc) · 5.12 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Model for RoomEase site."""
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
import datetime
db = SQLAlchemy()
###################################################################
# Model Definitions
class User(db.Model):
"""User of RoomEase website."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(64), nullable=True, unique=True)
password = db.Column(db.String(64), nullable=True)
name = db.Column(db.String(30), nullable=False)
house_id = db.Column(db.Integer, ForeignKey('houses.house_id'), nullable=False)
phone = db.Column(db.Integer, nullable=False, unique=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<User user_id=%s house_id=%s email=%s>" % (self.user_id, self.house_id, self.email)
def __init__(self, email, password, name, phone, house_id):
self.email = email
self.password = password
self.name = name
self.phone = phone
self.house_id = house_id
class House(db.Model):
"""Model for each individual group of roommates (houses)."""
__tablename__ = "houses"
house_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
address = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<House house_id=%s address=%s>" % (self.house_id, self.address)
# Define relationship to user
users = db.relationship("User", backref=db.backref("house", order_by=house_id))
# Define relationship to bill
bills = db.relationship("Bill", backref=db.backref("house", order_by=house_id))
class Bill(db.Model):
"""Model for each individual bill for a house."""
__tablename__ = "bills"
bill_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
house_id = db.Column(db.Integer, ForeignKey('houses.house_id'), nullable=False)
due_date = db.Column(db.Date, nullable=False)
amount = db.Column(db.Float, nullable=False)
description = db.Column(db.String(20), nullable=False)
paid = db.Column(db.Boolean, default=False, nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Bill bill_id=%s house_id=%s description=%s due_date=%s amount=%s paid=%s>" % (self.bill_id, self.house_id, self.description, self.due_date, self.amount, self.paid)
class User_Payment(db.Model):
"""Model to track who's paid what from the perspective of an individual user."""
__tablename__ = "user_payments"
user_payment_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, ForeignKey('users.user_id'), nullable=False)
bill_id = db.Column(db.Integer, ForeignKey('bills.bill_id'), nullable=False)
amount = db.Column(db.Integer, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.datetime.now())
paid = db.Column(db.Boolean, default=False, nullable=False)
def calculate_user_portion(self):
"""This function will query to count all of the users in a house,
then divide the total amount of a given bill by that count
to return the portion of the bill each user is personally responsible for."""
bill = Bill.query.filter_by(bill_id=bill_id).one()
count = User.query.filter_by(house_id=house_id).count()
user_portion = (bill.amount/count)
return user_portion
def __repr__(self):
"""Provide helpful representation when printed."""
return "<User_Payment user_id=%s bill_id=%s>" % (self.user_id, self.bill_id)
# Define relationship to user
user = db.relationship("User", backref=db.backref("user_payments", order_by=bill_id))
# Define relationship to bill
bill = db.relationship("Bill", backref=db.backref("bills", order_by=bill_id))
class Message(db.Model):
"""Model for the table which will contain messages sent via the message board."""
__tablename__ = "messages"
message_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
user_id = db.Column(db.Integer, ForeignKey('users.user_id'), nullable=False)
created_at = db.Column(db.DateTime, nullable=False)
content = db.Column(db.String(140), nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Message user_id=%s message_id=%s created_at=%s>" % (self.user_id, self.message_id, self.created_at)
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our SQLite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///roomease.db'
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."