-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
403 lines (308 loc) · 13.5 KB
/
application.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
###############################################################################
# IMPORT REQUIRED LIBRARIES/MODULES
###############################################################################
from flask import Flask #The FLASK framework (the webserver)
from flask import render_template # For opening and read the HTML file and showing them as the webpage
from flask import redirect
from cs50 import SQL
from objects import *
# from flask_table import table, col, linkcol
db = SQL("sqlite:///HandTrucks.db")
import sqlite3
#Needed for OPTION 4 (See below)
from flask import request #Allows me to get stuff from the URL (the ?)
app=Flask(__name__) # Create a Flask instance
###############################################################################
# SET FILE (CSV backend) or Databaser (SQLITE backend)
###############################################################################
type="Database"
# PAGE: INDEX
#------------------------------------------------------------------------------------
@app.route("/") # Decorator - Place above any other app.route to set "homepage"
@app.route("/index") # Decorator - Now
def index():
return render_template('index.html')
# PAGE: DISPLAY CUSTOMERS
#------------------------------------------------------------------------------------
@app.route("/customers", methods=["GET", "POST"])
def customers():
c_list = getDictList_DB()
return render_template("customers.html", customer = c_list)
# PAGE: DISPLAY SUPPLIERS
#------------------------------------------------------------------------------------
@app.route("/suppliers", methods=["GET", "POST"])
def suppliers():
s_list = getSuppDictList_DB()
return render_template("suppliers.html", supplier = s_list)
# PAGE: DISPLAY PRODUCTS
#------------------------------------------------------------------------------------
@app.route("/products", methods=["GET", "POST"])
def products():
p_list = getPDictList_DB()
return render_template("products.html", product = p_list)
# PAGE: EDIT CUSTOMERS
#------------------------------------------------------------------------------------
@app.route("/edit", methods=["POST","GET"])
def edit():
customerid = request.args.get('CustomerID')
if request.method == "GET":
e_customer=getCustomer_DB(customerid)
print(customerid)
return render_template("edit.html",customer = e_customer)
else:
CustomerID = getIntegerFormVariable("CustomerID")
Fname = getFormVariable("fname")
Lname = getFormVariable("lname")
ZipCode = getFormVariable("ZipCode")
Telephone = getIntegerFormVariable("Telephone")
email = getFormVariable("email")
Category = getFormVariable("Category")
updateCustomerDB(CustomerID,Fname, Lname, ZipCode, Telephone, email, Category)
#delete and save
#update(CustomerID,Fname, Lname, ZipCode, Telephone, email, Category)
print(updateCustomerDB(CustomerID,Fname, Lname, ZipCode, Telephone, email, Category))
c_list=getDictList_DB()
return render_template("customers.html",customer=c_list)
# PAGE: ADD CUSTOMER
#------------------------------------------------------------------------------------
@app.route("/add", methods=["POST", "GET"]) # Decorator - Now
def add():
if request.method == "GET":
cList=getDictList_DB()
return render_template("add.html", message=cList)
else:
CustomerID = getIntegerFormVariable("CustomerID")
Fname = getFormVariable("fname")
Lname = getFormVariable("lname")
ZipCode = getFormVariable("ZipCode")
Telephone = getIntegerFormVariable("Telephone")
email = getFormVariable("email")
Category = getFormVariable("Category")
saveCustomerDB(CustomerID, Fname, Lname, ZipCode, Telephone, email, Category )
cList=getDictList_DB()
return render_template("customers.html", customer=cList)
# PAGE: CREATE ORDER -1
#------------------------------------------------------------------------------------
@app.route("/createorder1", methods=["POST", "GET"]) # Decorator - Now
def createorder1():
if request.method == "GET":
dList=getDDictList_DB()
# oList = getODictList_DB()
return render_template("createorder1.html", message=dList)
else:
OrdDet_ID = getIntegerFormVariable("OrdDet_ID")
Product_ID = getIntegerFormVariable("Product_ID")
OrderID = getIntegerFormVariable("OrderID")
Quantity = getIntegerFormVariable("Quantity")
CustomerID= getIntegerFormVariable("CustomerID")
OrderDate= getFormVariable("OrderDate")
OrderType = getFormVariable("OrderType")
SalesPerson = getIntegerFormVariable("SalesPerson")
saveOrdDetDB(OrdDet_ID, Product_ID, OrderID,Quantity,OrderDate, CustomerID, OrderType, SalesPerson)
dList=getDDictList_DB()
oList = getODictList_DB()
if getIntegerFormVariable("OrdDet_ID2") != 0:
OrdDet_ID = getIntegerFormVariable("OrdDet_ID2")
Product_ID = getIntegerFormVariable("Product_ID2")
OrderID = getIntegerFormVariable("OrderID")
Quantity = getIntegerFormVariable("Quantity2")
saveOrdDetDB2(OrdDet_ID, Product_ID, OrderID, Quantity)
dList=getDDictList_DB()
# return render_template("createorder1.html", message=dList)
if getIntegerFormVariable("OrdDet_ID3") != 0:
OrdDet_ID = getIntegerFormVariable("OrdDet_ID3")
Product_ID = getIntegerFormVariable("Product_ID3")
OrderID = getIntegerFormVariable("OrderID")
Quantity = getIntegerFormVariable("Quantity3")
saveOrdDetDB2(OrdDet_ID, Product_ID, OrderID,Quantity)
dList=getDDictList_DB()
if getIntegerFormVariable("OrdDet_ID4") != 0:
OrdDet_ID = getIntegerFormVariable("OrdDet_ID4")
Product_ID = getIntegerFormVariable("Product_ID4")
OrderID = getIntegerFormVariable("OrderID")
Quantity = getIntegerFormVariable("Quantity4")
saveOrdDetDB2(OrdDet_ID, Product_ID, OrderID,Quantity)
dList=getDDictList_DB()
if getIntegerFormVariable("OrdDet_ID5") != 0:
OrdDet_ID = getIntegerFormVariable("OrdDet_ID5")
Product_ID = getIntegerFormVariable("Product_ID5")
OrderID = getIntegerFormVariable("OrderID")
Quantity = getIntegerFormVariable("Quantity5")
CustomerID= getIntegerFormVariable("CustomerID")
saveOrdDetDB2(OrdDet_ID, Product_ID, OrderID,Quantity)
dList=getDDictList_DB()
return render_template("createorder1.html", message=dList)
###################################################################################
# "NORMAL" PYTHON
###################################################################################
# 1. GET VARIABLES FROM FORMS
#---------------------------
def getFormVariable(variableName):
return request.form.get(variableName, 0)
def getIntegerFormVariable(variableName):
return int(request.form.get(variableName, 0))
########################################################################################
########################################################################################
# IF DATABASE BASED ....
########################################################################################
########################################################################################
# Customer Dictionary
#-------------------------------------
def getDictList_DB():
con = sqlite3.connect('HandTrucks.db')
cursorObj = con.cursor()
rowSTRING2 = "["
cursorObj.execute('SELECT * FROM Customer;')
rows = cursorObj.fetchall()
for row in rows:
newRow = []
c = Customer(row[0], row[1],row[2],row[3],row[4],row[5],row[6])
rowSTRING2 += "{" + c.getDict() + "},"
rowSTRING2 = rowSTRING2[0:-1]
rowSTRING2 = rowSTRING2 + "]"
rowSTRING2 = eval(rowSTRING2)
return rowSTRING2
#Supplier DB
def getSuppDictList_DB():
con = sqlite3.connect('HandTrucks.db')
cursorObj = con.cursor()
rowSTRING2 = "["
cursorObj.execute('SELECT * FROM Supplier;')
rows = cursorObj.fetchall()
for row in rows:
newRow = []
s = Supplier(row[0], row[1],row[2],row[3],row[4])
rowSTRING2 += "{" + s.getDict() + "},"
rowSTRING2 = rowSTRING2[0:-1]
rowSTRING2 = rowSTRING2 + "]"
rowSTRING2 = eval(rowSTRING2)
return rowSTRING2
#Product Dictionaries
def getPDictList_DB():
con = sqlite3.connect('HandTrucks.db')
cursorObj = con.cursor()
rowSTRING2 = "["
cursorObj.execute('SELECT * FROM Product;')
rows = cursorObj.fetchall()
for row in rows:
newRow = []
p = Product(row[0], row[1],row[2],row[3])
rowSTRING2 += "{" + p.getDict() + "},"
rowSTRING2 = rowSTRING2[0:-1]
rowSTRING2 = rowSTRING2 + "]"
rowSTRING2 = eval(rowSTRING2)
return rowSTRING2
#Order Dictionaries
def getODictList_DB():
con = sqlite3.connect('HandTrucks.db')
cursorObj = con.cursor()
rowSTRING2 = "["
cursorObj.execute('SELECT * FROM Orders;')
rows = cursorObj.fetchall()
for row in rows:
newRow = []
o = Order(row[0], row[1],row[2],row[3], row[4])
rowSTRING2 += "{" + o.getDict() + "},"
rowSTRING2 = rowSTRING2[0:-1]
rowSTRING2 = rowSTRING2 + "]"
rowSTRING2 = eval(rowSTRING2)
return rowSTRING2
#OrdDet Dictionary
def getDDictList_DB():
con = sqlite3.connect('HandTrucks.db')
cursorObj = con.cursor()
rowSTRING2 = "["
cursorObj.execute('SELECT * FROM OrderDet;')
rows = cursorObj.fetchall()
for row in rows:
newRow = []
d = OrderDet(row[0], row[1],row[2],row[3])
rowSTRING2 += "{" + d.getDict() + "},"
rowSTRING2 = rowSTRING2[0:-1]
rowSTRING2 = rowSTRING2 + "]"
rowSTRING2 = eval(rowSTRING2)
return rowSTRING2
# # 5. DELETE Customer and Update to a New One
# #-------------------------------------
def update(CustomerID,Fname, Lname, ZipCode, Telephone, email, Category):
def delCustomer_DB(CustomerID):
database = "HandTrucks.db"
# create a database connection
conn = None
conn = sqlite3.connect(database)
sql='DELETE FROM customer WHERE CustomerID=?'
cur = conn.cursor()
cur.execute(sql, (CustomerID,))
conn.commit()
delCustomer_DB(CustomerID)
def saveCustomerDB(CustomerID, Fname, Lname, ZipCode, Telephone, email, Category ):
database = "HandTrucks.db"
# create a database connection
conn = None
conn = sqlite3.connect(database)
sql='INSERT INTO Customer(CustomerID, Fname,Lname, ZipCode, Telephone,email,Category) VALUES(?,?,?,?,?,?,?)'
cur = conn.cursor()
cur.execute(sql, (CustomerID, Fname, Lname, ZipCode, Telephone, email, Category, ))
conn.commit()
saveCustomerDB(CustomerID, Fname, Lname, ZipCode, Telephone, email, Category )
# GET ONE CUSTOMER
def getCustomer_DB(CustomerID):
database = "HandTrucks.db"
conn = None
conn = sqlite3.connect(database)
sql='SELECT * FROM Customer WHERE CustomerID = ?'
cur= conn.cursor()
cur.execute(sql, [CustomerID])
row = cur.fetchone()
return row
# print(row)
# for row in rows:
# print (row)
# todo = cur.fetchone()
# # 5. ADD CUSTOMER FROM DB
# #-------------------------------------
def saveCustomerDB(CustomerID, Fname, Lname, ZipCode, Telephone, email, Category ):
database = "HandTrucks.db"
# create a database connection
conn = None
conn = sqlite3.connect(database)
sql='INSERT INTO Customer(CustomerID, Fname,Lname, ZipCode, Telephone,email,Category) VALUES(?,?,?,?,?,?,?)'
cur = conn.cursor()
cur.execute(sql, (CustomerID, Fname, Lname, ZipCode, Telephone, email, Category, ))
conn.commit()
# # 6. UPDATE CUSTOMER
# # -------------------------------
def updateCustomerDB(CustomerID,Fname, Lname, ZipCode, Telephone, email, Category):
database = "HandTrucks.db"
conn = None
conn = sqlite3.connect(database)
sql="UPDATE Customer SET Fname= ?, Lname = ?, ZipCode=?, Telephone= ?, email= ?, Category= ? WHERE CustomerID = ?"
cur = conn.cursor()
cur.execute(sql, (Fname, Lname, ZipCode, Telephone, email, Category,CustomerID, ))
conn.commit()
# These functions insert data into the OrderDet table and the Orders Table after the order is created.
def saveOrdDetDB(OrdDet_ID, Product_ID, OrderID, Quantity, OrderDate, CustomerID, OrderType, SalesPerson):
database = "HandTrucks.db"
# create a database connection
conn = None
conn = sqlite3.connect(database)
sql="INSERT INTO OrderDet(OrdDet_ID, Product_ID, OrderID, Quantity) VALUES(?,?,?,?)"
cur = conn.cursor()
cur.execute(sql, (OrdDet_ID, Product_ID, OrderID, Quantity, ))
conn.commit()
conn = None
conn = sqlite3.connect(database)
sql="INSERT INTO Orders(OrderID, OrderDate, CustomerID, OrderType, SalesPerson) VALUES(?,?,?,?,?)"
cur = conn.cursor()
cur.execute(sql, (OrderID, OrderDate, CustomerID, OrderType, SalesPerson, ))
conn.commit()
def saveOrdDetDB2(OrdDet_ID, Product_ID, OrderID, Quantity):
database = "HandTrucks.db"
# create a database connection
conn = None
conn = sqlite3.connect(database)
sql="INSERT INTO OrderDet(OrdDet_ID, Product_ID, OrderID, Quantity) VALUES(?,?,?,?)"
cur = conn.cursor()
cur.execute(sql, (OrdDet_ID, Product_ID, OrderID, Quantity, ))
conn.commit()
conn = None