-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
381 lines (325 loc) · 14.6 KB
/
app.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from flask import *
from sqlalchemy import *
from flask_hashing import Hashing
from flask_uploads import UploadSet, configure_uploads, IMAGES
from datetime import datetime
from model import *
import os
app = Flask(__name__)
hashing = Hashing(app)
images = UploadSet('images', IMAGES)
app.config['UPLOADED_IMAGES_DEST'] = 'static/img'
configure_uploads(app, images)
@app.route('/')
@app.route('/<username>')
def index(username=None):
products = sqlsession.query(Product).all()
return render_template("index.html", products=products, size=len(products))
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
password = str(request.form['password'])
username = str(request.form['username'])
user = sqlsession.query(User).filter_by(username=username).first()
if username == '' or hashing.check_value(password, '', salt='abcd'):
return render_template("register.html", error1="Fields can't be empty", error2=None)
if user is None:
return render_template("register.html", error1="Username does not exist!", error2=None)
elif hashing.check_value(user.password, password, salt='abcd'):
sqlsession.add(user)
sqlsession.commit()
session['user'] = user.username
return redirect(url_for('index', name=user.username))
elif not hashing.check_value(user.password, password, salt='abcd'):
return render_template("register.html", error1="Wrong Password!", error2=None)
return render_template('register.html', error1=None, error2=None)
@app.route('/logout')
def logout():
session.pop('user')
return redirect(url_for('index'))
@app.route('/signup', methods=['POST', 'GET'])
def signup():
if request.method == 'POST':
error = None
user = User()
user.username = str(request.form['username'])
user.email = str(request.form['email'])
user.name = str(request.form['name'])
user.mobile = str(request.form['tel'])
password = str(request.form['password'])
confirm = str(request.form['confirmation'])
user.isFormFilled = 0
if user.username == '' or user.email == '' or user.name == '' or user.mobile == '' or password == '' or confirm == '':
error = "Fields can't be empty"
elif set('[~!@#$%^&*()-_+={}":;\']+$\\/.,<>').intersection(password):
error = "Password can't contain special characters"
elif set('[~!@#$%^&*()-_+={}":;\']+$\\/.,<>').intersection(user.username):
error = "Username can't contain special characters"
elif set('[~!@#$%^&*()-_+={}":;\']+$\\/.,<>').intersection(user.name):
error = "Name can't contain special characters"
elif set('[~!@#$%^&*()-_+={}":;\']+$\\/.,<>').intersection(user.mobile):
error = "Mobile can't contain special characters"
elif len(password) < 8:
error = "Password should be more than 8 characters"
elif confirm != password:
error = "Passwords do not match"
elif sqlsession.query(User).filter_by(username=user.username).first() is not None:
error = "Username already exists"
elif sqlsession.query(User).filter_by(email=user.email).first() is not None:
error = "Email already exists"
elif sqlsession.query(User).filter_by(mobile=user.mobile).first() is not None:
error = "Mobile number already exists"
if error:
return render_template('register.html', error1=None, error2=error)
user.password = hashing.hash_value(password, salt='abcd')
sqlsession.add(user)
sqlsession.commit()
session['user'] = user.username
return redirect(url_for('index', username=user.username))
return render_template('register.html', error1=None, error2=None)
@app.route('/sell', methods=['GET', 'POST'])
def sell():
if request.method == 'POST':
error = None
seller = Seller()
user = sqlsession.query(User).filter_by(
username=session['user']).first()
seller.id = user.id
seller.businessname = str(request.form['businessname'])
seller.shopaddress = str(request.form['shopaddress'])
if seller.shopaddress == '' or seller.businessname == '':
error = "Fields can't be empty"
elif sqlsession.query(Seller).filter_by(businessname=seller.businessname).first() is not None:
error = "Businessname already exists"
if error:
return render_template('sell.html', error=error)
user.isFormFilled = 1
sqlsession.add(seller)
sqlsession.commit()
return redirect(url_for('addProduct'))
flag = sqlsession.query(User).filter_by(
username=session['user']).first().isFormFilled
if flag == 1:
return redirect(url_for('addProduct'))
else:
return render_template('sell.html', error=None)
@app.route('/addProduct', methods=['GET', 'POST'])
def addProduct():
if request.method == 'POST':
error = None
product = Product()
product.name = str(request.form['itemName'])
product.price = int(request.form['itemPrice'])
product.description = str(request.form['itemDescription'])
product.quantity = int(request.form['quantity'])
categoryName = str(request.form['categoryName'])
user = sqlsession.query(User).filter_by(
username=session['user']).first()
seller = sqlsession.query(Seller).filter_by(id=user.id).first()
try:
product.imageName = images.save(request.files['image'])
except:
error = "Upload image first"
if product.name == '' or product.description == '' or categoryName == '':
error = "Fields can't be empty"
elif product.quantity == 0:
error = "Add atleast one item"
elif product.quantity < 0 or product.price < 0:
error = "Negative figures are not allowed"
if error:
return render_template('addProduct.html', error=error, categories=sqlsession.query(Category).all())
category = sqlsession.query(Category).filter_by(
name=categoryName).first()
product.category = category
product.seller = seller
sqlsession.add(product)
sqlsession.commit()
return redirect(url_for('index', username=session['user']))
return render_template('addProduct.html', error=None, categories=sqlsession.query(Category).all())
@app.route('/product_detail', methods=['POST', 'GET'])
@app.route('/product_detail/<productid>', methods=['POST', 'GET'])
def product_detail(productid=None):
if request.method == 'POST':
error = None
product = sqlsession.query(Product).filter_by(id=productid).first()
similarProducts = sqlsession.query(Product).filter_by(
category=product.category).all()
size = len(similarProducts)
stock = product.quantity
quantity = int(request.form['product_quantity'])
if "user" not in session:
error = "Please login first"
elif stock == 0:
error = "Product is out of stock"
elif stock < quantity:
error = "Only " + str(stock) + " pieces are left"
elif quantity <= 0:
error = "figures can't be negative or zero"
if error:
return render_template('product_detail.html', product=product, similarProducts=similarProducts, size=size, error=error)
user = sqlsession.query(User).filter_by(
username=session['user']).first()
order = sqlsession.query(Order).filter_by(
user_id=user.id, isOrdered=0, product_id=product.id).first()
if order == None:
order = Order()
order.user = user
order.product = product
order.product_quantity = quantity
order.isOrdered = 0
sqlsession.add(order)
else:
order.product_quantity += quantity
sqlsession.commit()
return render_template('product_detail.html', product=product, similarProducts=similarProducts, size=size, error=None)
product = sqlsession.query(Product).filter_by(id=productid).first()
similarProducts = sqlsession.query(Product).filter_by(
category=product.category).all()
size = len(similarProducts)
return render_template('product_detail.html', product=product, similarProducts=similarProducts, size=size, error=None)
@app.route('/category')
@app.route('/category/<category_id>')
@app.route('/category/<category_id>/<page_number>')
def category_filter(category_id=None, page_number=None):
products = sqlsession.query(Product).filter_by(
category_id=category_id).all()
if page_number == None:
page_number = 1
startIndex = 0
else:
page_number = int(page_number)
startIndex = (page_number-1)*9-1
url="/category/"+str(category_id)
return render_template('products.html', products=products, startIndex=startIndex, size=len(products), page_number=page_number,url=url)
@app.route('/price', methods=['POST', 'GET'])
def filter_by_price(start_price=None, end_price=None, page_number=None):
if request.method == 'POST':
productIds = request.form.getlist("productIds")
start_price = request.form["start_price"]
end_price = request.form["end_price"]
products = []
for productId in productIds:
product = sqlsession.query(Product).filter_by(id=productId).first()
if int(product.price) >= int(start_price) and int(product.price) <= int(end_price):
products.append(product)
if page_number == None:
page_number = 1
startIndex = 0
else:
startIndex = (page_number-1)*9 - 1
url="/price"
return render_template('products.html', products=products, startIndex=startIndex, size=len(products), page_number=page_number,url=url)
@app.route('/search', methods=['POST', 'GET'])
@app.route('/search/<page_number>', methods=['POST', 'GET'])
def search(page_number=None):
text = request.form['search']
products = sqlsession.query(Product).filter(
Product.name.like('%'+text+'%')).all()
if page_number == None:
page_number = 1
startIndex = 0
else:
startIndex = (page_number-1)*9-1
url="/search"
return render_template('products.html', products=products, startIndex=startIndex, size=len(products), page_number=page_number,url=url)
@app.route('/cart', methods=['POST', 'GET'])
@app.route('/cart/<username>', methods=['POST', 'GET'])
def cart(username=None):
user = sqlsession.query(User).filter_by(username=session['user']).first()
cartDetails = sqlsession.query(Order).filter_by(
user_id=user.id, isOrdered=0).all()
if request.method == 'POST':
ids = request.form.getlist("to_delete")
for itemId in ids:
toDelete = sqlsession.query(
Order).filter_by(id=int(itemId)).first()
sqlsession.delete(toDelete)
sqlsession.commit()
cartDetails = sqlsession.query(Order).filter_by(
user_id=user.id, isOrdered=0).all()
totalPrice = 0
errorlist = []
error = None
for item in cartDetails:
totalPrice = totalPrice + \
(int(item.product.price)*(int)(item.product_quantity))
product = sqlsession.query(Product).filter_by(
id=item.product_id).first()
if product.quantity-item.product_quantity < 0:
errorlist.append(product)
error = "The Following Products : "
for item in errorlist:
error += item.name + "; "
if error:
error += "are not available. Please reduce quantity and try again!"
if len(cartDetails) == 0:
error = "Cart is Empty!!! Add Products to Checkout"
return render_template('cart.html', cartDetails=cartDetails, totalPrice=totalPrice, error=error)
@app.route('/checkout', methods=['POST', 'GET'])
@app.route('/checkout/<username>', methods=['POST', 'GET'])
def checkout(username=None):
user = sqlsession.query(User).filter_by(username=session['user']).first()
cartDetails = sqlsession.query(Order).filter_by(
user_id=user.id, isOrdered=0).all()
totalPrice = 0
error = None
for item in cartDetails:
totalPrice = totalPrice + \
(int(item.product.price)*(int)(item.product_quantity))
if request.method == 'POST':
date = datetime.now()
address1 = request.form['address1']
address2 = request.form['address2']
city = request.form['city']
postcode = request.form['postcode']
country = request.form['country']
state = request.form['state']
if address1 == '' or city == '' or postcode == '' or country == '' or state == '':
error = "Fields can't be empty"
if error:
return render_template('checkout.html', user=user, cartDetails=cartDetails, totalPrice=totalPrice, error=error)
for item in cartDetails:
item.date = date
item.isOrdered = 1
product = sqlsession.query(Product).filter_by(
id=item.product_id).first()
product.quantity -= item.product_quantity
log = Logs()
log.date = date
log.seller = product.seller
log.user = user
log.product = product
log.product_quantity = item.product_quantity
log.address1 = address1
log.address2 = address2
log.city = city
log.postcode = postcode
log.country = country
log.state = state
sqlsession.add(log)
sqlsession.commit()
return render_template('placed.html', totalPrice=(totalPrice*1.17))
return render_template('checkout.html', user=user, cartDetails=cartDetails, totalPrice=totalPrice, error=None)
@app.route('/usertable')
def usertable():
user = sqlsession.query(User).all()
return render_template('table.html', rows=user, message="USERS ::")
@app.route('/sellertable')
def sellertable():
sellers = sqlsession.query(Seller).all()
return render_template('sellertable.html', rows=sellers, message="SELLERS ::")
@app.route('/productTable')
def productTable():
products = sqlsession.query(Product).all()
return render_template('productTable.html', rows=products, message="PRODUCTS ::")
@app.route('/orderTable')
def orderTable():
orders = sqlsession.query(Order).all()
return render_template('orderTable.html', rows=orders, message="ORDERS ::")
@app.route('/logs')
def logs():
logs = sqlsession.query(Logs).all()
return render_template('log.html', rows=logs, message="LOGS ::")
if __name__ == '__main__':
app.secret_key = os.urandom(12)
app.run(debug=True)