def send_message(user_from, user_to_username, msg_text): user_to = User.load_user_by_name(user_to_username) if user_to: message = Message() message.from_id = user_from.id message.to_id = user_to.id message.msg_text = msg_text return message.save_to_db() raise ValueError(f"No user '{user_to_username}' in the database.")
_to = User.get_user_by_name(connection_to_db, send_to).get_user_id() messages = Message.get_all_messages(connection_to_db, user.get_user_id(), receiver_id=_to) for i, m in enumerate(messages): print(f" Wiadomość {i + 1}: {m}") else: print(f'Wiadomości wysłane przez użytkownika: {username}') messages = Message.get_all_messages(connection_to_db, user.get_user_id()) for i, m in enumerate(messages): print(f" Wiadomość {i + 1}: {m}") if send_to or message: if message and send_to: _to = User.get_user_by_name(connection_to_db, send_to) if _to: msg = Message(None, user.get_user_id(), _to.get_user_id(), message, 'now') msg.save_to_db(connection_to_db) print(f"Wiadomość: '{msg}' została wysłana") else: print(f"Użytkownik {send_to} nie istnieje") elif list_of_messages: pass else: print("Podaj adresata oraz treść wiadomości") else: print("nieprawidłowy login lub hasło")
for message in messages: print( f"From:{User.load_user_by_id(cur, message.from_id).username}\n Date: {message.creation_date}\n Message: {message.text}") else: print("Password incorrect") elif args.send: if args.username is not None and args.password is not None: user = User.load_user_by_email(cur, args.username) hashed = user.hashed_password if check_password(args.password, hashed): if args.to is not None: recipient = User.load_user_by_email(cur, args.to) if recipient is not None: if args.send is not None: new_message = Message() new_message.to_id = recipient.id new_message.from_id = user.id new_message.text = args.send new_message.creation_date = datetime.now() new_message.save_to_db(cur) print(f"You send a message to {recipient.username}") else: print("Please enter a message") else: print("There is no user with this mail") else: print("Password incorrect") else: parser.print_help()