-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
67 lines (48 loc) · 1.78 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
import uuid
from sqlalchemy.ext.associationproxy import association_proxy
from app import db
uuid_key = lambda: str(uuid.uuid4())
class Cart(db.Model):
id = db.Column(db.Unicode(36), default=uuid_key, primary_key=True)
cart_products = db.relationship('CartProduct', backref='cart')
products = association_proxy('cart_products', 'product')
def total_price(self):
total_price = 0
for item in self.cart_products:
total_price += item.product.price * item.quantity
return total_price
def __repr__(self):
return '{0} cart for {1}'.format(self.id, str(self.total_price()))
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(100))
description = db.Column(db.UnicodeText)
price = db.Column(db.DECIMAL(precision=2), db.CheckConstraint('price>0'))
availability = db.Column(
'availability',
db.Integer,
db.CheckConstraint('availability>=0'),
default=0,
)
img_file_path = db.Column(db.Unicode(128), nullable=True)
img_url = db.Column(db.Unicode(200))
def __repr__(self):
return '{0} {1:.2f}$'.format(self.title, self.price)
class CartProduct(db.Model):
product_id = db.Column(
db.Integer,
db.ForeignKey('product.id'),
primary_key=True,
)
cart_id = db.Column(
db.Unicode(36),
db.ForeignKey('cart.id'),
primary_key=True,
)
quantity = db.Column('quantity', db.Integer(), nullable=False)
def __init__(self, product=None, quantity=1):
self.product = product
self.quantity = quantity
product = db.relationship(Product, lazy='joined')
def __repr__(self):
return '{0} of {1} in {2} cart'.format(self.product, self.quantity, self.cart)