-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
131 lines (104 loc) · 3.33 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
from sqlalchemy import Column, String, Integer, create_engine
from flask_sqlalchemy import SQLAlchemy
import json
import os
from datetime import datetime
from sqlalchemy.sql import func
database_path = os.environ["DATABASE_URL"]
db = SQLAlchemy()
"""
setup_db(app)
binds a flask application and a SQLAlchemy service
"""
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
"""
Chocolate
Food made with chocolate, type of chocolate, vendor(foreign key/join table)
"""
class Chocolate(db.Model):
__tablename__ = "Chocolate"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=False)
chocolate_type = Column(String, nullable=False)
vendor = Column(String, nullable=False)
vendor_id = db.Column(
db.Integer, db.ForeignKey("Chocolatier.id", ondelete="CASCADE")
)
comments = Column(String)
def __init__(self, name, chocolate_type, vendor, vendor_id, comments=""):
self.name = name
self.chocolate_type = chocolate_type
self.vendor = vendor
self.vendor_id = vendor_id
self.comments = comments
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
"id": self.id,
"name": self.name,
"chocolate_type": self.chocolate_type,
"vendor": self.vendor,
"vendor_id": self.vendor_id,
"comments": self.comments,
}
def __repr__(self):
return "<Chocolate " + str(self.id) + " " + str(self.name) + ">"
"""
Chocolatier
Chocolatier details: vendor/biz name, address, chef,
website, phone, reviews/comments
"""
class Chocolatier(db.Model):
__tablename__ = "Chocolatier"
id = Column(Integer().with_variant(Integer, "postgres"), primary_key=True)
name = Column(String, nullable=False)
address = Column(String, nullable=False)
website = Column(String)
facebook = Column(String)
phone = Column(String, nullable=False)
chef = Column(String)
comments = Column(String)
chocolates = db.relationship(
"Chocolate", backref="chocolatier", passive_deletes=True
)
def __init__(self, name, address, website, facebook, phone, chef, comments=""): # noqa
self.name = name
self.address = address
self.website = website
self.facebook = facebook
self.phone = phone
self.chef = chef
self.comments = comments
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
"id": self.id,
"name": self.name,
"address": self.address,
"website": self.website,
"facebook": self.facebook,
"phone": self.phone,
"chef": self.chef,
"comments": self.comments,
}
def __repr__(self):
return "<Chocolatier " + str(self.id) + " " + str(self.name) + ">"