-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitercaller.py
128 lines (114 loc) · 4.74 KB
/
waitercaller.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
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from flask_login import LoginManager
from flask_login import login_required
from flask_login import login_user
from flask_login import current_user
from flask_login import logout_user
from forms import RegistrationForm
from forms import LoginForm
from forms import CreateTableForm
from forms import DeleteTableForm
from passwordhelper import PasswordHelper
from bitlyhelper import BitlyHelper
from user import User
import config
if config.test:
from mockdbhelper import MockDBHelper as DBHelper
else:
from dbhelper import DBHelper
import datetime
app = Flask(__name__)
app.secret_key = 'Z8e3QFDjQOur3NuT+UXeqp0JafZ0ifdIW2GozI9aE51KddkU67XbfOEDiTqb42TDCqv6Yp+19nvBJYruFPWjY1VEkFaQduSMQ7v'
login_manager = LoginManager(app)
DB = DBHelper()
PH = PasswordHelper()
BH = BitlyHelper()
@app.route("/")
def home():
return render_template("home.html", loginform=LoginForm(), registrationform=RegistrationForm())
@app.route("/register", methods=["POST"])
def register():
form = RegistrationForm(request.form)
if form.validate():
if DB.get_user(form.email.data):
form.email.errors.append("Email address already registered")
return render_template('home.html', loginform=LoginForm(), registrationform=form)
salt = PH.get_salt()
hashed = PH.get_hash(form.password2.data + salt)
DB.add_user(form.email.data, salt, hashed)
return render_template("home.html", loginform=LoginForm(), registrationform=form, onloadmessage="Registration successful. Please log in.")
return render_template("home.html", loginform=LoginForm(), registrationform=form)
@app.route("/login", methods=["POST"])
def login():
form = LoginForm(request.form)
if form.validate():
stored_user = DB.get_user(form.loginemail.data)
if stored_user and PH.validate_password(form.loginpassword.data,
stored_user['salt'], stored_user['hashed']):
user = User(form.loginemail.data)
login_user(user, remember=True)
return redirect(url_for('account'))
form.loginemail.errors.append("Email or password invalid")
return render_template("home.html", loginform=form,
registrationform=RegistrationForm())
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("home"))
@login_manager.user_loader
def load_user(user_id):
user_password = DB.get_user(user_id)
if user_password:
return User(user_id)
@app.route("/dashboard")
@login_required
def dashboard():
now = datetime.datetime.now()
requests = DB.get_requests(current_user.get_id())
for req in requests:
deltaseconds = (now - req['time']).seconds
req['wait_minutes'] = "{}.{}".format((deltaseconds/60), str(deltaseconds % 60).zfill(2))
return render_template("dashboard.html", requests=requests)
@app.route("/dashboard/resolve")
@login_required
def dashboard_resolve():
request_id = request.args.get("request_id")
DB.delete_request(request_id)
return redirect(url_for('dashboard'))
@app.route("/account")
@login_required
def account():
tables = DB.get_tables(current_user.get_id())
return render_template("account.html", createtableform=CreateTableForm(), deletetableform=DeleteTableForm(), tables=tables)
@app.route("/account/createtable", methods=["POST"])
@login_required
def account_createtable():
form = CreateTableForm(request.form)
if form.validate():
tableid = DB.add_table(form.tablenumber.data, current_user.get_id())
new_url = BH.shorten_url(config.base_url + "newrequest/" + str(tableid))
DB.update_table(tableid, new_url)
return redirect(url_for('account'))
return render_template("account.html", createtableform=form, deletetableform= DeleteTableForm(), tables=DB.get_tables(current_user.get_id()))
@app.route("/account/deletetable", methods=["POST"])
@login_required
def account_deletetable():
#tableid = request.args.get("tableid")
#DB.delete_table(tableid)
#return redirect(url_for('account'))
form = DeleteTableForm(request.form)
if form.validate():
DB.delete_table(form.tablenumber.data)
return redirect(url_for('account'))
return render_template("account.html", deletetableform=form, createtableform=CreateTableForm(), tables=DB.get_tables(current_user.get_id()))
@app.route("/newrequest/<tid>")
def new_request(tid):
if DB.add_request(tid, datetime.datetime.now()):
return "Your request has been logged and a waiter will be with you shortly"
return "There is already a request pending for this table. Please be patien, a waiter will be there ASAP."
if __name__ == '__main__':
app.run(port=5000, debug=True)