This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
143 lines (100 loc) · 4.78 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
141
142
143
""" Models and database functions for Community database. """
from flask_sqlalchemy import SQLAlchemy
# from flask_login import UserMixin
# Connect to the PostgreSQL database
db = SQLAlchemy()
# Model definitions
class Address(db.Model):
"""Standard format for address information for user location."""
__tablename__ = 'addresses'
add_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
lat = db.Column(db.Float(), nullable=False)
lng = db.Column(db.Float(), nullable=False)
formatted_add = db.Column(db.String(200), nullable=True)
def __repr__(self):
"""Prints address object in a more helpful way"""
return "<Address: add_id=%s lat=%s lng=%s>" % (self.add_id,
self.lat,
self.lng)
class Donor(db.Model):
"""Donor information collected when user registers."""
__tablename__ = 'donors'
donor_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=True)
email = db.Column(db.String(75), nullable=False, unique=True)
password = db.Column(db.String(100), nullable=False)
add_id = db.Column(db.ForeignKey(Address.add_id), nullable=True)
phone_number = db.Column(db.String(100), nullable=True)
address = db.relationship("Address", backref=db.backref("donors"))
def __init__(self, name, email, password, add_id):
self.name = name
self.email = email
self.password = password
self.add_id = add_id
def __repr__(self):
"""Prints donor object in a more helpful way"""
return "<Donor: donor_id=%s name=%s email=%s>" % (self.donor_id, self.name,
self.email)
# Necessary functions for Flask-login
# def get_id(self):
# return str(self.user_id)
class Receiver(db.Model):
"""Receiver information collected when user registers."""
__tablename__ = 'receivers'
receiver_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=True)
email = db.Column(db.String(75), nullable=False, unique=True)
password = db.Column(db.String(100), nullable=False)
add_id = db.Column(db.ForeignKey(Address.add_id), nullable=True)
phone_number = db.Column(db.String(100), nullable=True)
address = db.relationship("Address", backref=db.backref("receivers"))
def __init__(self, name, email, password, add_id):
self.name = name
self.email = email
self.password = password
self.add_id = add_id
def __repr__(self):
"""Prints receiver object in a more helpful way"""
return "<Receiver: receiver_id=%s name=%s email=%s>" % (self.receiver_id, self.name,
self.email)
# Necessary functions for Flask-login
# def get_id(self):
# return str(self.user_id)
class Food(db.Model):
"""Food specifications."""
__tablename__ = 'foods'
food_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
donor_id = db.Column(db.ForeignKey(Donor.donor_id), nullable=False)
receiver_id = db.Column(db.ForeignKey(Receiver.receiver_id), nullable=True)
name = db.Column(db.String(50), nullable=False)
date_of_expiration = db.Column(db.DateTime, nullable=False)
servings = db.Column(db.Integer, nullable=False)
prepared = db.Column(db.Boolean, nullable=False)
gluten = db.Column(db.Boolean, nullable=False)
refrigerated = db.Column(db.Boolean, nullable=False)
nuts = db.Column(db.Boolean, nullable=False)
dairy = db.Column(db.Boolean, nullable=False)
vegetarian = db.Column(db.Boolean, nullable=False)
donor = db.relationship("Donor", backref=db.backref("donors"))
receiver = db.relationship("Receiver", backref=db.backref("receivers"))
def __repr__(self):
"""Prints category object in a more helpful way"""
return "<Food: foods_id=%s name=%s>" % (self.food_id,
self.name)
##############################################################################
# Helper function
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our PstgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///food'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
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."
# # In case tables haven't been created, create them
db.create_all()