-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurchase.py
More file actions
129 lines (106 loc) · 3.13 KB
/
Copy pathPurchase.py
File metadata and controls
129 lines (106 loc) · 3.13 KB
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
username = form.getvalue('username')
productNames = []
productAmounts = []
maxIndex = 0
#get from the form which checkboxes with ticked and the quantities
if form.getvalue('dog1'):
productNames.append('dog1')
value = form.getvalue('numDog1')
if value != None:
productAmounts.append(int(value))
else:
productAmounts.append(0)
maxIndex = maxIndex+1
if form.getvalue('dog2'):
productNames.append('dog2')
value = form.getvalue('numDog2')
if value != None:
productAmounts.append(int(value))
else:
productAmounts.append(0)
maxIndex = maxIndex+1
if form.getvalue('dog3'):
productNames.append('dog3')
value = form.getvalue('numDog3')
if value != None:
productAmounts.append(int(value))
else:
productAmounts.append(0)
maxIndex = maxIndex+1
isLogged = 0
totalCost = 0.0
#check if the user is logged in
import csv
with open('../data/LoggedIn.csv', 'rb') as loggedInUsersFile:
reader = csv.reader(loggedInUsersFile)
for row in reader:
if row[0] == username:
isLogged = 1
#if he is print a bill from the inventory file
if isLogged == 1:
print "Content-Type: text/html;charset=us-ascii\n\n"
print "<html>\n"
print "<head>\n"
print "<title>Your Bill</title>\n"
print "</head>\n\n"
print "<body>\n\n"
print "<table cellspacing = '1' border = '1'>\n\n"
print "<tr>\n"
print "<td>Product Name</td>\n"
print "<td>Number of units</td>\n"
print "<td>Price</td>\n"
print "</tr>"
newRows = []
index = 0
with open('../data/Inventory.csv', 'rb') as inventoryFile:
reader = csv.reader(inventoryFile)
for row in reader:
newRows.append(row)
for i in range(0, maxIndex):
index = 0
with open('../data/Inventory.csv', 'rb') as inventoryFile:
reader = csv.reader(inventoryFile)
for row in reader:
if row[0] == productNames[i]:
if int(row[1]) < productAmounts[i]:
productAmounts[i] = int(row[1])
totalCost += (float(row[2]) * productAmounts[i])
newRows[index][1] = int(row[1]) - productAmounts[i]
print"<tr>\n"
print "<td>%s</td>\n" % productNames[i]
print "<td>%d</td>\n" % productAmounts[i]
print "<td>%0.2f</td>\n" % (float(row[2]) * productAmounts[i])
print "</tr>\n"
break
index = index + 1
print "<tr>\n"
print "<td>Total Cost</td>\n"
print "<td colspan = '2' align='right'>%0.2f</td>\n" % totalCost
print "</tr>\n"
print"</table>\n\n"
print"<a href='../index.html'><br><br>Home</a><br><br>\n"
print"<a href='../catalogue.html'>Back to catalogue</a><br><br>\n"
print"</body>\n"
print"</html>\n"
with open('../data/Inventory.csv', 'wb') as inv:
writer = csv.writer(inv)
writer.writerows(newRows)
else:
#1.1 If not logged in then display error screen in HTML linking back to catalogue.
print "Content-Type: text/html;charset=us-ascii\n\n"
print "<html>\n"
print "<head>\n"
print"<title>Failure</title>\n"
print"</head>\n\n"
print"<body>\n\n"
print"<b>Purchase Failed, you need to log in!<br><br></b>\n"
print"<a href='../index.html'>Home</a><br><br>\n"
print"<a href='../login.html'>Back to login</a><br><br>\n"
print"</body>\n"
print"</html>\n"