-
Notifications
You must be signed in to change notification settings - Fork 10
/
models.py
166 lines (129 loc) · 4.19 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from peewee import Model, SqliteDatabase, PostgresqlDatabase, Check
from peewee import DecimalField, TextField, CharField
from peewee import UUIDField, ForeignKeyField, IntegerField, BooleanField
from schemas import ItemSchema, UserSchema, AddressSchema
from schemas import OrderSchema, OrderItemSchema, FavoritesSchema
from passlib.hash import pbkdf2_sha256
from jsonschema import validate
from marshmallow_jsonschema import JSONSchema
from urllib.parse import urlparse
import uuid
import os
ENVIRONMENT = os.getenv('ENVIRONMENT', 'dev')
database = ''
if ENVIRONMENT == 'dev':
database = SqliteDatabase('database.db')
else:
url_db = urlparse(os.getenv('DATABASE_URL'))
database = PostgresqlDatabase(
url_db.path[1:],
user=url_db.username,
password=url_db.password,
host=url_db.hostname,
)
class BaseModel(Model):
class Meta:
database = database
def reload(self):
cls = type(self)
return cls.get(cls.id == self.id)
@classmethod
def get_schema(cls):
raise NotImplementedError
@classmethod
def verify_json(cls, json):
schema = cls.get_schema()
json_schema = JSONSchema().dump(schema).data
validate(json, json_schema)
def json(self):
schema = self.get_schema()
return schema.dump(self).data
@classmethod
def count(cls):
return cls.select().count()
class Item(BaseModel):
uuid = UUIDField(unique=True)
name = CharField()
price = DecimalField(max_digits=20)
description = TextField()
category = CharField()
availability = IntegerField(constraints=[Check('availability >= 0')])
@classmethod
def get_schema(cls):
return ItemSchema()
class User(BaseModel):
uuid = UUIDField(unique=True)
first_name = CharField()
last_name = CharField()
email = CharField(unique=True)
password = CharField()
superuser = BooleanField(default=False)
status = CharField(default='enable', constraints=[Check(
"status IN ('deleted','blocked','enable')")])
@classmethod
def get_schema(cls):
return UserSchema()
def verify_password(self, origin_password):
return pbkdf2_sha256.verify(origin_password, self.password)
def favorite_items(self):
return [favorite.item.json() for favorite in self.favorites]
def add_favorite(self, item):
favorite = Favorites.create(
uuid=uuid.uuid4(),
user=self,
item=item,
)
return favorite
def remove_favorite(self, item):
Favorites.delete().where(Favorites.item == item, Favorites.user == self).execute()
return None
class Address(BaseModel):
uuid = UUIDField(unique=True)
user = ForeignKeyField(User, related_name="address")
nation = CharField()
city = CharField()
postal_code = CharField()
local_address = CharField()
phone = CharField()
@classmethod
def get_schema(cls):
return AddressSchema()
class Order(BaseModel):
uuid = UUIDField(unique=True)
total_price = DecimalField(max_digits=20)
user = ForeignKeyField(User, related_name="orders")
@classmethod
def get_schema(cls):
return OrderSchema()
class OrderItem(BaseModel):
order = ForeignKeyField(Order, related_name="order_items")
item = ForeignKeyField(Item)
quantity = IntegerField()
subtotal = DecimalField(max_digits=20)
@classmethod
def get_schema(cls):
return OrderItemSchema()
class Picture(BaseModel):
uuid = UUIDField(unique=True)
title = CharField()
extension = CharField()
item = ForeignKeyField(Item, related_name="pictures")
def json(self):
return {
'uuid': str(self.uuid),
'title': self.title,
'extension': self.extension,
}
class Favorites(BaseModel):
uuid = UUIDField(unique=True)
user = ForeignKeyField(User, related_name="favorites")
item = ForeignKeyField(Item, related_name="favorites")
@classmethod
def get_schema(cls):
return FavoritesSchema()
def json(self):
return {
'uuid': str(self.uuid),
'user': str(self.user.uuid),
'item': str(self.item.uuid)
}