-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
131 lines (98 loc) · 4.66 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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, date
db = SQLAlchemy()
class User(db.Model):
"""A user."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
full_name = db.Column(db.String(75))
username = db.Column(db.String(25), unique=True, nullable=False)
email = db.Column(db.String(50), unique=True, nullable=False)
password = db.Column(db.String(25), nullable=False)
created_at = db.Column(db.DateTime, nullable=False, default=date.today())
user_image = db.Column(db.String)
# user.recipes
# user.favorites
# user.preferences
def __repr__(self):
return f"<User user_id={self.user_id}, email={self.email}, username={self.username}>"
class Recipe(db.Model):
"""A recipe."""
__tablename__ = "recipes"
recipe_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(100), nullable=False)
cuisine = db.Column(db.String(25))
created_at = db.Column(db.DateTime, nullable=False, default=date.today())
added_by = db.Column(db.Integer, db.ForeignKey("users.user_id"))
diet = db.Column(db.String)
meal = db.Column(db.String) # meal type (breakf, lunch etc)
private = db.Column(db.Boolean, default=False)
#recipe.steps
#recipe.favorites
#recipes.ingredients
#recipe.images
user = db.relationship("User", backref="recipes")
def __repr__(self):
return f"<Recipe recipe_id={self.recipe_id}, title={self.title}, added_by={self.added_by}>"
class Step(db.Model):
"""Recipe preparation steps."""
__tablename__ = "steps"
step_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
instruction = db.Column(db.Text, nullable=False)
step_image = db.Column(db.String)
recipe_id = db.Column(db.Integer, db.ForeignKey("recipes.recipe_id"), nullable=False)
order = db.Column(db.Integer, nullable=False)
recipe = db.relationship("Recipe", backref="steps")
def __repr__(self):
return f"<Step step_id={self.step_id}, recipe={self.recipe_id}>"
class Favorite(db.Model):
"""Recipes added to favorites by particular user."""
__tablename__ = "favorites"
fav_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
recipe_id = db.Column(db.Integer, db.ForeignKey("recipes.recipe_id"), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("users.user_id"), nullable=False)
recipe = db.relationship("Recipe", backref="favorites")
user = db.relationship("User", backref="favorites")
def __repr__(self):
return f"<Favorite recipe_id={self.recipe_id}, chosen by user_id={self.user_id}>"
class Ingredient(db.Model):
__tablename__ = "ingredients"
ing_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
ing_name = db.Column(db.String(100), nullable=False)
qty = db.Column(db.Integer, nullable=False)
image = db.Column(db.String)
recipe_id = db.Column(db.Integer, db.ForeignKey("recipes.recipe_id"), nullable=False)
unit = db.Column(db.String, nullable=False, default="gram")
recipe = db.relationship("Recipe", backref="ingredients")
def __repr__(self):
return f"<Ingredient ing_id={self.ing_id}, ing_name={self.ing_name}, recipe_id={self.recipe_id}>"
class Image(db.Model):
__tablename__ = "images"
img_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
url = db.Column(db.String)
#step_id = db.Column(db.Integer, db.ForeignKey("step.step_image"), nullable=True)
recipe_id = db.Column(db.Integer, db.ForeignKey("recipes.recipe_id"), nullable=True)
#step = db.relationship("Step", backref="images")
recipe = db.relationship("Recipe", backref="images")
def __repr__(self):
return f"<Image img_id={self.img_id}, recipe_id={self.recipe_id}>"
class Preference(db.Model):
"""User's interests (diet, cousine)."""
__tablename__ = "preferences"
pref_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.user_id"), nullable=False)
preference = db.Column(db.String,nullable=False)
user = db.relationship("User", backref="preferences")
def __repr__(self):
return f"<Preferences for user {self.user_id}: {self.preference}>"
def connect_to_db(app, db_uri="postgresql:///eat_this", echo=True):
"""Connect to database."""
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
app.config["SQLALCHEMY_ECHO"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
print("Connected to the db!")
if __name__ == "__main__":
from server import app
connect_to_db(app)