This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_models.py
153 lines (126 loc) · 4.16 KB
/
db_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
"""
Models for the database tables
"""
from os import getenv
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
BASE = declarative_base()
class User(BASE):
"""
Model for User table
"""
__tablename__ = "user"
id = Column(Integer, primary_key=True)
name = Column(String(32), nullable=False)
email = Column(String(64), nullable=False)
picture = Column(String(256))
restaurant = relationship("Restaurant", cascade="all,delete",
backref="User")
menu_item = relationship("MenuItem", cascade="all,delete", backref="User")
@property
def serialize(self):
"""
Serialize the object to JSON
:return: JSON
"""
return dict(
id=self.id,
name=self.name,
email=self.email,
picture=self.picture
)
def __str__(self):
"""
String representation of object
:return: str
"""
return "<id: {0}, name: {1}, email: {2}>".format(self.id, self.name,
self.email)
class Restaurant(BASE):
"""
Model for Restaurant table
"""
__tablename__ = "restaurant"
rid = Column(Integer, primary_key=True)
name = Column(String(128), nullable=False)
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE",
onupdate="CASCADE"))
user = relationship("User",
backref=backref("Restaurant", passive_deletes=True,
cascade="all,delete"))
menu_item = relationship("MenuItem", cascade="all,delete",
backref="Restaurant")
@property
def serialize(self):
"""
Serialize the object to JSON
:return: JSON
"""
return dict(
rid=self.rid,
name=self.name,
user_id=self.user_id
)
def __str__(self):
"""
String representation of object
:return: str
"""
return "<rid: {0}, name: {1}>".format(self.rid, self.name)
class MenuItem(BASE):
"""
Model for MenuItem table.
"""
__tablename__ = "menu_item"
mid = Column(Integer, primary_key=True)
name = Column(String(128), nullable=False)
course = Column(String(32))
description = Column(String(256))
price = Column(String(8))
rid = Column(Integer, ForeignKey("restaurant.rid", ondelete="CASCADE",
onupdate="CASCADE"))
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE",
onupdate="CASCADE"))
user = relationship("User",
backref=backref("MenuItem", passive_deletes=True,
cascade="all,delete"))
restaurant = relationship("Restaurant",
backref=backref("MenuItem", passive_deletes=True,
cascade="all,delete"))
@property
def serialize(self):
"""
Serialize the object to JSON
:return: JSON
"""
return dict(
mid=self.mid,
name=self.name,
course=self.course,
description=self.description,
price=self.price,
rid=self.rid,
user_id=self.user_id
)
def __str__(self):
"""
String representation of object
:return: str
"""
return "<mid: {0}, name: {1}>".format(self.mid, self.name)
def create_models(db_uri):
"""
Create the database tables
:param db_uri: Databse URI
:return: None
"""
print("Using db_uri: {0} to create models...".format(db_uri))
db_engine = create_engine(db_uri)
BASE.metadata.create_all(db_engine)
print("Database models have been created successfully.")
if __name__ == "__main__":
if getenv("DATABASE_URL"):
create_models(getenv("DATABASE_URL"))
else:
create_models("sqlite:///restaurant_menu_with_users.db")