· 6 years ago · Nov 03, 2019, 08:38 PM
1import sqlite3
2import re
3conn = sqlite3.connect("OS_Employee.db")
4
5def login():
6 print("Employee Login")
7 email = "@gmail.com"
8 userEmail = input("Please enter your email to login in: ")
9 #checks if user inputs a blank email
10 while not userEmail:
11 userEmail = input("Email cannot be blank. Please enter your email to login in: ")
12
13 #checks if user inputs a domain for email
14 while not email in userEmail:
15 userEmail = input("The following includes an invalid domain, please try again: ")
16 userPassword = input("Please enter your password: ")
17 #checks if user inputs a blank password
18 while not userPassword:
19 userPassword = input("Password cannot be blank. Please enter your password to login: ")
20 with conn:
21 cur = conn.cursor()
22 try:
23 cur.execute("SELECT COUNT (*) FROM Employee WHERE(Email = '" + userEmail +"' AND Password = '" + userPassword + "') ")
24 results = cur.fetchone()
25 if results[0]==1:
26 print("Login successful!")
27 else:
28 print("Login Unsuccessful. Information does not match database. Please try again.")
29 except:
30 print ("Connection to database was lost. Please run the application again.")
31login()
32
33def checkUniqueEmployId(EmpID) :
34 with conn:
35 cur = conn.cursor()
36 try:
37 cur.execute("SELECT count(*) FROM Employee WHERE(EmployeeID == '{}')".format(EmpID))
38 results = cur.fetchone()
39 # Error checking 1: Tell user that the employee ID entered already exists.
40 if results[0]==1:
41 return False
42 return True
43 except Exception as e:
44 print("Server Connection Failed. Please re-load the system to log back in." + str(e))
45
46def hasNumbers(inputString):
47 return any(char.isdigit() for char in inputString)
48
49
50
51def Main_Menu():
52 def Registration():
53 login()
54 print("--------------------------------------------")
55 print("Register a New User")
56 EmpID = input("Please enter your employee ID number: ")
57 # Error checking 1: employee ID must not be blank.
58 while not checkUniqueEmployId(EmpID) or not EmpID or not EmpID.isdigit() or not len(EmpID) == 4:
59 if not checkUniqueEmployId(EmpID):
60 EmpID = input("Employee ID already taken, please try again: ")
61 continue
62 if not EmpID:
63 EmpID = input("ID cannot be blank, please try again: ")
64 continue
65 # Error checking 2: employee ID must be in numerical values only.
66 if not EmpID.isdigit():
67 EmpID = input("Employee ID must contain numbers only. Please try again: ")
68 continue
69 # Error checking 3: employee ID must be 4 digits long
70 if not len(EmpID) == 4:
71 if len(EmpID) < 4:
72 EmpID = input("Incorrect number of digits, please try again: ")
73 if len(EmpID) > 4:
74 EmpID = input("Incorrect number of digits, please try again: ")
75 continue
76
77 space = " "
78 F_Name = input("Please enter first name: ")
79 F_Name.lower()
80 while not F_Name or not F_Name.isalpha() or not F_Name[0].isupper():
81 #checks for blank spaces
82 if not F_Name:
83 F_Name = input ("First name cannot be blank. Please re-enter your first name: ").lower()
84
85 continue
86 #checksfor unecessary space and makes sure user inputs alphabet.
87 if not F_Name.isalpha():
88 if space in F_Name:
89 F_Name = input("The following contains an unecessary space. Please try again: ").lower()
90
91
92 else:
93 F_Name = input("The following contains characters not in the traditional alphabet. Please try again: ").lower()
94
95 continue
96 if not F_Name[0].isupper():
97 F_Name = F_Name.capitalize()
98 print(F_Name)
99 continue
100
101 L_Name = input("Please enter last name: ")
102 L_Name.lower()
103 while not L_Name or not L_Name.isalpha() or not L_Name[0].isupper():
104 if not L_Name:
105 L_Name = input ("Last name cannot be blank. Please re-enter your last name").lower()
106
107 continue
108 if not L_Name.isalpha():
109 if space in L_Name:
110 L_Name = input("The following contains an unecessary space. Please try again: ").lower()
111
112
113 else:
114 L_Name = input("The following contains characters not in the traditional alphabet. Please try again: ").lower()
115
116 continue
117 if not L_Name[0].isupper():
118 L_Name = L_Name.capitalize()
119 print(L_Name)
120 continue
121
122 email = "@gmail.com"
123 userEmail = input("Please enter email: ")
124 while not userEmail or not email in userEmail:
125 if not userEmail:
126 userEmail = input("Email cannot be blank. Please enter your email to login in: ")
127 continue
128
129 #checks if user inputs a domain for email
130 if not email in userEmail:
131 userEmail = input("The following includes an invalid domain, please try again: ")
132 continue
133
134 regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
135 Password = input("Please enter your new password. The password should have greater than 8 characters and at least one number, one capital letter, and a special character: ")
136 while not Password or len(Password) < 8 or Password.isalpha() or not hasNumbers(Password) or regex.search(Password) == None:
137 if not Password:
138 Password = input ("Password cannot be blank. Please re-enter an adjusted password: ")
139 continue
140 if len(Password) < 8:
141 Password = input ("Password is not of correct length. Please re-enter an adjusted password: ")
142 continue
143 if not hasNumbers(Password):
144 Password = input("Password does not contain any numbers. Please re-enter an adjusted password: ")
145 continue
146 if(regex.search(Password) == None):
147 Password = input("Password does not contain any special characters. Please re-enter an adjusted password: ")
148 continue
149
150 # Password Confirmation
151 Password1 = input("Please verify your new password: ")
152 while (Password != Password1):
153 Password1= input("Passwords do not match. Please try again: ")
154 print(Password + " " + Password1 + " Password Accepted ")
155
156 with conn:
157 cur = conn.cursor()
158 try:
159 InsertValue = "INSERT INTO Employee VALUES ('{}','{}','{}','{}','{}')"
160 InsertString = InsertValue.format(EmpID,F_Name,L_Name,userEmail,Password)
161 cur.execute(InsertString)
162 cur.execute("SELECT*FROM Employee WHERE(EmployeeID == '{}')".format(EmpID))
163 #order of argument matters, does not know the difference between last name or employeeID.
164 results = cur.fetchone()
165 print(results)
166 except Exception as e:
167 print("Server Connection Failed. Please re-load the system to log back in." + str(e))
168
169 Registration()
170
171Main_Menu()