-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
370 lines (323 loc) · 12.1 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from flask import Flask, render_template, jsonify, request, redirect, url_for, session, abort, send_from_directory, logging
from werkzeug.utils import secure_filename
import sqlite3
import os
import addUser
import json
#This app was generated using the openCSM framework for python
#You can find it here https://github.com/TCA166/openCMS
app = Flask(__name__)
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app
app.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24)
def getConn():
"""Returns the connected database"""
return sqlite3.connect(r'main.db')
def isAuthorised(level:int=0):
"""Returns True or False depending on if the frontend (session) is authorised"""
#Authorisation is binary here. Logging in simply sets an encrypted cookie with True in it.
try:
if session['authorised'] == False:
return False
else:
if level <= session['level']:
return True
else:
return False
except KeyError: #there isn't even a key here - the user is unauthorised
return False
@app.errorhandler(404)
def notFound(e):
return render_template('404.html'), 404
@app.errorhandler(401)
def authFailed(e):
return render_template('401.html'), 401
@app.errorhandler(500)
def serverError(e):
return render_template('500.html'), 500
@app.route('/login', methods=['POST'])
def login():
conn = getConn()
cur = conn.cursor()
login = request.form['login']
password = request.form['pass']
cur.execute('SELECT salt FROM users WHERE login=?', (login,))
try:
salt = cur.fetchall()[0][0]
except IndexError:
return redirect(url_for('home'))
key = addUser.hash(password, salt)
cur.execute('SELECT EXISTS(SELECT * FROM users WHERE login=? AND pass=?)', (login, key))
exists = cur.fetchall()[0][0]
if exists == 1:
session['authorised'] = True
cur.execute('SELECT auth FROM users WHERE login=? AND pass=?', (login, key))
auth = cur.fetchall()[0][0]
session['level'] = auth
return redirect(url_for('home'))
@app.route('/logout', methods=['GET'])
def logout():
session['authorised'] = False
session['level'] = None
return redirect(url_for('home'))
@app.route('/', methods=['GET'])
@app.route('/Home', methods=['GET'])
def home():
if isAuthorised() == False:
return render_template('auth.html', encoding='utf-8')
if isAuthorised(0) == False:
abort(401)
return render_template('Home.html', encoding='utf-8')
@app.route('/Clients', methods=['GET'])
def Clients():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "Client"')
ClientRows = cur.fetchall()
return render_template('Clients.html', ClientRows=ClientRows, encoding='utf-8')
@app.route('/new/Client', methods=['GET'])
def Client():
if isAuthorised(0) == False:
abort(401)
return render_template('Client.html', encoding='utf-8')
@app.route('/new/Client/submit', methods=['POST'])
def ClientSubmit():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
data = dict(request.form)
rowRowid = data['Clientrowid-0']
if rowRowid == '':
sql = 'INSERT INTO "Client" VALUES (?, ?, ?, ?)'
cur.execute(sql, (data["ClientName-0"], data["ClientSurname-0"], data["ClientAge-0"], data["Clientuid-0"],))
ClientlastRowid = cur.lastrowid
else:
sql = 'UPDATE "Client" SET Name=?, Surname=?, Age=?, uid=? WHERE rowid=?'
values = [data["ClientName-0"], data["ClientSurname-0"], data["ClientAge-0"], data["Clientuid-0"],]
values.append(rowRowid)
cur.execute(sql, values)
ClientlastRowid = rowRowid
conn.commit()
return redirect('/')
@app.route('/edit/Client/<rowid>', methods=['GET'])
def ClientEdit(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "Client" WHERE rowid=?', (rowid, ))
dataClient = cur.fetchall()[0]
return render_template('Client.html', Clientrowid=rowid, dataClient=dataClient, encoding='utf-8')
@app.route('/copy/Client/<rowid>', methods=['POST'])
def Clientcopy(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute("PRAGMA foreign_keys = OFF;")
cur.execute("INSERT INTO Client SELECT * FROM Client WHERE rowid=?", rowid)
cur.execute("PRAGMA foreign_keys = ON;")
conn.commit()
return redirect('/')
@app.route('/Products', methods=['GET'])
def Products():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "Product"')
ProductRows = cur.fetchall()
return render_template('Products.html', ProductRows=ProductRows, encoding='utf-8')
@app.route('/new/Product/<rowidClient>', methods=['GET'])
def Product(rowidClient):
if isAuthorised(0) == False:
abort(401)
return render_template('Product.html', rowidClient=rowidClient, encoding='utf-8')
@app.route('/new/Product/submit', methods=['POST'])
def ProductSubmit():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
data = dict(request.form)
rowRowid = data['Productrowid-0']
if rowRowid == '':
sql = 'INSERT INTO "Product" VALUES (?, ?, ?)'
cur.execute(sql, (data["ProductrowidClient-0"], data["ProductProduct name-0"], data["ProductPrice-0"],))
ProductlastRowid = cur.lastrowid
else:
sql = 'UPDATE "Product" SET Product name=?, Price=? WHERE rowid=?'
values = [data["ProductrowidClient-0"], data["ProductProduct name-0"], data["ProductPrice-0"],]
values.append(rowRowid)
cur.execute(sql, values)
ProductlastRowid = rowRowid
conn.commit()
return redirect('/')
@app.route('/edit/Product/<rowid>', methods=['GET'])
def ProductEdit(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "Product" WHERE rowid=?', (rowid, ))
dataProduct = cur.fetchall()[0]
return render_template('Product.html', Productrowid=rowid, dataProduct=dataProduct, encoding='utf-8')
@app.route('/copy/Product/<rowid>', methods=['POST'])
def Productcopy(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute("PRAGMA foreign_keys = OFF;")
cur.execute("INSERT INTO Product SELECT * FROM Product WHERE rowid=?", rowid)
cur.execute("PRAGMA foreign_keys = ON;")
conn.commit()
return redirect('/')
@app.route('/secretPage', methods=['GET'])
def secretPage():
if isAuthorised(1) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "superSecret"')
superSecretRows = cur.fetchall()
return render_template('secretPage.html', superSecretRows=superSecretRows, encoding='utf-8')
@app.route('/new/superSecret', methods=['GET'])
def superSecret():
if isAuthorised(1) == False:
abort(401)
return render_template('superSecret.html', encoding='utf-8')
@app.route('/new/superSecret/submit', methods=['POST'])
def superSecretSubmit():
if isAuthorised(1) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
data = dict(request.form)
rowRowid = data['superSecretrowid-0']
if rowRowid == '':
sql = 'INSERT INTO "superSecret" VALUES (?)'
cur.execute(sql, (data["superSecretSecret-0"],))
superSecretlastRowid = cur.lastrowid
else:
sql = 'UPDATE "superSecret" SET Secret=? WHERE rowid=?'
values = [data["superSecretSecret-0"],]
values.append(rowRowid)
cur.execute(sql, values)
superSecretlastRowid = rowRowid
conn.commit()
return redirect('/')
@app.route('/edit/superSecret/<rowid>', methods=['GET'])
def superSecretEdit(rowid):
if isAuthorised(1) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "superSecret" WHERE rowid=?', (rowid, ))
datasuperSecret = cur.fetchall()[0]
return render_template('superSecret.html', superSecretrowid=rowid, datasuperSecret=datasuperSecret, encoding='utf-8')
@app.route('/copy/superSecret/<rowid>', methods=['POST'])
def superSecretcopy(rowid):
if isAuthorised(1) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute("PRAGMA foreign_keys = OFF;")
cur.execute("INSERT INTO superSecret SELECT * FROM superSecret WHERE rowid=?", rowid)
cur.execute("PRAGMA foreign_keys = ON;")
conn.commit()
return redirect('/')
@app.route('/Orders', methods=['GET'])
def Orders():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "order"')
orderRows = cur.fetchall()
cur.execute('SELECT rowid, * FROM "Products"')
ProductsRows = cur.fetchall()
return render_template('Orders.html', ProductsRows=ProductsRows, orderRows=orderRows, encoding='utf-8')
@app.route('/new/order', methods=['GET'])
def order():
if isAuthorised(0) == False:
abort(401)
return render_template('order.html', encoding='utf-8')
@app.route('/new/order/submit', methods=['POST'])
def orderSubmit():
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
data = dict(request.form)
rowRowid = data['orderrowid-0']
if rowRowid == '':
sql = 'INSERT INTO "order" VALUES (?)'
cur.execute(sql, (data["ordername-0"],))
orderlastRowid = cur.lastrowid
else:
sql = 'UPDATE "order" SET name=? WHERE rowid=?'
values = [data["ordername-0"],]
values.append(rowRowid)
cur.execute(sql, values)
orderlastRowid = rowRowid
i = 0
while 'Productsrowid' + '-' + str(i) in data.keys():
fields = ('rowidorder', 'name', 'count')
values = [orderlastRowid if f=='rowidorder' else data["Products" + f + '-' + str(i)] for f in fields]
rowRowid = data['Productsrowid-' + str(i)]
if rowRowid == '':
sql = 'INSERT INTO "Products" VALUES (?, ?, ?)'
cur.execute(sql, values)
ProductslastRowid = cur.lastrowid
else:
sql = 'UPDATE "Products" SET name=?, count=? WHERE rowid=?'
values.pop(0)
values.append(rowRowid)
cur.execute(sql, values)
ProductslastRowid = rowRowid
i += 1
conn.commit()
return redirect('/')
@app.route('/edit/order/<rowid>', methods=['GET'])
def orderEdit(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute('SELECT rowid, * FROM "order" WHERE rowid=?', (rowid, ))
dataorder = cur.fetchall()[0]
cur.execute('SELECT rowid, * FROM "Products" WHERE rowidorder=?', (dataorder[0], ))
dataProducts = cur.fetchall()
return render_template('order.html', orderrowid=rowid, dataorder=dataorder, dataProducts=dataProducts, encoding='utf-8')
@app.route('/copy/order/<rowid>', methods=['POST'])
def ordercopy(rowid):
if isAuthorised(0) == False:
abort(401)
conn = getConn()
cur = conn.cursor()
cur.execute("PRAGMA foreign_keys = OFF;")
cur.execute("INSERT INTO order SELECT * FROM order WHERE rowid=?", rowid)
cur.execute("PRAGMA foreign_keys = ON;")
conn.commit()
return redirect('/')
@app.route('/jsonDisplay', methods=['GET'])
def jsonDisplay():
if isAuthorised(0) == False:
abort(401)
with open('cards.json', 'r') as f:
data = json.load(f)
return render_template('jsonDisplay.html', json=data, encoding='utf-8')
if __name__ == '__main__':
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
debug = False
app.run(HOST, PORT, debug=debug)