-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·220 lines (171 loc) · 5.75 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
#!/bin/python
"""
This is the main body of group 25's webapp.
"""
import json
import copy
import os
from datetime import timedelta
from passlib.hash import argon2
import flask
import flask_login
from flask import Flask, render_template, request
from flask_login import current_user
from wtforms import Form, StringField, SelectField
APP = Flask(__name__)
LOGIN_MANAGER = flask_login.LoginManager()
LOGIN_MANAGER.init_app(APP)
APP.secret_key = os.urandom(12)
ACCOUNTS = json.load(open('users.json'))
INVOICES = json.load(open('invoice.json'))
# General TODOs
# TODO: Create fake data for the second req we did for sprint 1
# TODO: Requirements 3 & 4
class CustomerSearchForm(Form):
search = StringField('')
class User(flask_login.UserMixin):
"""
TODO: Write docstring
"""
pass
@LOGIN_MANAGER.user_loader
def user_loader(user_id):
"""
TODO: Write docstring
"""
if user_id not in ACCOUNTS:
return
user = User()
user.id = user_id
return user
@LOGIN_MANAGER.request_loader
def request_loader(request):
"""
TODO: Write docstring
"""
email = request.form.get('email')
if email not in ACCOUNTS:
return
user = User()
user.id = email
user.is_authenticated = argon2.verify(request.form['password'], ACCOUNTS[email]['password'])
return user
# TODO: Handle different user types correctly
@APP.route("/")
def home():
"""
TODO: Write docstring
"""
if current_user.is_authenticated is False:
# session['anonymous_user_id'] = user.id
return render_template('login.html')
role = ACCOUNTS[flask_login.current_user.id]["role"]
if not role:
return '<a href="/logout">Server Error</a>'
return render_template('users.html', user=str(role))
# TODO: Handle the correct user types
# TODO: Update users.json to use the correct user types instead of placeholders.
@APP.route('/login', methods=['GET', 'POST'])
def login():
"""
TODO: Write docstring
"""
if flask.request.method == 'GET':
return render_template('login.html')
email = flask.request.form['username']
password = flask.request.form['password']
if email not in ACCOUNTS:
return render_template('login.html', error='Invalid credentials')
if argon2.verify(password, ACCOUNTS[email]['password']):
user = User()
user.id = email
flask_login.login_user(user, remember=False, duration=timedelta(seconds=5))
return flask.redirect(flask.url_for('protected'))
return render_template('login.html', error='Invalid credentials')
@APP.route('/protected')
@flask_login.login_required
def protected():
"""
TODO: Write docstring
"""
if current_user.is_authenticated is False:
return flask.redirect(flask.url_for('login'))
role = ACCOUNTS[flask_login.current_user.id]["role"]
if not role:
return '<a href="/logout">Server Error</a>'
return render_template('users.html', user=str(role))
@flask_login.login_required
@APP.route('/account')
def account():
"""
TODO: Write docstring
"""
if current_user.is_authenticated is False:
return flask.redirect(flask.url_for('login'))
role = ACCOUNTS[flask_login.current_user.id]["role"]
if not role:
return '<a href="/logout">Server Error</a>'
return render_template('users.html', user=str(role))
@flask_login.login_required
@APP.route('/invoice', methods=['GET', 'POST'])
def invoice():
"""
TODO: Write docstring
"""
if current_user.is_authenticated is False:
return flask.redirect(flask.url_for('login'))
role = ACCOUNTS[flask_login.current_user.id]["role"]
if not role:
return 'Blah <a href="/logout">Server Error</a>'
query = CustomerSearchForm(request.form)
if request.method == 'POST':
return search_customers(query.data['search'])
return render_template('search.html', user=str(role), form=query)
def search_customers(query):
role = ACCOUNTS[flask_login.current_user.id]["role"]
results = []
customer_id = []
if not query:
return render_template('invoice.html', user=str(role), labels=INVOICES, customers=ACCOUNTS)
for user in ACCOUNTS:
if query.upper() is ACCOUNTS[user]["name"].upper() and ACCOUNTS[user]["role"] is "Customer":
customer_id.append(ACCOUNTS[user]["customer_id"])
get_invoices("[email protected]")
return
#return render_template('invoice.html', user=str(role), labels=INVOICES)
def get_invoices(customer_id):
INVOICES_REFINED = {}
for invoice in INVOICES:
if INVOICES[invoice]["customer_id"] == customer_id:
INVOICES_REFINED[invoice] = INVOICES[invoice]
print(INVOICES)
print(INVOICES_REFINED)
"""["customer_id"] == customer_id:
INVOICES_REFINED.append([INVOICES[invoice]["customer_id"],
INVOICES[invoice]["id"],
INVOICES[invoice]["check_number"],
INVOICES[invoice]["order_id"],
INVOICES[invoice]["order_date"],
INVOICES[invoice]["product_desc"],
INVOICES[invoice]["product_cost"]])
for invoice in INVOICES_REFINED:
"""
#print(invoice[0])
return
#return render_template('invoice.html', labels=INVOICES_REFINED)
@APP.route('/logout')
def logout():
"""
TODO: Write docstring
"""
flask_login.logout_user()
return render_template('login.html', error='Logged out')
@LOGIN_MANAGER.unauthorized_handler
def unauthorized_handler():
"""
TODO: Write docstring
"""
return flask.redirect(flask.url_for('login'))
if __name__ == '__main__':
APP.secret_key = os.urandom(12)
APP.run(debug=True, host='127.0.0.1', port=5000)