-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
446 lines (405 loc) · 17.4 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
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
try:
try:
import smtplib
except:
print("Error 100A")
try:
import random
except:
print("Error 100B")
try:
import mysql.connector
except:
print("Error 100C")
try:
import json
except:
print("Error 100D")
except:
print("Error 100")
try:
mydb = mysql.connector.connect(user='root',password='root', host='localhost', database='employees')
mycursor = mydb.cursor()
except:
print("Error 100")
def mail_function(to_address,message,subject):
EMAIL_ADDRESS = "ENTER_YOUR_GMAIL_ACCOUNT"
EMAIL_PASSWORD = 'ENTER_YOUR_PASSWORD'
with smtplib.SMTP('smtp.gmail.com',587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
msg=f'Subject: {subject}\n\n{message}'
smtp.sendmail(EMAIL_ADDRESS, to_address, msg)
return True
def unique_emp_id():
return random.randint(1111,9999)
def saving_file(emp_id):
with open('C:/Users/ashwi/Desktop/dbms pec/emp_id.txt', 'w') as file:
file.write(json.dumps(emp_id))
file.close()
return True
print("\n---------------------------------------------")
print("Welcome to Employee Managment System")
print("---------------------------------------------\n")
print("1. Admin Login")
print("2. Employee Login\n")
print("----------------")
choice_main=int(input())
print("----------------")
if choice_main == 1:
username_admin = input("Enter Admin username:")
if username_admin == 'admin':
password_admin = input("Enter Admin Password:")
if password_admin == 'admin':
print()
print("------------------")
print("Login Successfull")
print("------------------")
print()
print()
print('''1. Create Employee
2. Search Employee
3. Reset password for Employee
4. Change employee details
5. Delete Employee
6. Logout
''')
print("--------------")
admin_choice = int(input())
print("--------------")
print()
if admin_choice == 1:
emp_name = input("Enter Employee Name:")
emp_gender = input("Enter Employee Gender (M/F):")
emp_mail = input("Enter Employee Mail ID:")
emp_mobile = input ("Enter Employee Mobile No:")
emp_dept = input("Enter Employee Department:")
emp_city = input("Enter Employee City:")
emp_salary = input("Enter Employee Salary:")
emp_pass = input("Enter Password:")
emp_id=[]
file=open('C:/Users/ashwi/Desktop/dbms pec/emp_id.txt', 'r')
emp_id=file.read()
emp_id=json.loads(emp_id)
file.close()
message = '''
Hello {},
You Profile was Successfully Created by our Admin, with the following Details.
Name: {}
Gender: {}
Mobile: {}
Department: {}
City: {}
Salary: {}
Your Portal Login Credentials,
Username: {}
Password: {}
If you find any mistake in the details contact your HR.
Regards,
Admin'''.format(emp_name,emp_name,emp_gender,emp_mobile,emp_dept,emp_city,emp_salary,emp_mail,emp_pass)
subject = 'Employee Profile Created!!'
if mail_function(emp_mail,message,subject):
unique_empid = unique_emp_id()
while unique_emp_id in emp_id:
unique_empid = unique_emp_id()
emp_id.append(unique_empid)
if saving_file(emp_id):
sql_command = "insert into employee_details values('{}',{},'{}','{}',{},'{}','{}',{},'{}');".format(emp_name,int(unique_empid),emp_gender,emp_mail,int(emp_mobile),emp_city,emp_dept,int(emp_salary),emp_pass)
mycursor.execute(sql_command)
mydb.commit()
print()
print("Employee Sucessfully Created with Employee ID:{}".format(unique_empid))
elif admin_choice == 2:
emp_id=[]
file=open('C:/Users/ashwi/Desktop/dbms pec/emp_id.txt', 'r')
emp_id=file.read()
emp_id=json.loads(emp_id)
file.close()
emp_id_input = int(input("Enter Employee ID:"))
if emp_id_input in emp_id:
sql_command = "select name, employee_id, gender, email, city, mobile, dept, salary from employee_details where employee_id = {} ".format(emp_id_input)
mycursor.execute(sql_command)
lst = []
for i in mycursor:
for j in i:
lst.append(j)
print()
print("Name: {}".format(lst[0]))
print("Employee ID: {}".format(lst[1]))
print("Gender: {}".format(lst[2]))
print("Email: {}".format(lst[3]))
print("City: {}".format(lst[4]))
print("Mobile No: {}".format(lst[5]))
print("Department: {}".format(lst[6]))
print("Salary: {}".format(lst[7]))
print()
else:
print()
print("Employee ID Does Not Exists ")
elif admin_choice == 3:
emp_id_input = int(input("Enter Employee ID:"))
new_emp_pass = input("Enter New Password:")
confirm_new_pass = input("Confirm Password:")
if new_emp_pass == confirm_new_pass:
sql_command = "update employee_details set password = '{}' where employee_id = {}".format(confirm_new_pass,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
subject = 'Employee Profile Password Reset Successfull!!'
message = '''
Hello Employee,
You profile Password has been reset by our admin as per your request.
New Password = {}
We Request you to change your password afer login.
Regards,
Admin'''.format(confirm_new_pass)
print()
if mail_function(emp_mail,message,subject):
print("Password Changed Successfully")
else:
print("Password Not matched. Try Again!!")
elif admin_choice == 4:
print('''1. Name
2. City
3. Mobile
4. Email
5. Department
6. Salary''')
print('------------')
choice_user = int(input())
print('------------')
emp_id_input = int(input("Enter Employee ID:"))
if choice_user == 1:
emp_name = input("Enter New Name:")
sql_command = "update employee_details set name = '{}' where employee_id = {} ".format(emp_name,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif choice_user == 2:
emp_city= input("Enter New City:")
sql_command = "update employee_details set city= '{}' where employee_id = {}".format(emp_city,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif choice_user == 3:
emp_mob = input("Enter New Mobile No:")
sql_command = "update employee_details set mobile= '{}' where employee_id = {}".format(emp_mob,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif choice_user == 4:
emp_mail = input("Enter New Mail ID:")
sql_command = "update employee_details set email = '{}' where employee_id = {}".format(emp_mail,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif choice_user == 5:
emp_dept = input("Enter New Department:")
sql_command = "update employee_details set dept = '{}' where employee_id = {}".format(emp_dept,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif choice_user == 6:
emp_sal = input("Enter New Salary:")
sql_command = "update employee_details set salary = '{}' where employee_id = {}".format(emp_sal,emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
sql_command1 = "select email from employee_details where employee_id = {}".format(emp_id_input)
mycursor.execute(sql_command1)
emp_mail = ''
for i in mycursor:
for j in i:
emp_mail=j
message = '''
Your Details Have been Updated,
Login to the portal and check your new details
Regards,
Admin'''
subject = 'Employee Details Changed'
if mail_function(emp_mail,message,subject):
print("Updated Successfully")
elif admin_choice == 5:
emp_id=[]
file=open('C:/Users/ashwi/Desktop/dbms pec/emp_id.txt', 'r')
emp_id=file.read()
emp_id=json.loads(emp_id)
file.close()
emp_id_input = int(input("Enter Employee ID:"))
if emp_id_input in emp_id:
sql_command ="DELETE FROM employee_details WHERE employee_id={};".format(emp_id_input)
mycursor.execute(sql_command)
mydb.commit()
emp_id.remove(emp_id_input)
if saving_file(emp_id):
print("Employee Deleted Successfully")
else:
print("Employee ID Does'nt Exists")
elif admin_choice == 6:
exit()
else:
print("Incorrect Choice!!")
else:
print("Wrong Password")
else:
print("Wrong Username")
elif choice_main == 2:
username = input("Enter Username:")
password = input("Enter Password:")
sql_command = "select email from employee_details; "
mycursor.execute(sql_command)
for i in mycursor:
if username in i:
sql_command = "select password from employee_details where email = '{}'".format(username)
mycursor.execute(sql_command)
emp_pass = ""
for i in mycursor:
for j in i:
emp_pass = j
if emp_pass == password:
print()
print("Login Successfull")
sql_command = "select name, employee_id, gender, email, city, mobile, dept, salary from employee_details where email = '{}' ".format(username)
mycursor.execute(sql_command)
lst = []
for i in mycursor:
for j in i:
lst.append(j)
print()
print("Welcome {}".format(lst[0]))
print("-----------------------------------")
print("Employee Details")
print("-----------------------------------")
print("Name: {}".format(lst[0]))
print("Employee ID: {}".format(lst[1]))
print("Gender: {}".format(lst[2]))
print("Email: {}".format(lst[3]))
print("City: {}".format(lst[4]))
print("Mobile No: {}".format(lst[5]))
print("Department: {}".format(lst[6]))
print("Salary: {}".format(lst[7]))
print("------x----------x------------x----")
print()
else:
print("Password Incorrect.")
exit()
else:
print("Username Incorrect")
exit()
print('''
1. Modify Details
2. Change Password
3. Logout
''')
choice_user_emp = int(input())
if choice_user_emp == 1:
print('''
1. Modilfy email
2. Modify Mobile ''')
user_choice = int(input())
if user_choice == 1:
emp_mail = input("Enter New Mail ID:")
sql_command = "update employee_details set email = '{}' where email = '{}'".format(emp_mail,username)
mycursor.execute(sql_command)
mydb.commit()
print("Updated Successfully")
elif user_choice == 2:
emp_mob = int(input("Enter New Mobile:"))
sql_command = "update employee_details set mobile = {} where email = '{}'".format(emp_mob,username)
mycursor.execute(sql_command)
mydb.commit()
print("Updated Successfully")
else:
print("Incorrect Choice")
elif choice_user_emp == 2:
new_emp_pass = input("Enter New Password:")
confirm_new_pass = input("Confirm Password:")
if new_emp_pass == confirm_new_pass:
sql_command = "update employee_details set password = '{}' where email = '{}'".format(confirm_new_pass,username)
mycursor.execute(sql_command)
mydb.commit()
subject = 'Employee Profile Password Change Successfull!!'
message = '''
Hello Employee,
You profile Password has been Changed.
New Password = {}
Regards,
Admin'''.format(confirm_new_pass)
print()
if mail_function(username,message,subject):
print("Password Changed Successfully")
elif choice_user_emp == 3:
exit()
else:
print("Incorrect Choice")