-
Notifications
You must be signed in to change notification settings - Fork 13
/
models.py
251 lines (216 loc) · 7.67 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'''
Shopping Project Database
-----------------------------------------------------------------
Database Model For Shopping Site
author : Nitesh Kumar Niranjan <[email protected]>
'''
import datetime
from flask_bcrypt import generate_password_hash
from flask_login import UserMixin
from peewee import *
from playhouse.migrate import SqliteMigrator
import uuid
# Default database
DATABASE = SqliteDatabase('shop.db')
migrator = SqliteMigrator(DATABASE)
# User Table
class User(UserMixin, Model):
"""App Users Table"""
full_name = CharField()
email = CharField(unique=True)
password = CharField(max_length=100)
mobile_no = CharField()
joined_at = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
database = DATABASE
@classmethod
def create_user(cls, full_name, email, password, mobile_no, admin=False):
try:
cls.create(
full_name = full_name,
email = email,
password = generate_password_hash(password),
mobile_no = mobile_no,
is_admin = admin
)
except IntegrityError:
raise ValueError("User already exists")
class Product(Model):
"""Products Table"""
name = CharField()
title = CharField()
image_1 = CharField()
image_2 = CharField()
image_3 = CharField()
count = IntegerField()
actual_price = IntegerField(null=False)
off_percent = IntegerField(null=False)
buy_price = IntegerField(null=False)
style = CharField()
lenses_color = CharField()
frame_color = CharField()
brand_name = CharField()
lenses_material = CharField()
frame_material = CharField()
usage = CharField()
packaging = CharField()
uv_protection = CharField()
model_no = CharField()
suitable_for = CharField()
size = CharField()
ideal_for = CharField()
typ_e = CharField()
features = CharField()
case_type = CharField()
dimensions_bridgesize = CharField()
dimensions_hrizontal_width = CharField()
dimensions_frame_arm_lenght = CharField()
weight = CharField()
other_details = TextField()
published_at = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
order_by = ('-published_at',)
@classmethod
def add_product(cls, name, image_1, image_2, image_3, count, actual_price, off_percent, buy_price,
style, lenses_color, frame_color, brand_name, lenses_material, frame_material, usage, packaging,
uv_protection, model_no, suitable_for, size, ideal_for, typ_e, features, case_type, dimensions_bridgesize,
dimensions_hrizontal_width, dimensions_frame_arm_lenght, weight, other_details):
try:
_title = name.replace(" ", "_").lower()
cls.create(
name = name,
title = _title,
image_1 = image_1,
image_2 = image_2,
image_3 = image_3,
count = count,
actual_price = actual_price,
off_percent = off_percent,
buy_price = buy_price,
style = style,
lenses_color = lenses_color,
frame_color = frame_color,
brand_name = brand_name,
lenses_material = lenses_material,
frame_material = frame_material,
usage = usage,
packaging = packaging,
uv_protection = uv_protection,
model_no = model_no,
suitable_for = suitable_for,
size = size,
ideal_for = ideal_for,
typ_e = typ_e,
features = features,
case_type = case_type,
dimensions_bridgesize = dimensions_bridgesize,
dimensions_hrizontal_width = dimensions_hrizontal_width,
dimensions_frame_arm_lenght = dimensions_frame_arm_lenght,
weight = weight,
other_details = other_details,
)
except IntegrityError:
raise ValueError("Some Error Happened")
class Comment(Model):
user = ForeignKeyField(User, related_name='user_comment')
product = ForeignKeyField(Product, related_name='products_comment')
text = TextField()
rating = IntegerField()
comment_time = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
@classmethod
def add_comment(cls, user, product, text, rating):
try:
cls.create(
user = user,
product = product,
text = text,
rating = rating
)
except IntegrityError :
raise ValueError("Some Error Happened")
class Cart(Model):
user_email = ForeignKeyField(User, related_name='carts')
product_id = ForeignKeyField(Product, related_name='products')
count = IntegerField()
class Meta:
database = DATABASE
@classmethod
def add_product(cls, user_email_id, product_id_id, count=1):
try:
cls.create(
user_email_id=user_email_id,
product_id_id=product_id_id,
count=count
)
except IntegrityError:
raise ValueError("Some Error Happened")
class BuyHistory(Model):
"""Item Buying History"""
order_id = CharField(max_length=50, unique=True)
product_id = ForeignKeyField(Product, related_name='product')
buyer = ForeignKeyField(User, related_name='customer')
product_name = CharField()
buyer_name = CharField()
mobile_no = IntegerField()
payment_option = CharField()
product_quantity = IntegerField()
buyer_address = TextField()
buy_time = DateTimeField(default=datetime.datetime.now)
status = CharField()
delivered = BooleanField()
deliverTime = DateTimeField(null = True, default = datetime.datetime.now)
class Meta:
database = DATABASE
order_by = ('buy_time',)
@classmethod
def add_history(cls, buyer, product_id, product_name, product_quantity, buyer_name, buyer_address, mobile_no, payment_option, status="Initiated", delivered=False):
cls.create(
order_id = str(uuid.uuid4()),
buyer = buyer,
product_id = product_id,
product_name = product_name,
product_quantity = product_quantity,
buyer_name = buyer_name,
buyer_address = buyer_address,
mobile_no = mobile_no,
payment_option = payment_option,
status = status,
delivered=delivered
)
class Review(Model):
user = CharField()
order_id = CharField()
text = TextField()
comment_time = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
@classmethod
def add_review(cls, user, order_id, text):
try:
cls.create(
user = user,
order_id = order_id,
text = text
)
except IntegrityError :
raise ValueError("Some Error Happened")
class Banner(Model):
link = CharField()
class Meta:
database = DATABASE
@classmethod
def add_banner(cls, link):
try:
cls.create(
link = link,
)
except IntegrityError :
raise ValueError("Some Error Happened")
def initialize():
DATABASE.connect()
DATABASE.create_tables([User, Product, Cart, BuyHistory, Comment, Review, Banner], safe=True)
DATABASE.close()