-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinventory.py
318 lines (246 loc) · 8.1 KB
/
inventory.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
from flask import Flask, render_template, redirect, request, url_for
from flaskext.mysql import MySQL
from flask_basicauth import BasicAuth
import requests
import configparser
from random import randint
import sys
# Configuration
app = Flask(__name__)
config = configparser.ConfigParser()
config.read('config.ini')
version = "0.1"
# Setup protection
app.config['BASIC_AUTH_USERNAME'] = config['basicAuth']['username']
app.config['BASIC_AUTH_PASSWORD'] = config['basicAuth']['password']
app.config['BASIC_AUTH_FORCE'] = config['basicAuth']['forceAuth']
basic_auth = BasicAuth(app)
# Set the MySQL configuration
app.config['MYSQL_DATABASE_USER'] = config['mySQL']['username']
app.config['MYSQL_DATABASE_PASSWORD'] = config['mySQL']['password']
app.config['MYSQL_DATABASE_DB'] = config['mySQL']['database']
app.config['MYSQL_DATABASE_HOST'] = config['mySQL']['host']
mysql = MySQL(app)
mysql.init_app(app)
# Set the index page
@app.route('/')
@app.route('/edit/<editMode>')
@basic_auth.required
def main(editMode = False):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT * FROM items')
results = cur.fetchall()
return render_template('index.html', items=results, editMode=editMode, version=version, config=config['siteSettings'])
except Exception as e:
return redirect('/error/{}'.format(e))
# View all from category page
@app.route('/table/<int:type>')
def makeTable(type):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT * FROM items WHERE `type`="{}"'.format(type))
results = cur.fetchall()
return render_template('/inc/table.html', items=results, config=config['siteSettings'])
except Exception as e:
return redirect('/error/{}'.format(e))
# View items that are no longer in stock
@app.route('/nostock')
@app.route('/nostock/edit/<editMode>')
def nostock(editMode = False):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT * FROM items')
results = cur.fetchall()
return render_template('nostock.html', items=results, editMode=editMode, config=config['siteSettings'])
except Exception as e:
return redirect('/error/{}'.format(e))
# Error Page
@app.route('/error/<string:e>')
def error(e):
return render_template('error.html', error=e, version=version, config=config['siteSettings'])
# Edit name page
@app.route('/edititem/<int:bc>')
def edititem(bc):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT * FROM items WHERE `barcode`={}'.format(bc))
results = cur.fetchall()
return render_template('edititem.html', item=results, config=config['siteSettings'])
except Exception as e:
return redirect('/error/{}'.format(e))
# Update the product name to something else
@app.route('/updateitem',methods=['POST'])
def updateitem():
try:
_name = request.form['item_name']
_barcode = request.form['item_bc']
_type = request.form['item_type']
if _name and _barcode:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('UPDATE `items` SET `name`="{}", `type`="{}" WHERE `barcode`= {}'.format(_name, _type, _barcode))
conn.commit()
else:
raise RuntimeError("There was an error with the form")
return redirect(url_for('main'))
except Exception as e:
return redirect('/error/{}'.format(e))
# Add new item manually
@app.route('/add')
@app.route('/add/<bc>')
def add(bc = None):
if (bc != None):
try:
if checkAmount(bc) <= 0:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('UPDATE `items` SET `amount`=1 WHERE `barcode`= {}'.format(bc))
conn.commit()
else:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('UPDATE `items` SET `amount`=`amount`+1 WHERE `barcode`= {}'.format(bc))
conn.commit()
return redirect('/edit/true')
except Exception as e:
return redirect('/error/{}'.format(e))
else:
return render_template('add.html', config=config['siteSettings'])
# Generate a barcode for the DB if barcode is disabled or missing on item
def barcodeGenerator():
try:
# Set amount of digits in barcode and generate one
digits = 7
range_start = 10 ** (digits - 1)
range_end = (10 ** digits) - 1
bc = randint(range_start, range_end)
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT * FROM items WHERE `barcode`={}'.format(bc))
results = cur.fetchall()
if len(results) == 0:
return bc
else:
barcodeGenerator()
except Exception as e:
return redirect('/error/{}'.format(e))
# Add to the DB
@app.route('/additem', methods=['POST'])
def addItem():
try:
_barcode = request.form['barcode']
_name = request.form['item_name']
_amount = request.form['amount']
_type = request.form['type']
# Deal with the barcode
if _barcode == "off":
# If barcodes are disabled, generate a random 7 digit one to keep DB happy
_barcode = barcodeGenerator()
if _barcode and _name and _amount and _type:
try:
if config['siteSettings']['enablebarcodes'] == "on":
if sendToOpenFoods(_barcode) != "Unknown Product":
itemName = sendToOpenFoods(_barcode)
else:
itemName = _name
else:
itemName = _name
conn = mysql.connect()
cur = conn.cursor()
cur.execute('INSERT INTO `items` (`name`, `barcode`, `amount`, `type`) VALUES ("{}", {}, {}, {})'.format(itemName, _barcode, _amount, _type))
conn.commit()
return redirect(url_for('main'))
except Exception as e:
return redirect('/error/{}'.format(e))
else:
raise RuntimeError("There was an error with the form")
except Exception as e:
return redirect('/error/{}'.format(e))
# Remove items manually
@app.route('/remove/<int:bc>')
def remove(bc):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('UPDATE `items` SET `amount`=`amount`-1 WHERE `barcode`= {}'.format(bc))
conn.commit()
return redirect('/edit/true')
except Exception as e:
return redirect('/error/{}'.format(e))
# Check amount of items in the inventory DB
def checkAmount(bc):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('SELECT `amount` FROM `items` WHERE `barcode`= {}'.format(bc))
results = cur.fetchone()
return results[0]
except Exception as e:
return redirect('/error/{}'.format(e))
# Completely delete an item from the DB
@app.route('/delete/<int:bc>')
def deleteItem(bc):
try:
conn = mysql.connect()
cur = conn.cursor()
cur.execute('DELETE FROM `items` WHERE `barcode`= {}'.format(bc))
conn.commit()
return redirect('/')
except Exception as e:
return redirect('/error/{}'.format(e))
# Connect to the openfoodfacts.org API
def sendToOpenFoods(bc):
url = "http://world.openfoodfacts.org/api/v0/product/" + bc + ".json"
try:
r = requests.get(url)
data = r.json()
# Deal with response
if data['status_verbose'] == "product found": # If the product is in the DB
return (data['product']['product_name'] + " (" + data['product']['quantity'].replace(" ", "") + ")")
else:
return "Unknown Product"
except Exception as e:
return redirect('/error/{}'.format(e))
# Settings Page
@app.route('/settings')
@basic_auth.required
def settings():
return render_template('settings.html', version=version, config=config['siteSettings'], saved="false")
@app.route('/settings/save', methods=['POST'])
@basic_auth.required
def saveSettings():
try:
_siteTitle = request.form['siteTitle']
_darkMode = request.form.get("darkMode")
_enableBarcodes = request.form.get("enableBarcodes")
if _siteTitle:
try:
# Update the config file
cfg = open("config.ini", 'w')
config.set('siteSettings', 'sitetitle', _siteTitle)
if _darkMode == None:
config.set('siteSettings', 'darkmode', 'off')
else:
config.set('siteSettings', 'darkmode', 'on')
if _enableBarcodes == None:
config.set('siteSettings', 'enablebarcodes', 'off')
else:
config.set('siteSettings', 'enablebarcodes', 'on')
config.write(cfg)
cfg.close()
return "ok"
except Exception as e:
return redirect('/error/{}'.format(e))
else:
raise RuntimeError("There was an error with the form")
except Exception as e:
return redirect('/error/{}'.format(e))
# Check if test mode
if config['testMode']['active'] == "True":
if __name__ == "__main__":
app.run(host=config['testMode']['host'])