forked from Awambeng/online-inventory-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
413 lines (351 loc) · 15.2 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
406
407
408
409
410
411
412
413
from werkzeug.security import check_password_hash
from passlib.hash import bcrypt
from flask import Flask, render_template, request, redirect, jsonify, Response, flash, get_flashed_messages, url_for
import sqlite3
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///OIS.db'
app.secret_key = 'your_secret_key_here'
db=SQLAlchemy(app)
class Location(db.Model):
__tablename__ = "locations"
id=db.Column(db.Integer, primary_key=True)
location_name=db.Column(db.String(50))
number_of_offices=db.Column(db.Integer)
head_quater_contact=db.Column(db.Integer)
employees_rel=db.relationship('Employee', backref='employees_location')
equipment_rel=db.relationship('Equipment', backref='equipment_loation')
def __init__(self, location_name, number_of_offices, head_quater_contact):
self.location_name=location_name
self.number_of_offices=number_of_offices
self.head_quater_contact=head_quater_contact
def __repr__ (self):
return f"{self.location_name}, {self.number_of_offices}, {self.head_quater_contact}"
class Employee(db.Model):
__tablename__="employees"
id=db.Column(db.Integer, primary_key=True)
password=db.Column(db.String(20))
employee_name=db.Column(db.String(20))
gender=db.Column(db.String(6))
title=db.Column(db.String(10))
type=db.Column(db.String(10))
phone_number=db.Column(db.Integer)
department=db.Column(db.String(50))
location=db.Column(db.Integer, db.ForeignKey('locations.id'))
location_rel=db.relationship('Location', backref='employees_location')
equipment_rel=db.relationship('Equipment', backref='equipment_employee')
def __init__(self, employee_name, password, gender, title, type, phone_number, department, location):
self.password=password
self.employee_name=employee_name
self.gender=gender
self.title=title
self.type=type
self.phone_number=phone_number
self.department=department
self.location=location
def __repr__ (self):
return f"{self.employee_name}, {self.gender}, {self.title}, {self.type}, {self.phone_number}, {self.department}, {self.location}"
class Purchase(db.Model):
__tablename__ = "purchases"
id=db.Column(db.Integer, primary_key=True)
date=db.Column(db.String(10))
store=db.Column(db.String(10))
warranty_period=db.Column(db.Integer)
equipment_rel=db.relationship('Equipment', backref='equipment_purchase')
def __init__(self, date, store, warranty_period):
self.date=date
self.store=store
self.warranty_period=warranty_period
def __repr__(self):
return f"{self.date}, {self.store}, {self.warranty_period}"
class Equipment(db.Model):
__tablename__="equipment"
barcode_number=db.Column(db.Integer, primary_key=True)
type=db.Column(db.String(20))
serial_number=db.Column(db.Integer)
model_number=db.Column(db.Integer)
purchase_date=db.Column(db.Integer, db.ForeignKey('purchases.id'))
employee=db.Column(db.Integer, db.ForeignKey('employees.id'))
location=db.Column(db.Integer, db.ForeignKey('locations.id'))
purchase_rel=db.relationship('Purchase', backref='equipment_purchase')
employee_rel=db.relationship('Employee', backref='equipment_employee')
location_rel=db.relationship('Location', backref='equipment_location')
def __init__(self, type, serial_number, model_number, purchase_date, employee, location):
self.type=type
self.serial_number=serial_number
self.model_number=model_number
self.purchase_date=purchase_date
self.employee=employee
self.location=location
def __repr__ (self):
return f"{self.type}, {self.serial_number}, {self.model_number}, {self.purchase_date}, {self.employee}, {self.location}"
# creating a decorator that creates all the tables in the sqlalchemy model before any request is done
@app.before_request
def create_table():
db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
employees = Employee.query.all()
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
login = request.form['loging']
emp = Employee.query.filter_by(employee_name=username, type=login).first()
print(emp)
if emp is None or not bcrypt.verify(password, emp.password):
flash('Wrong username or password. Please try again.', 'error')
return render_template('login.html')
global keeper
keeper = emp.employee_name
employees = Employee.query.all()
employee_count = len(employees)
purchases = Purchase.query.all()
purchase_count = len(purchases)
locations = Location.query.all()
location_count = len(locations)
equipment = Equipment.query.all()
equipment_count = len(equipment)
return render_template('main.html',
employees = employees, employee_count=employee_count,
purchases = purchases, purchase_count=purchase_count,
locations = locations, location_count=location_count,
equipment = equipment, equipment_count=equipment_count,
name=keeper
)
@app.route('/entry_point')
def entry_point():
employees = Employee.query.all()
employee_count = len(employees)
purchases = Purchase.query.all()
purchase_count = len(purchases)
locations = Location.query.all()
location_count = len(locations)
equipment = Equipment.query.all()
equipment_count = len(equipment)
return render_template('main.html',
employees = employees, employee_count=employee_count,
purchases = purchases, purchase_count=purchase_count,
locations = locations, location_count=location_count,
equipment = equipment, equipment_count=equipment_count,
name=keeper
)
@app.route('/location')
def location():
locations = Location.query.all()
return render_template ('locationList.html', locations = locations)
@app.route('/add-location', methods=['GET', 'POST'])
def add_location_details():
if request.method == 'GET':
return render_template('locationForm.html')
if request.method == 'POST':
# Handle the form submission
location_name = request.form['location_name']
number_of_offices = request.form['number_of_offices']
head_quater_contact = request.form['head_quater_contact']
location = Location(
location_name=location_name,
number_of_offices=number_of_offices,
head_quater_contact=head_quater_contact
)
db.session.add(location)
db.session.commit()
return redirect('/location')
# If the request method is not GET or POST, return an error response
return 'Method Not Allowed', 405
@app.route('/add-employee', methods=['GET', 'POST'])
def add_employee_details():
locations = Location.query.all()
if request.method == 'GET':
return render_template('employeeForm.html', locations=locations)
if request.method == 'POST':
# Handle the form submission
employee_name = request.form['employee_name']
password= request.form['password']
hashed_password = bcrypt.hash(password)
gender = request.form['gender']
title = request.form['title']
type = request.form['type']
phone_number = request.form['phone_number']
department = request.form['department']
location = request.form['location']
employee = Employee(
employee_name=employee_name,
password=hashed_password,
gender=gender,
title=title,
type=type,
phone_number=phone_number,
department=department,
location=location,
)
db.session.add(employee)
db.session.commit()
return redirect('/employee')
# If the request method is not GET or POST, return an error response
return 'Method Not Allowed', 405
@app.route('/add-equipment', methods=['GET', 'POST'])
def add_equipment_details():
locations = Location.query.all()
employees = Employee.query.all()
purchases = Purchase.query.all()
if request.method == 'GET':
return render_template('equipmentForm.html', locations=locations, employees=employees, purchases=purchases)
if request.method == 'POST':
# Handle the form submission
type = request.form['type']
serial_number = request.form['serial_number']
model_number = request.form['model_number']
purchase_date = request.form['purchase_date']
employee = request.form['employee']
location = request.form['location']
equipment = Equipment(
type=type,
serial_number=serial_number,
model_number=model_number,
purchase_date=purchase_date,
employee=employee,
location=location,
)
db.session.add(equipment)
db.session.commit()
return redirect('/equipment')
# If the request method is not GET or POST, return an error response
return 'Method Not Allowed', 405
@app.route('/purchase')
def purchase():
purchases = Purchase.query.all()
return render_template('purchaseList.html', purchases=purchases)
@app.route('/add-purchase', methods=['GET', 'POST'])
def add_purchase_details():
if request.method == 'GET':
return render_template('purchaseForm.html')
if request.method == 'POST':
date = request.form['date']
store = request.form['store']
warranty_period = request.form['warranty-period']
purchase = Purchase(date=date, store=store, warranty_period=warranty_period)
db.session.add(purchase)
db.session.commit()
return redirect('/purchase')
@app.route('/edit_purchase<int:id>', methods=['GET' ,'POST' ])
def edit_purchase(id):
purchase = Purchase.query.get(id)
if request.method == 'GET':
return render_template('edit_purchase.html' ,purchase=purchase)
if request.method == 'POST':
purchase.date = request.form['date']
purchase.store = request.form['store']
purchase.warranty_period = request.form['warranty_period']
db.session.commit()
return redirect('/purchase')
@app.route('/delete_purchase<int:id>')
def delete_purchase(id):
purchase = Purchase.query.get_or_404(id)
db.session.delete(purchase)
db.session.commit()
return redirect('/purchase')
@app.route('/edit_location<int:id>', methods=['GET' ,'POST' ])
def edit_location(id):
location = Location.query.get(id)
if request.method == 'GET':
return render_template('edit_location.html' ,location=location)
if request.method == 'POST':
location.location_name = request.form['location_name']
location.number_of_offices = request.form['number_of_offices']
location.head_quater_contact = request.form['head_quater_contact']
db.session.commit()
return redirect('/location')
@app.route('/edit_employee<int:id>', methods=['GET' ,'POST' ])
def edit_employee(id):
locations = Location.query.all()
employee = Employee.query.get(id)
if request.method == 'GET':
return render_template('edit_employee.html', employee=employee, locations=locations)
if request.method == 'POST':
employee.employee_name = request.form['employee_name']
employee.gender = request.form['gender']
employee.title = request.form['title']
employee.type = request.form['type']
employee.phone_number = request.form['phone_number']
employee.department = request.form['department']
employee.location = request.form['location']
db.session.commit()
return redirect('/employee')
@app.route('/edit_equipment<int:barcode_number>', methods=['GET' ,'POST' ])
def edit_equipment(barcode_number):
locations = Location.query.all()
employees = Employee.query.all()
purchases = Purchase.query.all()
equipment = Equipment.query.get(barcode_number)
if request.method == 'GET':
return render_template('edit_equipment.html', equipment=equipment, locations=locations, purchases=purchases, employees=employees)
if request.method == 'POST':
equipment.type = request.form['type']
equipment.serial_number = request.form['serial_number']
equipment.model_number = request.form['model_number']
equipment.purchase_date = request.form['purchase_date']
equipment.employee = request.form['employee']
equipment.location = request.form['location']
db.session.commit()
return redirect('/equipment')
@app.route('/delete_location<int:id>')
def delete_location(id):
location = Location.query.get_or_404(id)
db.session.delete(location)
db.session.commit()
return redirect('/location')
@app.route('/delete_employee<int:id>')
def delete_employee(id):
employee = Employee.query.get_or_404(id)
db.session.delete(employee)
db.session.commit()
return redirect('/employee')
@app.route('/delete_equipment<int:barcode_number>')
def delete_equipment(barcode_number):
equipment = Equipment.query.get_or_404(barcode_number)
db.session.delete(equipment)
db.session.commit()
return redirect('/equipment')
@app.route('/equipment')
def equipment_index():
equipment = Equipment.query.all()
return render_template('equipmentList.html', equipment = equipment)
@app.route('/employee')
def employee_index():
employees = Employee.query.all()
return render_template ('employeeList.html', employees = employees)
@app.route('/search', methods=['GET'])
def search():
res = None
entity = request.args.get('entity')
query = request.args.get('query')
if entity == 'location':
data = Location.query.filter(Location.location_name.contains(query)).all()
res = jsonify([{
"location_name": r.location_name, "number_of_offices": r.number_of_offices, "head_quater_contact": r.number_of_offices
} for r in data])
elif entity == 'employee':
data = Employee.query.filter(Employee.employee_name.contains(query)).all()
res = jsonify([{
"employee_name": r.employee_name, "gender": r.gender, "title": r.title, "type": r.type, "phone_number": r.phone_number, "department": r.department, "location": r.location
} for r in data])
elif entity == 'equipment':
data = Equipment.query.filter(Equipment.type.contains(query)).all()
res = jsonify([{
"type": r.type, "serial_number": r.serial_number, "model_number": r.model_number, "purchase_date": r.purchase_date, "employee": r.employee, "location": r.location
} for r in data])
elif entity == 'purchase':
data = Purchase.query.filter(Purchase.store.contains(query)).all()
res = jsonify([{
"date": r.date, "store": r.store, "warranty_period": r.warranty_period,
} for r in data])
else:
res = Response("Invalid entity", status=400)
return res
if __name__ == '__main__':
app.run(host='localhost', port=8080, debug=True)