-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_routes.py
138 lines (116 loc) · 5.51 KB
/
user_routes.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
from flask import Flask, render_template, request, redirect
from application import app
from vault import agent, User, Product, Category, update ,Cart, Order_Detail, Order_Items
import os,time
from authentication import *
@app.route('/')
@app.route('/home')
def home():
if 'user' in session:
user = agent.query(User).filter(User.id == session['user']).first()
categories = agent.query(Category).order_by(Category.id.desc()).all()
products = agent.query(Product).order_by(Product.id.desc()).all()
recent_products = agent.query(Product).order_by(Product.id.desc()).limit(5).all()
return render_template('user/index.html', user=user, categories=categories, products=products, recent_products=recent_products)
else:
return render_template('login/login.html')
@app.route('/category/')
def category():
if 'user' in session:
categories = agent.query(Category).all()
user = agent.query(User).filter(User.id == session['user']).first()
return render_template('user/category.html', categories=categories, user=user)
else:
return render_template('login/new.html')
@app.route('/profile/<int:pid>')
def profile(pid):
if 'user' not in session:
return render_template('login/login.html')
else:
user = agent.query(User).filter(User.id == pid).first()
return render_template('user/profile.html', user=user)
@app.route('/product/<int:pid>', methods=['POST', 'GET'])
def product(pid):
if 'user' in session:
if request.method == 'POST':
# Retrieve form data to create a new product
new_cart = Cart(
user_id=session['user'],
product_id=request.form['product_id'],
product_name=request.form['product_name'],
quantity=int(request.form['quantity']),
price=int(request.form['price'])
)
# Add the new product to the database
agent.add(new_cart)
agent.commit()
return redirect('/home')
else:
# Fetch the product with the given ID and display it
item = agent.query(Product).filter(Product.id == pid).first()
return render_template('user/buy.html', product=item)
else:
return render_template('login/login.html')
@app.route('/cart/<int:pid>')
def cart(pid):
if 'user' in session:
# Fetch the product with the given ID and display it
product = agent.query(Cart).filter(Cart.user_id == pid).all()
total = sum([product[i].price*product[i].quantity for i in range(len(product))])
return render_template('user/cart.html', cart_items=product, total=total, user_id=session['user'])
else:
return render_template('login/login.html')
@app.route('/cart/del/<int:cid>')
def car_delete(cid):
if request.method == 'GET':
# Delete the product with the given ID from the database
agent.query(Cart).filter(Cart.cart_id == cid).delete()
agent.commit()
return redirect('/cart/'+str(session['user']))
@app.route('/cart/place_order/<int:pid>', methods=['POST'])
def place_order(pid):
if request.method == 'POST':
new_order = Order_Detail(user_id=int(request.form['user_id']),total=int(request.form['total']))
agent.add(new_order)
agent.commit()
cart = agent.query(Cart).filter(Cart.user_id == pid).all()
for i in range(len(cart)):
new_order_item = Order_Items(
user_id=session['user'],
order_id=new_order.id,
product_id=cart[i].product_id,
quantity=cart[i].quantity,
amount=cart[i].price,
product_price=cart[i].price,
product_name=cart[i].product_name)
#update product table quantity
product = agent.query(Product).filter(Product.id == cart[i].product_id).first()
product.quantity = product.quantity - cart[i].quantity
agent.add(new_order_item)
agent.commit()
agent.query(Cart).filter(Cart.user_id == pid).delete()
agent.commit()
return redirect('/home')
@app.route('/orders/<int:pid>')
def my_orders(pid):
if 'user' not in session:
return render_template('login/login.html')
else:
orders = agent.query(Order_Detail).filter(Order_Detail.user_id == pid).all()
return render_template('user/orders.html', orders=orders)
@app.route('/ordered_products/<int:oid>')
def ordered_products(oid):
if 'user' not in session:
return render_template('login/login.html')
else:
products = agent.query(Order_Items).filter(Order_Items.order_id == oid).all()
order = agent.query(Order_Detail).filter(Order_Detail.id == oid).first()
return render_template('user/ordered_products.html', items=products, order=order, user_id=session['user'], cart_items=order)
@app.route('/search', methods=['POST', 'GET'])
def search():
if request.method == 'POST':
search_value = request.form['search']
user = agent.query(User).filter(User.id == session['user']).first()
categories = agent.query(Category).filter(Category.name.like('%'+search_value+'%')).all()
products = agent.query(Product).filter(Product.name.like('%'+search_value+'%')).all()
return render_template('user/search.html', user=user, categories=categories, products=products,sv=search_value)