-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
405 lines (341 loc) · 15.6 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/python3
from flask import Flask
from flask import render_template, request
from os import environ
# PostgreSQL libraries
import psycopg2
import psycopg2.extras
app = Flask(__name__, static_url_path='/static')
# SGBD configs
DB_HOST = environ["DB_HOST"]
DB_USER = environ["DB_USER"]
DB_DATABASE = DB_USER
DB_PASSWORD = environ["DB_PASSWORD"]
DB_CONNECTION_STRING = "host=%s dbname=%s user=%s password=%s" % (DB_HOST, DB_DATABASE, DB_USER, DB_PASSWORD)
FILENAME = ""
# ==============================================================================
@app.route("/")
def index():
return render_template("index.html", FILENAME = FILENAME)
# =============== Inserir e remover categorias e subcategorias =================
# Category Table
@app.route("/category_table")
def category_table():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute("SELECT name FROM super_category ORDER BY name;")
super_c_rows = cursor.fetchall()
cursor.execute("SELECT name FROM simple_category ORDER BY name;")
simple_c_rows = cursor.fetchall()
return render_template("CategoryTable.html", simple_c_rows = simple_c_rows, super_c_rows = super_c_rows, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.close()
# Category Independent
@app.route("/category_independent")
def category_independent():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute("SELECT name FROM category;")
return render_template("CategoryIndependent.html", cursor = cursor, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.close()
# Category Add to Super
@app.route("/category_add_to_super", methods = ["POST"])
def category_add_to_super():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
simple_c_name = request.form["simple_c_name"]
super_c_name = request.form["super_c_name"]
# Check if the Simple Category is on the DB
cursor.execute("SELECT DISTINCT name FROM category WHERE name=%s", (simple_c_name,))
if cursor.rowcount == 0:
cursor.execute("INSERT INTO category VALUES(%s)", (simple_c_name,))
cursor.execute("INSERT INTO simple_category VALUES(%s)", (simple_c_name,))
# If the Super Category is in fact a Simple one, we must transfer it
cursor.execute("SELECT DISTINCT name FROM super_category WHERE name = %s", (super_c_name,))
if cursor.rowcount == 0:
cursor.execute("DELETE FROM simple_category WHERE name = %s", (super_c_name,))
cursor.execute("INSERT INTO super_category VALUES(%s)", (super_c_name,))
# Add the relation between the categories to the DB
cursor.execute("INSERT INTO has_other VALUES(%s, %s)", (simple_c_name, super_c_name))
return render_template("CategoryTableAddSuccess.html", simple_c_name = simple_c_name, super_c_name = super_c_name, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
dbConn.commit()
cursor.close()
dbConn.close()
# Category Add
@app.route("/category_add", methods = ["POST"])
def category_add():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
c_name = request.form["c_name"]
cursor.execute("INSERT INTO category VALUES(%s)", (c_name,))
# Restriction 1: must add to simple_category
cursor.execute("INSERT INTO simple_category VALUES(%s)", (c_name,))
return render_template("CategoryIndependentAddSuccess.html", c_name = c_name, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.commit()
dbConn.close()
# Category Remove
@app.route("/category_remove")
def category_remove():
dbConn = None
cursor = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
c_name = request.args["c_name"]
# Delete the category from everywhere it is, no dependencies must be broken
cursor.execute("DELETE FROM has_other WHERE category = %s", (c_name,))
cursor.execute("DELETE FROM has_other WHERE super_category = %s", (c_name,))
cursor.execute("DELETE FROM has_category WHERE name = %s", (c_name,))
# Get every product that belongs to the category
cursor.execute("SELECT ean FROM product WHERE cat = %s", (c_name,))
eans = cursor.fetchall()
for ean in eans:
cursor.execute("DELETE FROM has_category WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM replenishment_event WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM planogram WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM product WHERE cat = %s", (c_name,))
# Get every shelf that has a product of the category
cursor.execute("SELECT nro, serial_number, manufacturer FROM shelf WHERE name = %s", (c_name,))
shelves = cursor.fetchall()
for s in shelves:
cursor.execute("DELETE FROM replenishment_event WHERE nro = %s AND serial_number = %s AND manufacturer = %s", (s[0], s[1], s[2]))
cursor.execute("DELETE FROM planogram WHERE nro = %s AND serial_number = %s AND manufacturer = %s", (s[0], s[1], s[2]))
cursor.execute("DELETE FROM shelf WHERE name = %s", (c_name,))
cursor.execute("DELETE FROM responsible_for WHERE name_cat = %s", (c_name,))
cursor.execute("DELETE FROM simple_category WHERE name = %s", (c_name,))
cursor.execute("DELETE FROM super_category WHERE name = %s", (c_name,))
cursor.execute("DELETE FROM category WHERE name = %s", (c_name,))
return render_template("CategoryTableRemoveSuccess.html", c_name = c_name, cursor = cursor, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
dbConn.commit()
cursor.close()
dbConn.close()
# ====================== Inserir e remover um retalhista =======================
# Retailer Table
@app.route("/retailer_table")
def retailer_table():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute("SELECT tin, name FROM retailer ORDER BY name;")
return render_template("RetailerTable.html", cursor = cursor, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.close()
# Retailer Independent
@app.route("/retailer_independent")
def retailer_independent():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute("SELECT tin, name FROM retailer;")
return render_template("RetailerIndependent.html", cursor = cursor, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.close()
# Retailer Add
@app.route("/retailer_add", methods = ["POST"])
def retailer_add():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
r_tin = request.form["r_tin"]
r_name = request.form["r_name"]
cursor.execute("INSERT INTO retailer VALUES(%s, %s)", (r_tin, r_name))
return render_template("RetailerIndependentAddSuccess.html", r_tin = r_tin, r_name = r_name, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.commit()
dbConn.close()
# Retailer Remove
@app.route("/retailer_remove")
def retailer_remove():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
r_tin = request.args["r_tin"]
r_name = request.args["r_name"]
# Get the products the retailer is responsible for
query = "SELECT ean FROM has_category h \
JOIN responsible_for r ON h.name = r.name_cat \
WHERE r.tin = %s;"
cursor.execute(query, (r_tin,))
eans = cursor.fetchall()
# Delete the retailer from everywhere he/she is, no dependencies must be broken
cursor.execute("DELETE FROM replenishment_event WHERE tin = %s", (r_tin,))
cursor.execute("DELETE FROM responsible_for WHERE tin = %s", (r_tin,))
cursor.execute("DELETE FROM retailer WHERE tin = %s", (r_tin,))
# Delete all products the retailer is responsible for from everywhere they are
for ean in eans:
cursor.execute("DELETE FROM has_category WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM planogram WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM replenishment_event WHERE ean = %s", (ean[0],))
cursor.execute("DELETE FROM product WHERE ean = %s", (ean[0],))
return render_template("RetailerTableRemoveSuccess.html", r_tin = r_tin, r_name = r_name, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
dbConn.commit()
cursor.close()
dbConn.close()
# ============== Listar todos os eventos de reposição de uma IVM ===============
# IVM Table
@app.route("/ivm_table")
def ivm_table():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute("SELECT manufacturer, serial_number FROM ivm ORDER BY manufacturer;")
return render_template("IvmTable.html", cursor = cursor, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.close()
# IVM Events
@app.route("/ivm_events")
def ivm_events():
cursor1 = None
cursor2 = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor1 = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor2 = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
i_manufacturer = request.args["i_manufacturer"]
i_serial_number = request.args["i_serial_number"]
query1 = "SELECT cat, SUM(units) \
FROM replenishment_event r JOIN product p ON r.ean = p.ean \
GROUP BY p.cat, r.manufacturer, r.serial_number \
HAVING manufacturer=%s AND serial_number=%s;"
cursor1.execute(query1, (i_manufacturer, i_serial_number))
query2 = "SELECT cat, descr, instant, units, name \
FROM replenishment_event e \
JOIN product p ON e.ean = p.ean \
JOIN retailer r ON e.tin = r.tin \
WHERE manufacturer=%s AND serial_number=%s \
ORDER BY instant DESC;"
cursor2.execute(query2, (i_manufacturer, i_serial_number))
return render_template("IvmEvents.html", cursor1 = cursor1, cursor2 = cursor2, i_manufacturer = i_manufacturer, i_serial_number = i_serial_number, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
dbConn.commit()
cursor1.close()
cursor2 = None
dbConn.close()
# =========== Listar todas as subcategorias de uma super categoria =============
# Category List
@app.route("/category_list")
def category_list():
cursor1 = None
cursor2 = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor1 = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor2 = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
query1 = "SELECT DISTINCT name FROM super_category s \
JOIN has_other h ON s.name = h.super_category;"
cursor1.execute(query1)
query2 = "SELECT name FROM super_category s \
LEFT JOIN has_other h ON s.name = h.super_category \
WHERE h.super_category IS NULL;"
cursor2.execute(query2)
return render_template("CategoryList.html", cursor1 = cursor1, cursor2 = cursor2, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor1.close()
cursor2.close()
dbConn.close()
# Category Sub
@app.route("/category_sub")
def category_sub():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
super_c_name = request.args["super_c_name"]
subcategories = []
queue = [super_c_name]
while len(queue) != 0:
current = queue.pop(0)
cursor.execute("SELECT category FROM has_other WHERE super_category = %s" , (current,))
for c in cursor:
subcategories.append(c[0])
queue.append(c[0])
return render_template("CategoryListSub.html", super_c_name = super_c_name, subcategories = subcategories, FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
dbConn.commit()
cursor.close()
dbConn.close()
# ================ Dar reset e popular de novo a base de dados =================
# Reset database
@app.route("/reset_database")
def reset_database():
cursor = None
dbConn = None
try:
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cursor.execute(open("sql/reset.sql", "r", encoding="utf-8").read())
cursor.execute(open("sql/constraint.sql", "r", encoding="utf-8").read())
cursor.execute(open("sql/populate.sql", "r", encoding="utf-8").read())
return render_template("index.html", FILENAME = FILENAME)
except Exception as e:
return render_template("error.html", error = str(e), FILENAME = FILENAME)
finally:
cursor.close()
dbConn.commit()
dbConn.close()
# ==============================================================================
if __name__ == "__main__":
app.run(debug = True, use_reloader = True)