-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
459 lines (335 loc) · 11.1 KB
/
models.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""Database SQLAlchemy Models"""
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from sqlalchemy.orm import backref
db = SQLAlchemy()
bcrypt = Bcrypt()
def submit_data(model):
"""Add and commit model to database"""
db.session.add(model)
db.session.commit()
def connect_db(app):
"""Connect to database"""
db.app = app
db.init_app(app)
class User(db.Model):
"""User model"""
__tablename__ = "users"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True)
username = db.Column(
db.String(30),
unique=True,
nullable=False)
password = db.Column(
db.Text,
nullable=False
)
language_pref_id = db.Column(
db.Integer,
db.ForeignKey("languages.id"),
nullable=False
)
language_pref = db.relationship("Language")
bookmarks = db.relationship(
"Drink",
secondary="bookmarks"
)
def __repr__(self):
"""Return data representation of User class."""
return f"<User {self.id}>"
@classmethod
def register(cls, username, pwd, lang_pref_id):
"""Register new user account"""
username = username
hashed = bcrypt.generate_password_hash(pwd, rounds=14)
hashed_utf8 = hashed.decode("utf8")
user = cls(username=username, password=hashed_utf8, language_pref_id=lang_pref_id)
submit_data(user)
return user
@classmethod
def authenticate(cls, username, pwd):
"""Authenticate user.
Returns user instance if password matches.
Returns False if no user is found or if password hashes do not match"""
user = cls.query.filter_by(username=username).one_or_none()
if user and bcrypt.check_password_hash(user.password, pwd):
return user
else:
return False
@classmethod
def check_username(cls, username) -> bool:
"""Checks whether username is present in database."""
return cls.query.filter_by(username=username).one_or_none() if True else False
def serialize(self):
"""Returns dict object of User model"""
return {
"id": self.id,
"username": self.username,
"language_pref": self.language_pref.code
}
def has_bookmark(self, drink_id):
"""Finds relevant bookmark for user object by drink id.
Returns bookmark object if found, else None."""
return Bookmark.query.filter_by(drink_id=drink_id, user_id=self.id).one_or_none()
class Language(db.Model):
"""Model class for languages"""
__tablename__ = "languages"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True)
code = db.Column(
db.String(10),
unique=True,
nullable=False)
name = db.Column(
db.String(100),
unique=True,
nullable=False
)
def __repr__(self):
"""Return instance string representation"""
return f"<Language {self.id} {self.code}>"
@classmethod
def get_id(cls, code):
"""Get id of language code based on array index."""
return ["", "DE", "ES", "FR", "IT", "ZH-HANS", "ZH-HANT"].index(code) + 1
class Category(db.Model):
"""Model class for drink categories"""
__tablename__ = "categories"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True)
name = db.Column(
db.String(100),
unique=True,
nullable=False
)
def __repr__(self):
"""Return instance string representation"""
return f"<Category {self.id} {self.name}>"
class Glass(db.Model):
"""Model class for glass types"""
__tablename__ = "glasses"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True)
name = db.Column(
db.String(100),
unique=True,
nullable=False
)
def __repr__(self):
"""Return instance string represenation"""
return f"<Glass {self.id} {self.name}>"
class Instruction(db.Model):
"""Model class for drink recipe instructions"""
__tablename__ = "instructions"
drink_id = db.Column(
db.Integer,
db.ForeignKey("drinks.id", ondelete="CASCADE"),
primary_key=True
)
language_id = db.Column(
db.Integer,
db.ForeignKey("languages.id"),
primary_key=True
)
text = db.Column(
db.Text,
nullable=False
)
language = db.relationship("Language")
def __repr__(self):
"""Return instance string representation"""
return f"<Instruction drink({self.drink_id}) lang({self.language.code})>"
class Ingredient(db.Model):
"""Model class for recipe ingredient"""
__tablename__ = "ingredients"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True)
name = db.Column(
db.String(30),
unique=True,
nullable=False
)
def __repr__(self):
"""Return instance string representation"""
return f"<Ingredient {self.id} {self.name}>"
@classmethod
def get_ids(cls, ingredient_names):
"""Takes list of ingredient names and returns their ids"""
ids = []
for name in ingredient_names:
ingredient = cls.query.filter_by(name=name).one_or_none()
if ingredient:
ids.append(ingredient.id)
else:
cls.create(name)
ids.append(cls.query.filter_by(name=name).one().id)
return ids
# return [cls.query.filter_by(name=name).one().id for name in ingredient_names]
@classmethod
def create(cls, name):
"""Create new ingredient model and commit to database"""
ingr = cls(name=name)
submit_data(ingr)
class DrinkIngredient(db.Model):
"""Model class for drink recipe to ingredient associations"""
__tablename__ = "drinks_ingredients"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True
)
drink_id = db.Column(
db.Integer,
db.ForeignKey("drinks.id", ondelete="cascade"),
)
ingredient_id = db.Column(
db.Integer,
db.ForeignKey("ingredients.id", ondelete="cascade")
)
quantity = db.Column(
db.Text
)
ingredient = db.relationship("Ingredient")
def __repr__(self):
"""Return instance string representation"""
return f"<DrinkIngredient drink:{self.drink_id} ingredient:{self.ingredient_id}>"
@classmethod
def generate_models(cls, drink_id, ingredient_ids, quantities):
"""Returns list of DrinkIngredient model instances
which associate drink_id with ingredient_ids"""
return [DrinkIngredient(
drink_id=drink_id,
ingredient_id=ingredient_ids[i],
quantity=quantities[i]
) for i in range(len(ingredient_ids))]
class Drink(db.Model):
"""Model class for drinks"""
__tablename__ = "drinks"
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True
)
name = db.Column(
db.String(100),
unique=True,
nullable=False
)
image_url = db.Column(db.Text)
image_attribution = db.Column(db.Text)
video_url = db.Column(db.Text)
alcoholic = db.Column(
db.Boolean,
nullable=False
)
optional_alc = db.Column(
db.Boolean,
nullable=False
)
category_id = db.Column(
db.Integer,
db.ForeignKey("categories.id"),
nullable=False
)
glass_id = db.Column(
db.Integer,
db.ForeignKey("glasses.id"),
nullable=False
)
instructions = db.relationship(
"Instruction",
backref="drink"
)
ingredients = db.relationship(
"DrinkIngredient",
primaryjoin=(DrinkIngredient.drink_id == id)
)
category = db.relationship("Category")
glass = db.relationship("Glass")
def __repr__(self):
"""Returns string representation of instance"""
return f"<Drink {self.name}>"
def serialize(self):
"""Returns dict object containing drink data"""
return {
"id": self.id,
"name": self.name.title(),
"image_url": self.image_url,
"image_attribution": self.image_attribution,
"alcoholic": self.alcoholic,
"optional_alc": self.optional_alc,
"category": self.category.name.title(),
"category_id": self.category_id
}
def get_video_url_id(self):
"""Returns last section of video URL.
This is formatted for YouTube URL links, where the video id is located at the end of the URL."""
return self.video_url.split("=")[-1]
@classmethod
def parse_drink_data(cls, data):
"""Parses JSON data from thecocktaildb API
Returns appropriate Drink, Instruction,
and DrinkIngredient models into an array
"""
category_id = Category.query.filter_by(name=data["strCategory"].lower()).one().id
glass_id = Glass.query.filter_by(name=data["strGlass"].lower()).one().id
[instr_data, ingr_data, quant_data] = [[], [], []]
for key, val in data.items():
if "Measure" in key: quant_data.append(val)
if val is not None and val != "":
if "Ingredient" in key: ingr_data.append(val.lower())
if "Instructions" in key:
instr_data.append((key[15:], val))
instructions = [
Instruction(
drink_id=data["idDrink"],
language_id=Language.get_id(lang_code),
text=text
) for (lang_code, text) in instr_data]
drink_ingredients = DrinkIngredient.generate_models(
data["idDrink"],
Ingredient.get_ids(ingr_data),
quant_data
)
return [
cls(
id=data["idDrink"],
alcoholic=(True if data["strAlcoholic"].lower() == "alcoholic" else False),
optional_alc=(True if data["strAlcoholic"].lower() == "optional alcohol" else False),
category_id=category_id,
name=data["strDrink"].lower(),
image_url=data["strDrinkThumb"] if data["strDrinkThumb"] else data["strImageSource"],
image_attribution=data["strImageAttribution"],
glass_id=glass_id,
video_url=data["strVideo"]
),
instructions,
drink_ingredients
]
class Bookmark(db.Model):
"""Model class for user bookmarks"""
__tablename__ = "bookmarks"
user_id = db.Column(
db.Integer,
db.ForeignKey("users.id", ondelete="cascade"),
primary_key=True
)
drink_id = db.Column(
db.Integer,
db.ForeignKey("drinks.id", ondelete="cascade"),
primary_key=True
)
def __repr__(self):
"""Returns string representation of instance"""
return f"<Bookmark user:{self.user_id} drink:{self.drink_id}>"