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