def sign_up_validator(nick, firstname, surname, email, password, r_password): correct_nick = False correct_names = False correct_password = False correct_r_password = False correct_email = False nicks = [ nick[0] for nick in DB().get_data_from_db(query="SELECT nick from users") ] if nick not in nicks: correct_nick = True else: InfoBoxGUI().info_box(message='The entered data are incorrect.\n' ' Nickname already exist in database.') if firstname.isalpha() and surname.isalpha(): correct_names = True else: InfoBoxGUI().info_box( message='The entered data are incorrect.\n' 'Firstname and surname cannot contain characters other than letters.' ) if re.match( r"^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[@#$])[\w\d@#$]{6,12}$", password): correct_password = True else: InfoBoxGUI().info_box( message='The entered data are incorrect.\n' 'The password must contain an uppercase letter, number and special character. ' 'Password must be longer than 6 characters') if password == r_password: correct_r_password = True else: InfoBoxGUI().info_box(message='The entered data are incorrect.\n' 'The passwords are not the same.') if re.match(r"[^@]+@[^@]+\.[^@]+", email): correct_email = True else: InfoBoxGUI().info_box(message='The entered data are incorrect.\n' 'Email is incorrect.') if all([ correct_nick, correct_names, correct_password, correct_r_password, correct_email ]): return True
def name_checker(self): if self.entry_product.get() == '': message = "You must enter the name of the item." InfoBoxGUI().info_box(message) if self.entry_product.get() in self.name_of_products: message = "The product is already on the list." InfoBoxGUI().info_box(message) if len(self.entry_product.get()) > 60: message = "The name of product is too long." InfoBoxGUI().info_box(message) else: return True
def get_data_from_db(self, query): cursor = self.toss_db.cursor() try: cursor.execute(f"{query}") except ProgrammingError as e: error = f"Check query syntax! \n\n Error: {e}" InfoBoxGUI().error_box(error) except Exception as e: error = f"Something is wrong... \n\n {e}" InfoBoxGUI().error_box(error) else: result = cursor.fetchall() return result
def __init__(self): # TODO: eliminate keeping DB credentials in code try: self.toss_db = mysql.connector.connect(host='localhost', user='******', passwd='admin1234', database='tossdb') except (InterfaceError, ProgrammingError, AttributeError) as e: error = f"Probably config data are incorrect. \n\n Error: {e}" InfoBoxGUI().error_box(error) except Exception as e: error = f"Something went wrong during connecting with database. \n\n Error: {e}" InfoBoxGUI().error_box(error)
def execute_query(self, query): cursor = self.toss_db.cursor() try: cursor.execute(f"{query}") except ProgrammingError as e: error = f"Check query syntax! \n\n Error: {e}" InfoBoxGUI().error_box(error) except Exception as e: error = f"Something is wrong... \n\n {e}" InfoBoxGUI().error_box(error) else: self.toss_db.commit() cursor.close()
def click_next_button(self): """This module validates the inputs, set a list_name and starts the AddProductsGUI""" if self.list_name.get() == '': message = "The list name field can't be empty" InfoBoxGUI().info_box(message) list_name = self.list_name.get() query = f"SELECT tittle from lists WHERE owner = '{User.current_logged.nickname}'" names_of_lists = [ list_name[0] for list_name in DB().get_data_from_db(query) ] if list_name in names_of_lists: message = "A list with that name already exist. \n Name the list in a different way." InfoBoxGUI().info_box(message) else: ListCreatorGUI.list_name = self.list_name.get() self.root.destroy() ListCreatorGUI.ProductsInserterGUI().start()
def price_checker(self): try: float(self.entry_price.get()) except (ValueError, TypeError): msg = "Price must be float \n\n Use dot, not comma." InfoBoxGUI().info_box(msg) else: return True
def number_checker(self): try: int(self.entry_num_of_products.get()) except ValueError as e: msg = "Number of item must be integer." InfoBoxGUI().info_box(msg) else: return True
def click_add_friend_button(self): nick = self.entry_friends.get() if ListCreatorGUI.FriendsInserterGUI( ).check_if_friend_exist_in_database(nick): self.list_of_friends.append(nick) self.init_listbox() else: InfoBoxGUI().error_box( 'User with this nickname does not exist!')
def get_user_from_db(self, nick): query = f"SELECT * FROM users WHERE nick=\'{nick}\'" cursor = self.toss_db.cursor() try: cursor.execute(f"{query}") except ProgrammingError as e: error = f"Check query syntax! \n\n Error: {e}" InfoBoxGUI().error_box(error) except Exception as e: error = f"Something is wrong... \n\n {e}" InfoBoxGUI().error_box(error) else: result = cursor.fetchall() user_id = result[0][0] nick = result[0][1] firstname = result[0][2] surname = result[0][3] email = result[0][4] return User(user_id, nick, firstname, surname, email)
def add_data_to_relations_table(self): # preparing and insert data to relations table ListCreatorGUI.list_of_friends.append(User.current_logged.nickname) for friend in ListCreatorGUI.list_of_friends: try: query = f"SELECT userid FROM users WHERE nick = '{friend}'" user_id = DB().get_data_from_db(query)[0][0] query = f"INSERT INTO relations VALUES (NULL, {user_id}, {int(ListCreatorGUI.id_list_of_products)})" DB().execute_query(query) except IndexError as e: error = f"{e} \n\n Probably user '{friend}' doesn't exists in database " InfoBoxGUI().error_box(error)
def click_delete(self): # TODO: Depending on the service, maybe only the owner should be able to delete the list? title = 'Delete list' question = 'Are you sure you want to delete the list?' if InfoBoxGUI().askyesno(title, question): query = f"DELETE FROM relations WHERE list_id={MainMenuGUI.current_list_id}" DB().execute_query(query) query = f"DELETE FROM items WHERE list_id={MainMenuGUI.current_list_id}" DB().execute_query(query) query = f"DELETE FROM lists WHERE list_id={MainMenuGUI.current_list_id}" DB().execute_query(query) self.root.destroy() MainMenuGUI().start()
def send_msg(self, subject, body, recipient): # TODO: eliminate keeping mail credentials in code msg = EmailMessage() msg['Subject'] = subject msg['From'] = '*****@*****.**' msg['To'] = f'{recipient}' msg.set_content(f'{body}') try: with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login('*****@*****.**', 'ovoneL09') smtp.send_message(msg) except Exception as e: print(e) print(msg['To']) error = f"Mail could not be sent. \n Check internet connection. \n\n Error: {e}" InfoBoxGUI().error_box(error)
def login_validator(login, password): correct_login = False correct_password = False nicks = [ nick[0] for nick in DB().get_data_from_db(query="SELECT nick FROM users") ] if login in nicks: correct_login = True password_db = DB().get_data_from_db( query=f"SELECT password FROM users WHERE nick = '{login}'") if password == password_db[0][0]: correct_password = True if all([correct_login, correct_password]): return True else: InfoBoxGUI().info_box('Login or password is incorrect.')