-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
475 lines (427 loc) · 18.4 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# importing libraries and flask
from flask import Flask, render_template,flash,redirect,url_for,session,request,logging
from functools import wraps
from wtforms import Form, StringField, PasswordField, IntegerField, TextAreaField, validators
from passlib.hash import sha256_crypt
from flask_mysqldb import MySQL
from flask_script import Manager
from datetime import datetime
#creating instance for flask
app = Flask(__name__)
#config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'timecards'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# initialize MySQL
mysql = MySQL(app)
# index
@app.route('/')
def index():
return redirect(url_for('login'))
# LoginHome
@app.route('/loginHome')
def loginHome():
return render_template('loginHome.html')
# Login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['email']
password = request.form['password']
if username =='' or password=='':
flash("please fill all fields",'warning')
return render_template('login.html')
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE email = %s", [username])
if result > 0:
data = cur.fetchone()
password_candidate = data['password']
user_type = data['usertype']
if password_candidate==password:
#**********************************************************
if session.get('logged_in') is None:
session['checked_in'] = False
session['checked_out'] = True
#session['intime'] =
#************************************************************
session['logged_in'] = True
session['username'] = username
session['user_type'] = user_type
session['password'] = password
if user_type ==1:
flash('You are now logged in', 'success')
return redirect(url_for('employerHome'))
else:
flash('You are now logged in', 'success')
return redirect(url_for('employeeHome'))
else:
error = 'Invalid password'
return render_template('login.html', error=error)
cur.close()
else:
error='username not found'
return render_template('login.html', error=error)
else:
return render_template('login.html')
# Home Page
@app.route('/home')
def home():
return render_template('home.html')
# Check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('loginHome'))
return wrap
#check if admin is logged in or not
def is_logged_admin(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session and session['user_type'] == 1:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
# Logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
# Employee Logout *********
@app.route('/employeeLogout')
@is_logged_in
def employeeLogout():
session['logged_in'] = False
flash('You are now logged out employee', 'success')
return redirect(url_for('login'))
# Employer Home
@app.route('/employerHome')
@is_logged_admin
def employerHome():
return render_template('employerHome.html')
# Employee Home
@app.route('/employeeHome', methods = ['GET', 'POST'])
@is_logged_in
def employeeHome():
if request.method=='GET':
return render_template('employeeHome.html')
else:
tick1=0
tick2=0
eswar = dict(request.form)
if eswar['action']==['id1']:
tick1 =1
else:
tick2 =1
app.logger.info('Post request recieved')
email = session['username']
entered_date = request.form['leave_date']
year,month,dte = entered_date.split('-')
app.logger.info(year)
cur = mysql.connection.cursor()
sameresult = cur.execute("SELECT * FROM leaves WHERE email = %s and date = %s",(email,entered_date))
app.logger.info(sameresult)
if sameresult>0:
flash('You have already taken leave on requested date','warning')
app.logger.info(sameresult)
cur.close()
return redirect(url_for('employeeHome'))
result = cur.execute("SELECT * FROM workingdata WHERE email = %s and year = %s and month = %s",(email,year,month))
if result>0:
data = cur.fetchone()
result2 = cur.execute("SELECT * FROM user_details WHERE email = %s",[email])
data2 = cur.fetchone()
eswar = dict(request.form)
app.logger.info(eswar['action'])
if eswar['action']==['id1']:
app.logger.info('in if')
app.logger.info(data2['email'])
if data2['max_casual_leaves'] >data['casualleaves']:
result3 = cur.execute("SELECT * FROM leaves WHERE email = %s and date =%s",(email,entered_date))
if result3>0:
flash('casual leave is already granted on that date','warning')
cur.close()
return redirect(url_for('employeeHome'))
else:
flash('casual leave granted','success')
val =1 + data['casualleaves']
session['leave'] = True
session['leavedate'] = datetime.now()
app.logger.info(val)
cur.execute("insert into leaves(email, date, type) values(%s, %s, %s)", (email, entered_date, 1))
cur.execute("update workingdata set casualleaves = %s where email = %s and year = %s and month = %s",(val,email,year,month))
else:
flash('you have reached your maximum casual leaves','warning')
cur.close()
return redirect(url_for('employeeHome'))
else:
app.logger.info('in else')
if data2['max_sick_leaves'] >data['sickleaves']:
result3 = cur.execute("SELECT * FROM leaves WHERE email = %s and date =%s",(email,entered_date))
if result3>0:
flash('sick leave is already granted on that date','warning')
cur.close()
return redirect(url_for('employeeHome'))
else:
flash('sick leave granted','success')
val =1+data['sickleaves']
cur.execute("update workingdata set sickleaves = %s where email = %s and year = %s and month = %s",(val,email,year,month))
cur.execute("insert into leaves(email, date, type) values(%s, %s, %s)", (email, entered_date, 2))
else:
flash('you have reached your maximum sick leaves','warning')
cur.close()
return redirect(url_for('employeeHome'))
else:
if tick1 ==1:
flash('Casual leave granted','success')
else:
flash('Sick leave granted','success')
cur.execute("insert into workingdata(email, year, month,sickleaves,casualleaves,workinghours) values(%s, %s, %s,%s,%s,%s)", (email,year,month,tick2,tick1,0))
mysql.connection.commit()
cur.close()
return redirect(url_for('employeeHome'))
#admin req needes***********
#Check in********************
# Check if it is a leave for an user:<username> on the day he is checking in
def checkLeave(username, date):
cur = mysql.connection.cursor()
result = cur.execute("select * from leaves where email=%s and date=%s", (username, date))
if result == 0:
cur.close()
return False
else:
cur.close()
return True
@app.route('/checkin')
@is_logged_in
def checkin():
date = datetime.now().date()
if session['checked_in'] == True:
flash('already checked in', 'warning')
return redirect(url_for('employeeHome'))
else:
if checkLeave(session['username'], date):
flash('Today is a leave for you', 'warning')
return redirect(url_for('employeeHome'))
else:
flash('You are checked in', 'success')
session['checked_in'] = True
session['checked_out'] = False
session['checkin'] = datetime.now()
#time and date = session[checkout]
# add time and date to session[checkin]
#
return redirect(url_for('employeeHome'))
#*******
#*********************************************
@app.route('/checkout')
@is_logged_in
def checkout():
if session['checked_out'] == True:
flash('already checked out', 'warning')
return redirect(url_for('employeeHome'))
else:
flash('you are checked out', 'success')
session['checked_in'] = False
session['checked_out'] = True
session['checkout'] = datetime.now()
workingdur = session['checkout'] - session['checkin']
email = session['username']
year = session['checkin'].year
month = session['checkin'].month
#*******SQL QUERY
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM workingdata WHERE email = %s and year = %s and month = %s",(email,year,month))
if result > 0:
app.logger.info('check1')
data = cur.fetchone()
totalworking = data['workinghours']
else:
app.logger.info('check2')
cur.execute("insert into workingdata(email, year, month,sickleaves,casualleaves,workinghours,overtime) values(%s, %s, %s,%s,%s,%s,%s)", (email,year,month,0,0,0,0))
mysql.connection.commit()
totalworking = 0
totalworking = totalworking +(workingdur.seconds/3600.0)
cur.execute("update workingdata set workinghours = %s where email = %s and year = %s and month = %s",(totalworking,email,year,month))
mysql.connection.commit()
cur.close()
#******
#calculate time and date based on
return redirect(url_for('employeeHome'))
#*********************************************
@app.route('/employeeInfo/<string:username>', methods=['GET', 'POST'])
@is_logged_in
def employeeInfo(username):
if request.method != 'POST':
cur = mysql.connection.cursor()
bun = datetime.now()
year = bun.year
month = bun.month
app.logger.info(month)
result = cur.execute("select * from user_details where email=%s",[username])
row = cur.fetchone()
pesult = cur.execute("select * from workingdata where email=%s and year = %s and month = %s",(username,year,month))
prow = cur.fetchone()
app.logger.info(prow)
if result>0:
cur.close()
return render_template('employeeInfo.html', row=row,prow=prow)
'''
rows = cur.fetchall()
for row in rows:
if row['email']==username:
app.logger.info(row)
return render_template('employeeInfo.html', row=row)
cur.close()
break
'''
else:
eswar = dict(request.form)
app.logger.info('Post request recieved')
name = request.form['username']
email = session['username']
salaryPerHour = request.form['salaryPerHour']
jobTitle = request.form['jobTitle']
payInOvertime = request.form['payInOvertime']
maxCasualLeaves = request.form['maxCasualLeaves']
maxSickLeaves = request.form['maxSickLeaves']
cur = mysql.connection.cursor()
app.logger.info(eswar)
if eswar['action']==['update']:
cur.execute("update user_details set name=%s, salary_per_hr=%s, jobTitle=%s, pay_in_overtime=%s, max_casual_leaves=%s, max_sick_leaves=%s where email=%s", (name, salaryPerHour, jobTitle, payInOvertime, maxCasualLeaves, maxSickLeaves, username))
flash("Details updated Succesfully", 'success')
else:
cur.execute('delete from user_details where email=%s', [username])
cur.execute('delete from users where email=%s', [username])
cur.execute('delete from leaves where email=%s', [username])
cur.execute('delete from workingdata where email=%s', [username])
flash('employee deleted succesfully','success')
mysql.connection.commit()
cur.close()
return render_template('employerHome.html')
#View Employees
@app.route('/viewEmployees', methods=['GET', 'POST'])
@is_logged_admin
def viewEmployees():
if request.method=='GET':
cur = mysql.connection.cursor()
result = cur.execute("select * from user_details where email in (select email from users where usertype=2)")
if result>0:
rows = cur.fetchall()
app.logger.info('Hello')
for row in rows:
app.logger.info(row['email'])
return render_template('viewEmployees.html', rows = rows)
cur.close()
else:
flash('No employees found', 'info')
return render_template('employerHome.html')
else:
app.logger.info('IN POAST')
#date = request.form['salary_date']
date = datetime.now()
app.logger.info(date)
month_year = request.form['salary_month_year']
year,month = month_year.split('-')
app.logger.info(year,' ',month)
app.logger.info(year)
cur = mysql.connection.cursor()
result = cur.execute("select * from (workingdata join user_details on workingdata.email = user_details.email) where workingdata.email in (select email from workingdata where year = %s and month = %s) and workingdata.month=%s",(year,month,month))
if result>0:
rows = cur.fetchall()
app.logger.info(rows)
for row in rows:
row.update( {"salary":0})
for row in rows:
row['salary'] = (row['workinghours']+row['sickleaves']*6+row['casualleaves']*6)*row['salary_per_hr']
app.logger.info(row['salary'])
#salary['value'] = row['workinghours']*
#app.logger.info(dict[i]['email'] + ' and ' + dict[i]['salary'])
return render_template('salary_generate.html', rows = rows, date = date)
cur.close()
else:
flash('No employees worked in that month','info')
return render_template('viewEmployees.html')
class NewEmployeeForm(Form):
email = StringField('email', [validators.Length(min=6, max=50)])
name = StringField('name', [validators.Length(min=1, max=50)])
salaryPerHour = IntegerField('salaryPerHour', [validators.NumberRange(min=1, max=100000)])
jobTitle = StringField('jobTitle', [validators.Length(min=1, max=50)])
payInOvertime = IntegerField('payInOvertime', [validators.NumberRange(min=1, max=100000)])
maxCasualLeaves = IntegerField('maxCasualLeaves', [validators.NumberRange(min=0, max=10)])
maxSickLeaves = IntegerField('maxSickLeaves', [validators.NumberRange(min=0, max=10)])
#Add Employee
@app.route('/newEmployee', methods=['GET', 'POST'])
@is_logged_admin
def newEmployee():
form = NewEmployeeForm(request.form)
if request.method == 'POST' and form.validate():
app.logger.info('got a request as POST')
name = form.name.data
email = form.email.data
salaryPerHour = form.salaryPerHour.data
jobTitle = form.jobTitle.data
payInOvertime = form.payInOvertime.data
maxCasualLeaves = form.maxCasualLeaves.data
maxSickLeaves = form.maxSickLeaves.data
cur = mysql.connection.cursor()
result = cur.execute("select * from users where email=%s",[email])
if result>0:
flash('username already taken','warning')
return render_template('newEmployee.html')
cur.execute("insert into users(email, password, usertype) values(%s, %s, %s)", (email, '0000', 2))
mysql.connection.commit()
cur.execute("insert into user_details(email, name, salary_per_hr, jobtitle, pay_in_overtime, max_casual_leaves, max_sick_leaves) values(%s, %s, %s, %s, %s, %s, %s)", (email, name, salaryPerHour, jobTitle, payInOvertime, maxCasualLeaves, maxSickLeaves))
mysql.connection.commit()
cur.close()
flash('New User has been added to the database', 'success')
return redirect(url_for('employerHome'))
else:
if request.method =='POST':
flash('Please fill all blanks','warning')
return render_template('newEmployee.html', form=form)
#Employee Data
@app.route('/employeeData')
@is_logged_in
def employeeData():
return render_template('employeeData')
# Update Password
@app.route('/updatePassword', methods=['GET', 'POST'])
@is_logged_in
def updatePassword():
if request.method=='POST':
app.logger.info('postdone')
cur = mysql.connection.cursor()
currentPassword = request.form['currentPassword']
newPassword = request.form['newPassword']
if newPassword=='' or currentPassword =='':
flash('Please fill all feilds','warning')
return render_template('updatePassword.html')
if currentPassword == session['password']:
cur.execute("update users set password=%s where email=%s",(newPassword,session['username']))
flash('Your password has been updated, Use this password from next login','success')
mysql.connection.commit()
cur.close()
if session['user_type'] ==1:
return redirect(url_for('logout'))
return redirect(url_for('employeeLogout'))
else:
flash('Please fill correct password','warning')
return render_template('updatePassword.html')
else:
app.logger.info('getdone')
return render_template('updatePassword.html')
if __name__ == '__main__':
app.secret_key = 'hel#33'
app.debug = True
manager = Manager(app)
manager.run()