forked from LinvernSS/afs-labs-student
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
343 lines (236 loc) · 15.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_utils import ArrowType
import arrow
db = SQLAlchemy()
class Dietary_Restriction(db.Model):
"""Dietary Restriction. i.e. Vegan, Gluten-Free, Organic"""
__tablename__ = "dietary_restrictions"
diet_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self):
return "<Dietary_Restriction diet_id={} name={}>".format(self.diet_id,
self.name)
class Customer(db.Model):
"""Customer of Farm to Front Door"""
__tablename__ = "customers"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
first_name = db.Column(db.String(100), nullable=True)
last_name = db.Column(db.String(100), nullable=True)
email = db.Column(db.String(100), nullable=False, unique=True)
password_hash = db.Column(db.String(500), nullable=False)
street_address = db.Column(db.String(100), nullable=True)
zipcode = db.Column(db.String(15), nullable=True)
state = db.Column(db.String(2), nullable=True)
phone = db.Column(db.String(30), nullable=True)
orders = db.relationship("Order", backref="customer")
diets = db.relationship("Dietary_Restriction",
secondary="customer_restrictions",
backref="customers")
recipes = db.relationship("Recipe",
secondary="customer_recipes",
backref="customers")
def __repr__(self):
return "<Customer id={}, first_name={}, last_name={}, email={}>".format(self.user_id,
self.first_name,
self.last_name,
self.email)
class Customer_Restriction(db.Model):
"""Association table. Relates Customer class to Dietary_Restriction class."""
__tablename__ = "customer_restrictions"
cust_restr_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customers.user_id'), nullable=False)
diet_id = db.Column(db.Integer, db.ForeignKey('dietary_restrictions.diet_id'), nullable=False)
def __repr__(self):
return "<Customer_Restriction cust_restr_id={}, customer_id={}, diet_id={}>".format(self.cust_restr_id,
self.customer_id,
self.diet_id)
class Pickup(db.Model):
"""Pickup locations"""
__tablename__ = "pickups"
pickup_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=True)
street_address = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(100), nullable=False)
zipcode = db.Column(db.String(15), nullable=False)
state = db.Column(db.String(2), nullable=False, default="CA")
def __repr__(self):
return "<Pickup pickup_id={}, name={}, street_address={}, zipcode={}>".format(self.pickup_id,
self.name,
self.street_address,
self.zipcode)
class Icon(db.Model):
"""Icon for web usage"""
__tablename__ = "icons"
icon_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
url = db.Column(db.String(500), nullable=False)
credit = db.Column(db.String(100), nullable=False)
def __repr__(self):
return "<Icon icon_id={} url={} credit={}>".format(self.icon_id,
self.url,
self.credit)
class Product(db.Model):
"""Product sold by Farm to Front Door"""
__tablename__ = "products"
product_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(200), nullable=False)
description = db.Column(db.Unicode, nullable=True) # db.Text(collation='utf8', convert_unicode=True)
weight = db.Column(db.Numeric(asdecimal=False), nullable=True) # db.Numeric(asdecimal=False)
unit = db.Column(db.String(50), nullable=True)
price = db.Column(db.Numeric(asdecimal=False), nullable=False) # db.Numeric(asdecimal=False)
price_per = db.Column(db.Numeric(asdecimal=False), nullable=True) # db.Numeric(asdecimal=False)
per_unit = db.Column(db.String(50), nullable=True)
aisle = db.Column(db.String(50), nullable=True)
category = db.Column(db.String(50), nullable=True)
img = db.Column(db.String(500), nullable=True)
icon_id = db.Column(db.Integer, db.ForeignKey('icons.icon_id'), nullable=True)
color = db.Column(db.String(10), nullable=True)
search_term = db.Column(db.String(50), nullable=True)
search_strength = db.Column(db.Integer, nullable=True)
icon = db.relationship("Icon", backref="products")
tags = db.relationship("Tag",
secondary="product_tags",
backref="products")
delivery_qty = db.relationship("Delivery_Quantity", backref="product")
order_qty = db.relationship("Order_Quantity", backref="product")
def __repr__(self):
return "<Product product_id={} name={} weight={} unit={} price={}>".format(self.product_id,
self.name,
self.weight,
self.unit,
self.price)
class Tag(db.Model):
"""Tag for products i.e. Certified Organic, Locally Grown"""
__tablename__ = "tags"
tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self):
return "<Tag tag_id={} name={}>".format(self.tag_id, self.name)
class Product_Tag(db.Model):
"""Association table relating Tag class to Product class"""
__tablename__ = "product_tags"
prod_tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('products.product_id'), nullable=False)
tag_id = db.Column(db.Integer, db.ForeignKey('tags.tag_id'), nullable=False)
def __repr__(self):
return "<Product_Tag prod_tag_id={} product_id={} tag_id={}>".format(self.prod_tag_id,
self.product_id,
self.tag_id)
class Recipe(db.Model):
"""Recipe pulled from Edamam API"""
__tablename__ = "recipes"
recipe_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
url = db.Column(db.String(300), nullable=False)
name = db.Column(db.String(100), nullable=False)
ingredients = db.Column(db.JSON, nullable=False)
img = db.Column(db.String(300), nullable=True)
def __repr__(self):
return "<Recipe name={} ingredients={} url={} img={}>".format()
class Customer_Recipe(db.Model):
"""Association table for Recipes each Customer has liked"""
__tablename__ = "customer_recipes"
cust_rec_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customers.user_id'), nullable=False)
recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.recipe_id'), nullable=False)
def __repr__(self):
return "<Customer_Recipe cust_rec_id={} customer_id={} recipe_id={}>".format(self.cust_rec_id,
self.customer_id,
self.recipe_id)
class Delivery(db.Model):
"""A delivery of incoming products, composed of Delivery-Quantities"""
__tablename__ = "deliveries"
delivery_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
vendor = db.Column(db.String(500), nullable=True)
received_at = db.Column(ArrowType, nullable=False) # or db.DateTime v. db.TimeStamp?
quantities = db.relationship("Delivery_Quantity", backref="delivery")
def __repr__(self):
return "<Delivery delivery_id={} vendor={} received_at={}>".format(self.delivery_id,
self.vendor,
self.received_at)
class Delivery_Quantity(db.Model):
"""An amount of a certain product, in each delivery"""
__tablename__ = "delivery_quantities"
deliv_qty_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey("products.product_id"), nullable=False)
product_qty = db.Column(db.Integer, nullable=False)
delivery_id = db.Column(db.Integer, db.ForeignKey("deliveries.delivery_id"), nullable=False)
def __repr__(self):
return "<Delivery_Quantity deliv_qty_id={} product_id={} product_qty={} delivery_id={}>".format(self.deliv_qty_id,
self.product_id,
self.product_qty,
self.delivery_id)
class Order(db.Model):
"""An order placed by a customer, composed of Order-Quantities"""
__tablename__ = "orders"
order_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey("customers.user_id"), nullable=False)
placed_at = db.Column(ArrowType, nullable=False) # or db.DateTime v. db.TimeStamp?
total = db.Column(db.Numeric, nullable=False)
pickup_id = db.Column(db.Integer, db.ForeignKey("pickups.pickup_id"), nullable=False)
received_at = db.Column(ArrowType, nullable=True) # or db.DateTime v. db.TimeStamp?
pickup = db.relationship("Pickup", backref="orders")
quantities = db.relationship("Order_Quantity", backref="order")
def __repr__(self):
return "<Order order_id={} customer_id={} total={} placed_at={} received_at={}>".format(self.order_id,
self.customer_id,
self.total,
self.placed_at,
self.received_at)
class Order_Quantity(db.Model):
"""An amount of a certain product, in each order"""
__tablename__ = "order_quantities"
order_qty_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey("products.product_id"), nullable=False)
product_qty = db.Column(db.Integer, nullable=False, default=1)
order_id = db.Column(db.Integer, db.ForeignKey("orders.order_id"), nullable=False)
def __repr__(self):
return "<Order_Quantity order_qty_id={} product_id={} product_qty={} order_id={}>".format(self.order_qty_id,
self.product_id,
self.product_qty,
self.order_id)
def connect_to_db(app):
try:
database = 'postgresql://' + os.environ["POSTGRES_USER"] + ':' + os.environ["POSTGRES_PASSWORD"] + '@' + \
os.environ["DB_SERVICE_SERVICE_HOST"] + ':' + os.environ["DB_SERVICE_PORT_5432_TCP_PORT"] + '/shop'
except:
database = 'postgresql://' + os.environ["POSTGRES_USER"] + ':' + os.environ["POSTGRES_PASSWORD"] + '@' + \
os.environ["DB_CONTAINER_NAME"] + ':5432/shop'
"""Connect the database to Flask app."""
# Configure to use PostgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
def example_data():
"""Populate test database"""
user1 = Customer(first_name="Jane", last_name="Doe", email="[email protected]",
password_hash="$pbkdf2-sha256$20000$GuPc.z/HmPN.LwUA4FwLQQ$B8HsLHPOCBO2YFBoeF9IVS2UbB78QxoWVrnWG8Nb6h0")
user2 = Customer(first_name="Susie", last_name="Q", email="[email protected]",
password_hash="$pbkdf2-sha256$20000$GuPc.z/HmPN.LwUA4FwLQQ$B8HsLHPOCBO2YFBoeF9IVS2UbB78QxoWVrnWG8Nb6h0",
street_address="100 Main St", zipcode="90210", state="CA")
pickup = Pickup(name="Parnassus Farmers' Market (UCSF)", description="Wednesdays: 10 a.m-3 p.m.",
street_address="505 Parnassus Avenue", zipcode="94122", state="CA")
icon = Icon(url="https://d30y9cdsu7xlg0.cloudfront.net/png/404999-200.png", credit="Blackberry Jam By Nikita Kozin, RU")
order = Order(customer_id=1, placed_at=arrow.utcnow(), total=35.00, pickup_id=1)
product = Product(name="Organic Blackberries", description="Sweet and tart, these delicious blackberries are the perfect fall fruit.",
weight=6, unit="oz", price_per=3.99, price=3.99, per_unit="oz", aisle="Produce",
category="New & Peak Season", img="http://goodeggs2.imgix.net/product_photos/NmgHoSgSqmShNF10cLni_blackberries_01.jpg?w=380&h=238&fm=jpg&q=41&fit=crop",
icon_id=1)
recipe = Recipe(name="Persimmon Cosmopolitan", url="http://foodandstyle.com/2012/12/20/persimmon-cosmopolitan/",
ingredients="1 lb (454gr) very ripe fuyu or hachiya persimmons (3 medium) \u2013 peeled and cut in 1\u201d pieces, 1 tablespoon fresh lime juice, 4 oz (118ml) vodka, 3 oz (89ml) persimmon pur\u00e9e, 1 oz (30ml) fresh lime juice, 1 oz (30ml) st. germain elderflower liqueur, 2 dashes lemon bitters, 2 lime wheels as garnish",
img="https://www.edamam.com/web-img/536/536f7846c9def37bad1b763922c31c29.jpg")
customer_recipe = Customer_Recipe(recipe_id=1, customer_id=1)
tag = Tag(name="Organic")
product_tag = Product_Tag(product_id=1, tag_id=1)
db.session.add_all([user1, user2, pickup, icon, recipe, tag])
db.session.commit()
db.session.add_all([order, product, customer_recipe])
db.session.commit()
db.session.add(product_tag)
db.session.commit()
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.")