forked from CircleCI-Public/sample-python-cfd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
126 lines (101 loc) · 3.37 KB
/
models.py
File metadata and controls
126 lines (101 loc) · 3.37 KB
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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import DataError
db = SQLAlchemy()
# helper method for easy mocking
def _commit_item(item):
db.session.add(item)
db.session.commit()
class MenuItem(db.Model):
# definitely messes this up, the id should not be specified in the request
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String())
name = db.Column(db.String())
price = db.Column(db.Float())
# image_id = db.Column(db.Integer) # this is probably broken
image_id = db.Column(db.Integer, db.ForeignKey("image.id"))
cart_id = db.Column(db.String, db.ForeignKey("cart.host"), nullable=True)
def __init__(
self,
description,
name,
price,
image_id,
cart_id=None,
):
self.description = description
self.name = name
self.price = price
self.image_id = image_id # this should probably be anohter reference to a model
self.cart_id = cart_id
def __repr__(self):
return "<id {}>".format(self.id)
# something like marshmallow would be a good addition if we were planning to scale this, but
# there's just one model
def serialize(self):
return {
"description": self.description,
"id": self.id,
"imageId": self.image_id,
"name": self.name,
"price": self.price,
}
@classmethod
def add(cls, menu_item):
item = cls(
menu_item.description, menu_item.name, menu_item.price, menu_item.image_id
)
_commit_item(item)
return item
@classmethod
def query_all(cls):
return cls.query.all()
@classmethod
def query_by_id(cls, item_id):
return cls.query.filter(MenuItem.id == item_id).first()
class Cart(db.Model):
host = db.Column(db.String, primary_key=True)
items = db.relationship("MenuItem")
def __init__(self, host):
self.host = host
def __repr__(self):
return "<host {}>".format(self.host)
@classmethod
def add_item(cls, host, menu_item):
if len(Cart.query.filter(Cart.host == host).all()) < 1:
new_cart = cls(host)
db.session.add(new_cart)
db_item = MenuItem.query.filter(MenuItem.id == menu_item.id).first()
db_item.cart_id = host
db.session.commit()
@classmethod
def query_by_host(cls, host):
return cls.query.filter(Cart.host == host).first()
@classmethod
def delete_item_by_id(cls, host, item_id):
cart = cls.query_by_host(host)
for item in cart.items:
if item.id == item_id:
cart.items.remove(item)
break
db.session.commit()
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.LargeBinary)
def __init__(self, data):
self.data = data
def __repr__(self):
return "<id {}>".format(self.id)
@classmethod
def add(cls, raw_data):
image = cls(raw_data)
_commit_item(image)
return image
@classmethod
def delete_image(cls, image_id):
if not (image := cls.get_image(image_id)):
raise DataError
db.session.delete(image)
db.session.commit()
@classmethod
def get_image(cls, image_id):
return cls.query.filter(Image.id == image_id).first()