Skip to content

Commit 60f6c83

Browse files
authored
Merge pull request #22 from Kouam23/main
Update CreateAccount.py with modifications at the (isUsernameTaken) b…
2 parents bdfd279 + f4abd0c commit 60f6c83

1 file changed

Lines changed: 180 additions & 1 deletion

File tree

CreateAccount.py

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ def __init__(self, widget):
2727
## METHOD THAT VERIFIES IF THE USERNAME THE USER IS ENTERING IS NOT YET TAKEN ##
2828
#########################################################################################
2929
def isUsernameTaken(self, username):
30+
31+
try:
32+
# Step 1: Establish a connection to the SQLite database named "usersInfo.db"
33+
conn = sqlite3.connect("usersInfo.db")
34+
35+
# Step 2: Create a cursor object to interact with the database
36+
cursor = conn.cursor()
37+
38+
# Step 3: Execute an SQL query to check if the username already exists in the Users table
39+
cursor.execute("SELECT Username FROM Users WHERE Username = ?", (username,))
40+
41+
# Step 4: Fetch the result of the query
42+
result = cursor.fetchone()
43+
44+
# Step 5: Close the database connection to free up resources
45+
conn.close()
46+
47+
# Step 6: Return True if the username exists, False otherwise
48+
return result is not None
49+
50+
except Exception as e:
51+
# Step 7: Handle any exceptions that might occur during the process and return False
52+
print(f"Error checking username existence: {str(e)}")
53+
return False
54+
3055
pass
3156

3257

@@ -35,25 +60,179 @@ def isUsernameTaken(self, username):
3560
########################################################################################
3661
def CheckInfo(self, username, email, password, age, phonenumber, location, gender):
3762
# Check if the information is in the correct format
63+
64+
try:
65+
# Step 1: Check if any of the required input fields is empty
66+
if (
67+
len(username) == 0
68+
or len(password) == 0
69+
or len(email) == 0
70+
or len(age) == 0
71+
or len(phonenumber) == 0
72+
or len(location) == 0
73+
or len(gender) == 0
74+
):
75+
return "Please fill in all input fields!"
76+
77+
# Step 2: Check if the username has at least 4 characters
78+
elif not len(username) >= 4:
79+
return "Username should have at least 4 characters!"
80+
81+
# Step 3: Check if the username is already taken
82+
elif self.isUsernameTaken(username):
83+
return "Username is already taken!"
84+
85+
# Step 4: Check if the email is in the correct format
86+
elif not re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email):
87+
return "Incorrect email!"
88+
89+
# Step 5: Check if the password length is at least 5 characters
90+
elif not len(password) >= 5:
91+
return "Password length must be at least 5 characters"
92+
93+
# Step 6: Check if the phone number consists of digits only
94+
elif not phonenumber.isdigit():
95+
return "Invalid phone number!"
96+
97+
# Step 7: Check if the age consists of digits only
98+
elif not age.isdigit():
99+
return "Invalid age!"
100+
101+
# Step 8: Check if the gender is either 'male' or 'female'
102+
elif gender.lower() not in ['male', 'female']:
103+
return "Gender should be Male or Female"
104+
105+
# Step 9: If all checks pass, return None (indicating successful validation)
106+
else:
107+
return None
108+
109+
except Exception as e:
110+
# Step 10: Handle any exceptions that might occur during the process and return an error message
111+
print(f"Error checking user info format: {str(e)}")
112+
return "An error occurred during user info validation."
113+
38114
pass
39115

40116
#############################################################################################
41117
## METHOD THAT STORES USER INFO IN THE DATABASE AFTER VALIDATION ##
42118
#############################################################################################
43119

44120
def storeInDataBase(self, username, email, password, age, phonenumber, location,gender, image_path):
121+
122+
try:
123+
# Step 1: Connect to the database
124+
self.conn = sqlite3.connect("usersInfo.db")
125+
self.cursor = self.conn.cursor()
126+
127+
# Step 2: Create the Users table if it does not exist
128+
self.cursor.execute(
129+
'''
130+
CREATE TABLE IF NOT EXISTS Users (
131+
UserId INTEGER PRIMARY KEY AUTOINCREMENT,
132+
Username TEXT,
133+
Email TEXT,
134+
Password TEXT,
135+
PhoneNumber TEXT,
136+
Age INTEGER,
137+
Location TEXT,
138+
Gender TEXT,
139+
Image BLOB -- Store the image.
140+
)
141+
'''
142+
)
143+
self.conn.commit()
144+
145+
# Step 3: Insert user information into the Users table
146+
self.cursor.execute(
147+
'''
148+
INSERT INTO Users(Username, Email, Password, PhoneNumber, Age, Location, Gender, Image)
149+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
150+
''', (username, email, password, phonenumber, age, location, gender, image_path)
151+
)
152+
self.conn.commit()
153+
154+
# Step 4: Close the database connection
155+
self.conn.close()
156+
157+
except Exception as e:
158+
# Step 5: Handle any exceptions that might occur during the process
159+
print(f"Error storing user info in the database: {str(e)}")
45160
pass
46161

47162
##############################################################################################
48163
## METHOD THAT ALLOWS USER TO UPLOAD AN IMAGE FOR HIS PROFILE ##
49164
##############################################################################################
50165

51166
def uploadImage(self):
167+
168+
try:
169+
# Step 1: Open a file dialog to select an image file
170+
file_dialog = QFileDialog()
171+
self.image_path, _ = file_dialog.getOpenFileName(self, 'Select Image', '', 'Image Files (*.jpg *.png *.jpeg)')
172+
173+
# Step 2: Check if an image was selected
174+
if self.image_path:
175+
# Step 3: Display the selected image in a label
176+
pixmap = QPixmap(self.image_path)
177+
self.Picture_label.setPixmap(pixmap)
178+
179+
# Optional: Scale the image to fit the label
180+
self.Picture_label.setScaledContents(True)
181+
182+
# Optional: Show the label with the selected image
183+
self.Picture_label.show()
184+
185+
except Exception as e:
186+
# Step 4: Handle any exceptions that might occur during the process
187+
print(f"Error uploading image: {str(e)}")
188+
52189
pass
53190

54191
####################################################################################################
55192
## THE FINAL SUBMIT BUTTON THAT VERIFIES ALL THE OTHER METHODS, THEN MOVES TO THE MAINPAGE ##
56193
####################################################################################################
57194

58195
def Submit(self):
59-
pass
196+
197+
try:
198+
# Step 1: Get user input from the input fields
199+
username = self.username_LineEdit.text()
200+
email = self.Email_LineEdit.text()
201+
password = self.Password_LineEdit.text()
202+
age = self.Age_LineEdit.text()
203+
phonenumber = self.PhoneNo_lineEdit.text()
204+
location = self.Location_LineEdit.text()
205+
gender = self.Gender_LineEdit.text()
206+
207+
# Step 2: Check the information format using the CheckInfo method
208+
error_message = self.CheckInfo(username, email, password, age, phonenumber, location, gender)
209+
210+
# Step 3: Display error message if there is any issue with the information format
211+
if error_message:
212+
self.error_label.setText(error_message)
213+
else:
214+
# Step 4: Check if an image path is available
215+
if hasattr(self, 'image_path'):
216+
# Step 5: Read the image file and convert it to binary
217+
with open(self.image_path, 'rb') as file:
218+
image_binary = file.read()
219+
220+
# Step 6: Store user information in the database using storeInDataBase method
221+
self.storeInDataBase(username, email, password, age, phonenumber, location, gender, image_binary)
222+
else:
223+
# Step 7: If no image is selected, store user information with a None image
224+
self.storeInDataBase(username, email, password, age, phonenumber, location, gender, None)
225+
226+
# Step 8: Create an instance of the MainScreen and set the user profile
227+
mainpage = MainScreen(username, email, password, age, phonenumber, location, gender)
228+
mainpage.setUserProfile()
229+
230+
# Step 9: Add the MainScreen to the widget and move to the MainScreen
231+
self.widget.addWidget(mainpage)
232+
self.widget.setCurrentIndex(self.widget.currentIndex() + 1)
233+
234+
except Exception as e:
235+
# Step 10: Handle any exceptions that might occur during the process
236+
print(f"Error in the Submit method: {str(e)}")
237+
238+
pass

0 commit comments

Comments
 (0)