· 6 years ago · Nov 03, 2019, 08:42 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("Connection to database was lost. Please run the application again." + 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 #checks for unecessary space.
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 #checks for characters that are not an alphabet.
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 #checks for blank spaces
105 if not L_Name:
106 L_Name = input ("Last name cannot be blank. Please re-enter your last name").lower()
107
108 continue
109 #checks for unecessary space.
110 if not L_Name.isalpha():
111 if space in L_Name:
112 L_Name = input("The following contains an unecessary space. Please try again: ").lower()
113
114 #checks for characters that are not an alphabet.
115 else:
116 L_Name = input("The following contains characters not in the traditional alphabet. Please try again: ").lower()
117
118 continue
119 if not L_Name[0].isupper():
120 L_Name = L_Name.capitalize()
121 print(L_Name)
122 continue
123
124 email = "@gmail.com"
125 userEmail = input("Please enter email: ")
126 while not userEmail or not email in userEmail:
127 #checks if user input is blank
128 if not userEmail:
129 userEmail = input("Email cannot be blank. Please enter your email to login in: ")
130 continue
131
132 #checks if user inputs a domain for email
133 if not email in userEmail:
134 userEmail = input("The following includes an invalid domain, please try again: ")
135 continue
136
137 regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
138 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: ")
139 while not Password or len(Password) < 8 or Password.isalpha() or not hasNumbers(Password) or regex.search(Password) == None:
140 if not Password: #checks if password is blank
141 Password = input ("Password cannot be blank. Please re-enter an adjusted password: ")
142 continue
143 if len(Password) < 8: #checks if password is less than 8 chracters long
144 Password = input ("Password is not of correct length. Please re-enter an adjusted password: ")
145 continue
146 if not hasNumbers(Password): #checks if password contains at least one number
147 Password = input("Password does not contain any numbers. Please re-enter an adjusted password: ")
148 continue
149 if(regex.search(Password) == None): #checks if password contains at least one special character
150 Password = input("Password does not contain any special characters. Please re-enter an adjusted password: ")
151 continue
152
153 # Password Confirmation
154 Password1 = input("Please verify your new password: ")
155 while (Password != Password1):
156 Password1= input("Passwords do not match. Please try again: ")
157 print(Password + " " + Password1 + " Password Accepted ")
158
159 with conn:
160 cur = conn.cursor()
161 try:
162 InsertValue = "INSERT INTO Employee VALUES ('{}','{}','{}','{}','{}')"
163 InsertString = InsertValue.format(EmpID,F_Name,L_Name,userEmail,Password)
164 cur.execute(InsertString)
165 cur.execute("SELECT*FROM Employee WHERE(EmployeeID == '{}')".format(EmpID))
166 #order of argument matters, does not know the difference between last name or employeeID.
167 results = cur.fetchone()
168 print(results)
169 except Exception as e:
170 print("Connection to database was lost. Please run the application again." + str(e))
171
172 Registration()
173
174Main_Menu()