-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmpApp.py
418 lines (352 loc) · 15.1 KB
/
EmpApp.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
414
415
416
417
418
from flask import Flask, render_template, request,url_for,flash,redirect
from pymysql import connections
import os
import boto3
from config import *
app = Flask(__name__)
bucket = custombucket
region = customregion
db_conn = connections.Connection(
host=customhost,
port=3306,
user=customuser,
password=custompass,
db=customdb
)
output = {}
table = 'employee'
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('HomePage.html')
@app.route("/home_page", methods=['GET', 'POST'])
def home_page():
return render_template('HomePage.html')
@app.route("/search_employee", methods=['GET', 'POST'])
def search_employee():
return render_template('SearchEmployee.html')
@app.route("/my_profile", methods=['GET','POST'])
def my_profile():
cursor = db_conn.cursor()
try:
cursor.execute("SELECT * FROM employee WHERE job='admin'")
records = cursor.fetchall()
for row in records:
emp_id= row[0]
first_name= row[1]
last_name = row[2]
age= row[3]
gender= row[4]
location= row[5]
pri_skill= row[6]
email= row[7]
department= row[8]
job= row[9]
salary= row[10]
hire_date= row[11]
emp_name = "" + first_name + " " + last_name
emp_image_file_name_in_s3 = "https://david-jeremy-jaedon-cc-asm-bucket.s3.amazonaws.com/emp-id-" + str(emp_id) + "_image_file"
# iterate over the cursor
finally:
cursor.close()
# Not relevant to our design
return render_template('MyProfile.html', emp_id=emp_id,emp_name=emp_name,gender=gender,pri_skill=pri_skill, job=job,location=location, hire_date=hire_date,image_url=emp_image_file_name_in_s3)
@app.route("/employee_documentation", methods=['GET', 'POST'])
def employee_documentation():
cursor = db_conn.cursor()
cursor.execute("SELECT * FROM employee")
data = cursor.fetchall()
return render_template('EmployeeDocumentation.html', data=data)
@app.route("/add_employee", methods=['GET', 'POST'])
def add_employee():
return render_template('AddEmployee.html')
@app.route("/add_employee_function", methods=['POST'])
def add_employee_function():
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
pri_skill = request.form['pri_skill']
location = request.form['location']
email = request.form['email']
age = request.form['age']
gender = request.form['gender']
hire_date = request.form['hire_date']
salary = request.form['salary']
job = request.form['job']
department = request.form['department']
payroll_id= "OT"+emp_id
late_hours= "0"
overtime_hours= "0"
emp_image_file = request.files['emp_image_file']
insert_sql= "INSERT into employee VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)"
insert_payroll_sql= "INSERT into payroll VALUES (%s, %s, %s, %s, %s)"
cursor = db_conn.cursor()
if emp_image_file.filename == "":
return "Please select a file"
try:
cursor.execute(insert_sql,(emp_id,first_name,last_name,age,gender,location,pri_skill,email,department,job,salary,hire_date))
db_conn.commit()
cursor.execute(insert_payroll_sql,(payroll_id,late_hours,overtime_hours,salary,emp_id))
db_conn.commit()
emp_name = "" + first_name + " " + last_name
# Uplaod image file in S3 #
emp_image_file_name_in_s3 = "emp-id-" + str(emp_id) + "_image_file"
s3 = boto3.resource('s3')
try:
print("Data inserted in MySQL RDS... uploading image to S3...")
s3.Bucket(custombucket).put_object(Key=emp_image_file_name_in_s3, Body=emp_image_file)
bucket_location = boto3.client('s3').get_bucket_location(Bucket=custombucket)
s3_location = (bucket_location['LocationConstraint'])
if s3_location is None:
s3_location = ''
else:
s3_location = '-' + s3_location
object_url = "https://s3{0}.amazonaws.com/{1}/{2}".format(
s3_location,
custombucket,
emp_image_file_name_in_s3)
except Exception as e:
return str(e)
finally:
cursor.close()
# Not relevant to our design
return render_template('EmployeeProfile.html', emp_id=emp_id,emp_name=emp_name,gender=gender,pri_skill=pri_skill, job=job,location=location, hire_date=hire_date,image_url="https://david-jeremy-jaedon-cc-asm-bucket.s3.amazonaws.com/emp-id-" + str(emp_id) + "_image_file")
@app.route("/delete_profile_function", methods=['GET', 'POST'])
def delete_profile_function():
emp_id = request.form['emp_id']
delete_payroll_sql = "DELETE FROM payroll where emp_id=(%s)"
delete_employee_sql = "DELETE FROM employee where emp_id=(%s)"
cursor = db_conn.cursor()
cursor.execute(delete_payroll_sql,(emp_id))
cursor.execute(delete_employee_sql,(emp_id))
db_conn.commit()
cursor.execute("SELECT * FROM employee")
data = cursor.fetchall()
if data == None:
return render_template('EmployeeDocumentation.html')
else:
return render_template('EmployeeDocumentation.html', data=data)
@app.route("/search_employee_function", methods=['POST'])
def search_employee_function():
emp_id = request.form['emp_id']
search_sql= "SELECT * FROM employee WHERE emp_id = (%s)"
cursor = db_conn.cursor()
emp_image_file_name_in_s3 = "https://david-jeremy-jaedon-cc-asm-bucket.s3.amazonaws.com/emp-id-" + str(emp_id) + "_image_file"
try:
cursor.execute(search_sql,(emp_id))
records = cursor.fetchall()
for row in records:
emp_id= row[0]
first_name= row[1]
last_name = row[2]
age= row[3]
gender= row[4]
location= row[5]
pri_skill= row[6]
email= row[7]
department= row[8]
job= row[9]
salary= row[10]
hire_date= row[11]
emp_name = "" + first_name + " " + last_name
# iterate over the cursor
finally:
cursor.close()
# Not relevant to our design
return render_template('EmployeeProfile.html', emp_id=emp_id,emp_name=emp_name,gender=gender,pri_skill=pri_skill, job=job,location=location, hire_date=hire_date,image_url=emp_image_file_name_in_s3)
@app.route("/edit_profile_function", methods=['GET', 'POST'])
def edit_profile_function():
emp_id = request.form['emp_id']
search_sql= "SELECT * FROM employee WHERE emp_id = (%s)"
cursor = db_conn.cursor()
try:
cursor.execute(search_sql,(emp_id))
records = cursor.fetchall()
for row in records:
emp_id= row[0]
first_name= row[1]
last_name = row[2]
age= row[3]
gender= row[4]
location= row[5]
pri_skill= row[6]
email= row[7]
department= row[8]
job= row[9]
salary= row[10]
hire_date= row[11]
# iterate over the cursor
finally:
cursor.close()
return render_template('EditEmployeeProfile.html', emp_id=emp_id,first_name=first_name, last_name=last_name, age=age, gender=gender, location=location, pri_skill=pri_skill, email=email,department=department,job=job,salary=salary,hire_date=hire_date)
@app.route("/update_employee_function", methods=['GET', 'POST'])
def update_employee_function():
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
pri_skill = request.form['pri_skill']
location = request.form['location']
email = request.form['email']
age = request.form['age']
gender = request.form['gender']
hire_date = request.form['hire_date']
salary = request.form['salary']
job = request.form['job']
department = request.form['department']
search_sql= "SELECT * FROM employee WHERE emp_id = (%s)"
emp_image_file = request.files['emp_image_file']
update_sql= "UPDATE employee SET first_name=(%s), last_name=(%s), age=(%s), gender=(%s), location=(%s), pri_skill=(%s), email=(%s), department=(%s), job=(%s), salary=(%s), hire_date=(%s) WHERE emp_id=(%s)"
update_payroll_sql= "UPDATE payroll SET payroll=(%s) WHERE emp_id=(%s)"
cursor = db_conn.cursor()
if emp_image_file.filename == "":
return "Please select a file"
try:
cursor.execute(update_sql,(first_name,last_name,age,gender,location,pri_skill,email,department,job,salary,hire_date,emp_id))
db_conn.commit()
cursor.execute(update_payroll_sql,(salary,emp_id))
db_conn.commit()
emp_name = "" + first_name + " " + last_name
# Uplaod image file in S3 #
emp_image_file_name_in_s3 = "emp-id-" + str(emp_id) + "_image_file"
s3 = boto3.resource('s3')
try:
print("Data inserted in MySQL RDS... uploading image to S3...")
s3.Bucket(custombucket).put_object(Key=emp_image_file_name_in_s3, Body=emp_image_file)
bucket_location = boto3.client('s3').get_bucket_location(Bucket=custombucket)
s3_location = (bucket_location['LocationConstraint'])
if s3_location is None:
s3_location = ''
else:
s3_location = '-' + s3_location
object_url = "https://s3{0}.amazonaws.com/{1}/{2}".format(
s3_location,
custombucket,
emp_image_file_name_in_s3)
except Exception as e:
return str(e)
finally:
cursor.close()
# Not relevant to our design
return render_template('EmployeeProfile.html', emp_id=emp_id,emp_name=emp_name,gender=gender,pri_skill=pri_skill, job=job,location=location, hire_date=hire_date,image_url="https://david-jeremy-jaedon-cc-asm-bucket.s3.amazonaws.com/emp-id-" + str(emp_id) + "_image_file")
@app.route("/payroll", methods=['GET', 'POST'])
def payroll():
return render_template('Payroll.html')
@app.route("/payroll_deduction", methods=['GET', 'POST'])
def payroll_deduction():
cursor = db_conn.cursor()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('PayrollDeduction.html')
else:
return render_template('PayrollDeduction.html', data=data)
@app.route("/add_payroll_deduction", methods=['GET', 'POST'])
def add_payroll_deduction():
return render_template('AddPayrollDeduction.html')
@app.route("/add_payroll_deduction_function", methods=['GET', 'POST'])
def add_payroll_deduction_function():
emp_id= request.form['emp_id']
salary_sql="SELECT CAST(payroll as UNSIGNED INTEGER) FROM payroll WHERE emp_id=(%s)"
cursor = db_conn.cursor()
cursor.execute(salary_sql,(emp_id))
records = cursor.fetchall()
for row in records:
salary = row[0]
late_hours= request.form['late_hours']
salaryInt= int(salary)
lateHoursInt= int(late_hours)
# For every hour of being late, salary is deducted by 10
payroll = salaryInt - (lateHoursInt*10)
payrollString = str(payroll)
add_late_sql="UPDATE payroll SET late_hours=(%s),payroll=(%s) WHERE emp_id=(%s)"
cursor.execute(add_late_sql,(late_hours, payrollString, emp_id))
db_conn.commit()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('PayrollDeduction.html')
else:
return render_template('PayrollDeduction.html', data=data)
@app.route("/delete_payroll_deduction_function", methods=['GET', 'POST'])
def delete_late_function():
emp_id=request.form['emp_id']
late_hours=request.form['late_hours']
lateHoursInt= int(late_hours)
payroll_sql="SELECT CAST(payroll as UNSIGNED INTEGER) FROM payroll WHERE emp_id=(%s)"
cursor = db_conn.cursor()
cursor.execute(payroll_sql,(emp_id))
records = cursor.fetchall()
for row in records:
payroll = row[0]
payrollInt = int(payroll)
reset_payroll = payrollInt + (lateHoursInt*10)
resetPayrollString = str(reset_payroll)
update_sql= "UPDATE payroll SET late_hours='0', payroll=(%s) WHERE emp_id=(%s)"
cursor.execute(update_sql,(resetPayrollString,emp_id))
db_conn.commit()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('PayrollDeduction.html')
else:
return render_template('PayrollDeduction.html', data=data)
@app.route("/overtime", methods=['GET', 'POST'])
def overtime():
cursor = db_conn.cursor()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('Overtime.html')
else:
return render_template('Overtime.html', data=data)
@app.route("/add_overtime", methods=['GET', 'POST'])
def add_overtime():
return render_template('AddOvertime.html')
@app.route("/add_overtime_function",methods=['GET','POST'])
def add_overtime_function():
emp_id= request.form['emp_id']
salary_sql="SELECT CAST(payroll as UNSIGNED INTEGER) FROM payroll WHERE emp_id=(%s)"
cursor = db_conn.cursor()
cursor.execute(salary_sql,(emp_id))
records = cursor.fetchall()
for row in records:
salary = row[0]
overtime_hours= request.form['overtime_hours']
salaryInt= int(salary)
overtimeHoursInt= int(overtime_hours)
# For every hour of OT, salary is increased by 100
payroll = salaryInt + (overtimeHoursInt*100)
payrollString = str(payroll)
add_overtime_sql="UPDATE payroll SET overtime_hours=(%s),payroll=(%s) WHERE emp_id=(%s)"
cursor.execute(add_overtime_sql,(overtime_hours, payrollString, emp_id))
db_conn.commit()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('Overtime.html')
else:
return render_template('Overtime.html', data=data)
@app.route("/delete_overtime_function", methods=['GET', 'POST'])
def delete_overtime_function():
emp_id=request.form['emp_id']
overtime_hours=request.form['overtime_hours']
overtimeHoursInt= int(overtime_hours)
payroll_sql="SELECT CAST(payroll as UNSIGNED INTEGER) FROM payroll WHERE emp_id=(%s)"
cursor = db_conn.cursor()
cursor.execute(payroll_sql,(emp_id))
records = cursor.fetchall()
for row in records:
payroll = row[0]
payrollInt = int(payroll)
reset_payroll = payrollInt - (overtimeHoursInt*100)
resetPayrollString = str(reset_payroll)
update_sql= "UPDATE payroll SET overtime_hours='0', payroll=(%s) WHERE emp_id=(%s)"
cursor.execute(update_sql,(resetPayrollString,emp_id))
db_conn.commit()
cursor.execute("SELECT e.*,p.* FROM employee e, payroll p WHERE e.emp_id=p.emp_id")
data = cursor.fetchall()
if data == None:
return render_template('Overtime.html')
else:
return render_template('Overtime.html', data=data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)