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