def delete_user(self, user: User) -> None: connection = try_connect_db() cursor = get_cursor(connection) result = user.delete(cursor) cursor.close() connection.close() return result
def send_message(self, adress: User, sender: User, message_text: str) -> Message: """Create message to adress (User) to sender (User) into database.""" connection = try_connect_db() cursor = get_cursor(connection) from_user = User() to_user = User() from_user = from_user.load_by_email(cursor, sender) to_user = to_user.load_by_email(cursor, adress) if not to_user: print("User {} does not exist. Message cannot be sent.".format( adress)) cursor.close() connection.close() return False message = Message() message.from_id = from_user.id message.to_id = to_user.id message.text = message_text try: message.save(cursor) except Exception as err: print( 'Data not saved to database due to the error: {}'.format(err)) cursor.close() connection.close() return False cursor.close() connection.close() return True
def delete_user(username: User, password) -> None: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): user.delete(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
def login_user(username: str, hash_password: str) -> Union[User, None]: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) cursor.close() connection.close() if user and user.check_password(hash_password): return True else: raise WrongParameterError("Wrong login or password!")
def create_user(username: str, password: str) -> User: connection = create_connection() cursor = get_cursor(connection) if not User.get_by_username(cursor, username): user = User._create_user_object(username, password, username) user.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("This user already exist!")
def print_all_users() -> List[Union[User, None]]: """Print all users which are in database""" connection = create_connection() cursor = get_cursor(connection) all_users = User.get_all(cursor) cursor.close() connection.close() if all_users: return all_users else: raise WrongParameterError("There is no users!")
def change_password(username: User, password: str, new_password: str) -> None: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): user.set_password(new_password) user.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
def change_password(self, user: User, new_password: str) -> None: password_validator(new_password) if user.check_password(new_password): raise Exception("given password is the same as the previous one") connection = try_connect_db() cursor = get_cursor(connection) salt = generate_salt() user.set_password(new_password, salt) user.save(cursor) cursor.close() connection.close() return True
def list_messages_to_user(username, password) -> List[Union[Message, None]]: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): messages = Message.load_all_messages_for_user( cursor, user.username) cursor.close() connection.close() return messages else: raise WrongParameterError("Wrong login or password!")
def all_users_list(self) -> List[Union[User, None]]: """Print all users which are in database""" connection = try_connect_db() cursor = get_cursor(connection) user = User() all_users = user.load_all(cursor) cursor.close() connection.close() if all_users: print("All users:") for user in all_users: print(user.email) else: print("There are no any users in database")
def send_message(username, password, to_id, text) -> Message: """Create message to adress (User) to sender (User) into database.""" connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): message = Message() message._id = -1 message.from_id = user.id message.to_id = to_id message.tekst = text message.creation_date = datetime.now() message.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
def list_messages_to_user(self, user: User) -> List[Union[Message, None]]: """Return list of all messages in database for specific user""" connection = try_connect_db() cursor = get_cursor(connection) message = Message() all_messages = message.load_all(cursor) print("All messages to user {}:".format(user.email)) count = 0 for item in all_messages: if item.to_id == user.id: count += 1 print("Message from {} sent on {}: {}".format( user.load_by_id(cursor, item.from_id).email, item.creation_date, item.text)) if count == 0: print("Inbox empty!") cursor.close() connection.close()
def login_user(self, username: str, password: str) -> Union[User, None]: """: Args: username (str): user email as an username password (str): user password Returns: tab[User, int] res[None, -1] if not valid email res[None, -2] if not valid password / login failed res[None, 1] if given username (email) not in database res[User, 2] object class User when login succeeded """ res = [None, 0] if not email_validator(username): res[1] = -1 return res connection = try_connect_db() cursor = get_cursor(connection) user = User() user.email = username db_user = user.load_by_email(cursor, username) if db_user is None: print('User {} does not exist in database'.format(username)) cursor.close() connection.close() res[1] = 1 return res else: print('Username {} exists in database'.format(username)) valid = db_user.check_password(password) cursor.close() connection.close() if valid: print("Login succeeded") res[0] = db_user res[1] = 2 return res else: print("Password is incorrect. Login failed") res[1] = -2 return res
def create_user(self, new_username: str, password: str) -> User: if not email_validator(new_username): return False password_validator(password) connection = try_connect_db() cursor = get_cursor(connection) salt = generate_salt() user = User() user.email = new_username user.set_password(password, salt) try: user.save(cursor) cursor.close() connection.close() return True except Exception as err: print( 'Data not saved to database due to the error: {}'.format(err)) cursor.close() connection.close() return False
parser.add_argument('--delete', '-d', help='Delete user', action='store_true') parser.add_argument('--list', '-l', help='List of user or massages', action='store_true') parser.add_argument('--send', '-s', help='Send', action='store') parser.add_argument('--to', '-t', help='Address of message', action='store') if __name__ == '__main__': args = parser.parse_args() print(args) dispacher = Dispacher() salt = generate_salt() connection = create_connection() cursor = get_cursor(connection) option_handler = OptionsHandler(args.password, args.username, args.new_password, args.edit, args.delete, args.list, args.send, args.to) if option_handler.create_user: print(dispacher.create_user(args.username, args.password)) elif option_handler.list_all_users: print(dispacher.print_all_users()) elif option_handler.list_all_messages_for_user: print(dispacher.list_messages_to_user(args.username, args.password)) elif option_handler.change_password: print( dispacher.change_password(args.username, args.password, args.new_password))