-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
286 lines (205 loc) · 8.14 KB
/
main.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
import os
from shutil import copy2
import sqlite3 as sql
from flask import Flask, render_template, request
repo = Flask(__name__)
def connect():
conn = sql.connect("images.db")
cur = conn.cursor()
return (conn, cur)
"""Buy Product"""
@repo.route("/buy/<product>")
def buy(product):
return render_template("getval.html", message="Enter the quantity you want to buy")
@repo.route("/buy/<product>", methods=['POST'])
def buy_post(product):
if not product:
return render_template("message.html", message="Invalid Product!")
number = None
try:
number = int(request.form['num'])
if number < 0:
return render_template("message.html", message="Invalid Amount!")
except ValueError:
return render_template("message.html", message="Invalid Amount!")
(conn, cur) = connect()
cur.execute("SELECT rowid, buy_price FROM items WHERE rowid = ?", (product,))
item = cur.fetchone()
if not item:
return render_template("message.html", message="Invalid product")
(rowid, buy_price) = item
print("Processed transaction of value $%.2f" % (buy_price * number))
cur.execute("UPDATE items SET amt = amt + ? WHERE rowid = ?", (number, product))
conn.commit()
global bought
bought += number * buy_price
global money
money -= number * buy_price
return render_template("message.html", message="Transaction Complete")
"""Sell Product"""
@repo.route("/sell/<product>")
def sell(product):
return render_template("getval.html", message="Enter the quantity you want to sell")
@repo.route("/sell/<product>", methods=['POST'])
def sell_post(product):
if not product:
return render_template("message.html", message="Invalid Product!")
number = None
try:
number = int(request.form['num'])
if number < 0:
return render_template("message.html", message="Invalid Amount!")
except ValueError:
return render_template("message.html", message="Invalid Amount!")
(conn, cur) = connect()
cur.execute("SELECT rowid, sell_price, amt FROM items WHERE rowid = ?", (product,))
item = cur.fetchone()
if not item:
return render_template("message.html", message="Invalid product")
(rowid, sell_price, amt) = item
if amt < number:
return render_template("message.html", message="Out of Stock!")
print("Processed transaction of value $%.2f" % (sell_price * number))
cur.execute("UPDATE items SET amt = amt - ? WHERE rowid = ?", (number, product))
conn.commit()
global sold
sold += number * sell_price
global money
money += number * sell_price
return render_template("message.html", message="Transaction Complete")
"""Edit Product"""
@repo.route("/edit/<product>")
def edit(product):
(conn, cur) = connect()
cur.execute("SELECT buy_price, sell_price, amt FROM items WHERE rowid = ?", (product,))
item = cur.fetchone()
(buy_price, sell_price, amt) = item
return render_template("edit.html", buy=buy_price, sell=sell_price, amount=amt)
@repo.route("/edit/<product>", methods=['POST'])
def edit_post(product):
if not product:
return render_template("message.html", message="Invalid Product!")
bprice = 0
sprice = 0
amount = 0
try:
bprice = float(request.form['buy_price'])
sprice = float(request.form['sell_price'])
amount = int(request.form['amount'])
if amount < 0 or bprice < 0 or sprice < 0:
return render_template("message.html", message="Invalid Entry!")
except ValueError:
return render_template("message.html", message="Invalid Entry!")
(conn, cur) = connect()
cur.execute("SELECT rowid FROM items WHERE rowid = ?", (product,))
item = cur.fetchone()
if not item:
return render_template("message.html", message="Invalid product")
(rowid) = item[0]
print("Editing prices of {}".format(product))
if bprice is not None:
cur.execute("UPDATE items SET buy_price = ? WHERE rowid = ?", (bprice, rowid))
if sprice is not None:
cur.execute("UPDATE items SET sell_price = ? WHERE rowid = ?", (sprice, rowid))
if amount is not None:
cur.execute("UPDATE items SET amt = ? WHERE rowid = ?", (amount, rowid))
conn.commit()
return render_template("message.html", message="Transaction Complete")
"""Get Money"""
@repo.route("/money")
def get_money():
return render_template("getval.html", message="Enter the amount of money you want to add or remove")
@repo.route('/money', methods=['POST'])
def money_post():
msg = None
global money
if request.method == 'POST':
try:
cost = float(request.form['num'])
money += round(cost, 2)
msg = "You now have ${:.2f}".format(money)
except ValueError:
msg = "Invalid entry!"
return render_template('message.html', message=msg)
@repo.route("/clear")
def clear():
initialize()
return render_template("message.html", message="Database Cleared.")
"""Instantiate DB"""
def initialize():
(conn, cur) = connect()
cur.execute("DROP TABLE IF EXISTS items")
cur.execute("CREATE TABLE items (name TEXT, path TEXT, buy_price REAL, sell_price REAL, amt INTEGER)")
cur.execute("""INSERT INTO items (name, path, buy_price, sell_price, amt) VALUES \
('Starry Night', 'images/starry_night.jpg', 50.00, 120.00, 8), \
('Last Supper', 'images/supper.jpg', 100.00, 270.00, 3), \
('American Gothic', 'images/gothic.jpg', 15.00, 18.50, 34), \
('Mona Lisa', 'images/monalisa.jpg', 600.00, 850.00, 1), \
('Great Waves', 'images/wave.jpg', 3.00, 4.79, 3129)
""")
# Commit the db changes
conn.commit()
print("Initialized database")
global bought, sold, money
bought = 0
sold = 0
money = 1000
@repo.route("/")
def home_page():
(_, cur) = connect()
cur.execute("SELECT rowid, * FROM items")
rows = cur.fetchall()
items = []
for row in rows:
items.append({
"id": row[0],
"name": row[1],
"src": "/static/%s" % (row[2]),
"buy_price": "$%.2f" % (row[3]),
"sell_price": "$%.2f" % (row[4]),
"amt": "%d left" % (row[5])
})
return render_template("index.html", items=items, bought="$%.2f" % bought, sold="$%.2f" % sold, cost="$%.2f" % money)
@repo.route("/add")
def add():
return render_template("add.html")
@repo.route("/add", methods=['POST'])
def add_post():
try:
name = str(request.form['name'])
path = os.path.expanduser(str(request.form['path']))
buy_price = float(request.form['buy_price'])
sell_price = float(request.form['sell_price'])
amt = int(request.form['amt'])
if amt < 0 or buy_price < 0 or sell_price < 0 or not os.path.isfile(path):
return render_template("message.html", message="Invalid Entry!")
except ValueError:
return render_template("message.html", message="Invalid Entry!")
if os.path.basename(path) in os.listdir("./static/images"):
return render_template("message.html", message="This image has the same name as an existing one.")
else:
copy2(path, "./static/images")
path = "images/" + os.path.basename(path)
(conn, cur) = connect()
cur.execute("INSERT INTO items (name, path, buy_price, sell_price, amt) VALUES \
(?, ?, ?, ?, ?)", (name, path, buy_price, sell_price, amt))
conn.commit()
return render_template("message.html", message="The item was added")
@repo.route("/remove/<product>")
def remove(product):
if not product:
return render_template("message.html", message="Invalid Product!")
(conn, cur) = connect()
cur.execute("SELECT path FROM items WHERE rowid = ?", (product,))
item = cur.fetchone()
if not item:
return render_template("message.html", message="Invalid product")
(path) = item[0]
path = "./static/" + path
cur.execute("DELETE FROM items WHERE rowid = ?", (product,))
conn.commit()
os.remove(path)
return render_template("message.html", message="Item removed")
if __name__ == '__main__':
initialize()
repo.run(debug = True)