-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
135 lines (93 loc) · 3.83 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
from flask import Flask, render_template, request, redirect, url_for, flash
from Forms import CreateUserForm, LoginForm, SignUpForm
import User
import main
app = Flask(__name__)
main.init()
# Main page
# Current is Login Page
@app.route('/')
def home():
return render_template('home.html')
# Called For testing
# HF
@app.route('/testing/<choice>')
def testing(choice):
#return 'Test successful, code is {}'.format(code)
return render_template('users.html')
# Called when user successful logged in
# HF
@app.route('/users/<username>/<int:choice>')
def users(choice, username):
print("TESTTTTT")
temp = main.db.return_keys("Users")
if temp != None and username in temp:
temp2 = main.db.get_storage("Users")
user_details = temp2[username]
else:
print("ERRORRRRRR")
return render_template('users.html', menu=choice, user=user_details)
#Called when admin login
@app.route('/admin')
def admin():
return render_template('admin.html')
# Called when sign up button is clicked from the login page
# HF
@app.route('/signup', methods=['POST', 'GET'])
def sign_up():
signup_form = SignUpForm(request.form)
if request.method == 'POST' and signup_form.validate():
user = User.User(signup_form.first_name.data, signup_form.last_name.data, signup_form.username.data.lower(),
signup_form.password.data, signup_form.postal_code.data, signup_form.address.data,
signup_form.country.data, signup_form.city.data, signup_form.unit_number.data)
# to create and check if the storage exist
main.db.get_storage('Users', True, True)
main.db.add_item('Users', user.get_username(), user)
# create temporary storage
main.db.get_storage("TEMP", True, True)
main.db.add_item('TEMP', "username", user.get_username())
return redirect(url_for('users', choice=1, username=user.get_username()))
return render_template('signUp.html', form=signup_form)
# Called when user press submit at main page
# Two methods, GET is called when website request the page
# There is POST request only when user click the submit button
# HF
@app.route('/loginMenu', methods=['POST', 'GET'])
def loginMenu():
login_form = LoginForm(request.form)
# login if user already logged in before
temp_exist = main.db.check_exist('TEMP')
if temp_exist == True:
session = main.db.get_storage('TEMP')
s_keys = session.keys()
if "username" in s_keys:
username = session['username']
return redirect(url_for('users', choice=1, username=username))
# When a button is clicked
if request.method == 'POST':
btn_pressed = request.form['submit']
# Login clicked
# Validate only on a POST request
if login_form.validate() and btn_pressed == "Login":
login_name = login_form.username.data.lower()
admin_acc = main.db.get_storage("ADMIN")
temp = main.db.return_keys("Users")
if admin_acc.get_username() == login_name:
print("Admin Login")
return redirect(url_for('admin'))
elif temp != None and login_name in temp:
temp2 = main.db.get_storage("Users")
user = temp2[login_name]
# create temporary storage
main.db.get_storage("TEMP", True, True)
main.db.add_item('TEMP', "username", user.get_username())
return redirect(url_for('users', choice=1, username=user.get_username()))
else:
print("ERRORRRRRR")
# Sign up clicked
elif btn_pressed == "Sign Up":
return redirect(url_for('sign_up'))
# Get request will be skipped to this
return render_template('userLogin.html', form=login_form)
if __name__ == '__main__':
app.run()