/
api.py
116 lines (71 loc) · 2.66 KB
/
api.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
from flask import Flask,render_template,request,redirect,url_for,session
from model import check_user,add_user_Todb,check_product,addproduct_todb
app = Flask(__name__)
app.secret_key = 'hello'
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html',title = 'home')
@app.route('/about')
def about():
return render_template('about.html',title='about')
@app.route('/contact')
def contact():
return render_template('contact.html',title='contact')
@app.route('/signup',methods=['GET','POST'])
def signup():
if request.method == 'POST':
userinfo = {}
userinfo['username'] = request.form['username']
userinfo['fullname'] = request.form['fullname']
userinfo['password'] = request.form['password1']
password2 = request.form['password2']
userinfo['email'] = request.form['email']
userinfo['type'] = request.form['type']
if userinfo['password'] != password2:
return "Password fields dint mantch"
if bool(check_user(userinfo['username'])) is True:
return "user already exists.try logging in"
add_user_Todb(userinfo)
return redirect(url_for('home'))
return redirect(url_for('home'))
@app.route('/login',methods = ['GET','POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if bool(check_user(username)) and (check_user(username)['password']==password):
# if bool(check_user(username)) and (check_user(username)['password']==password):
session['username'] = username
session['type'] = check_user(username)['type']
return redirect(url_for('home'))
return "username or password is in correct"
return redirect(url_for('home'))
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
# @app.route('/login',methods =['GET','POST'])
# def login():
# if request.method =='POST':
# db = {'newuser' :'12345','Testuser':'12345'}
# username = request.form['username']
# password = request.form['password']
# if username in db and db[username] == password:
# return redirect(url_for('about'))
# return "username and password is incorrect"
@app.route('/products',methods=['GET','POST'])
def products():
if request.method == 'POST':
productinfo = {}
productinfo['pname'] = request.form['pname']
productinfo['price'] = request.form['price']
productinfo['description'] = request.form['description']
productinfo['seller'] = session['username']
if bool(check_product(productinfo['pname'])) is True:
return "Product already in cart"
addproduct_todb(productinfo)
return redirect(url_for('products'))
return redirect(url_for('products'))
if __name__ == '__main__':
app.run(debug = True)