def send(): form = SendForm(request.form) if request.method == 'POST' and form.validate(): conn = get_connection() if conn: cursor = conn.cursor() sender_id = form.sender.data receiver_id = form.receiver.data sender = User.load_user_by_id(cursor, sender_id) if check_password(form.password.data, sender.hashed_password): message = Message() message.to_id = receiver_id message.from_id = sender_id message.text = form.message.data message.creation_date = datetime.today().strftime( "%Y-%m-%d %H:%M:%S") message.save_to_db(cursor) conn.commit() else: flash("Błąd hasła!") cursor.close() conn.close() return redirect('/user/{}'.format(sender_id)) return render_template('send_message.html', form=form)
def message(cursor, user, recipient, text): nm = Message() nm.from_id = user nm.to_id = recipient nm.text = text nm.creation_date = datetime.now() nm.save_to_db(cursor)
def send_message(self, cursor, username, password, text, to_id) -> Message: user = self.login_user(cursor, username, password) if user: message = Message() message.from_id = user.id message.to_id = to_id message.text = text message.creation_date = datetime.now() message.save(cursor)
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 sent_to(username, password, toUserName, bodyToUser, creation_date): print("SEND A MESSAGE TO A GIVEN USER") c = get_connection() cursor = c.cursor() u = User() u.set_password(password, "1") if u.get_item_by_login(username, cursor) and u.get_user_by_password( username, cursor)[1] == u.hashed_password and u.get_item_by_login( toUserName, cursor)[0]: m = Message() m.body = bodyToUser m.creation_date = creation_date m.from_user = u.get_user_by_password(username, cursor)[0] m.to_user = u.get_user_by_password(toUserName, cursor)[0] m.save_message(cursor) c.close() else: print( "Nie ma takiego usera lub podałeś złe hasło/login lub nie ma usera do ktorego chcesz cos wyslac" )
if args.user and args.password: print(f"Podany login: {args.user}, podane hasło to: {args.password}") from_user = User.load_user_by_username(cursor, args.user) if check_password(args.password, from_user.hashed_password): print("Popawne hasło") if args.to: to_user = User.load_user_by_username(cursor, args.to) print('Adresat znajduje sie w bazie danych') if to_user and args.send: message = Message() message.from_id = from_user.id message.to_id = to_user.id message.text = args.send message.creation_date = datetime.datetime.today() message.save_to_db(cursor) print(f'Wysłano wiadomość do użytkownika {args.to}') if not to_user: print('Niepoprawny login adresata') if not args.send: print('Komunikat musi zawierać treśc') elif args.list: loaded_user = User.load_user_by_username(cursor, args.user) messages = Message.load_all_messages_for_user( cursor, loaded_user.id) print("ID: \t Od: \t Treść: \t Data:") for message in messages: print(message.id, '\t', message.from_id, '\t', message.text, '\t', message.creation_date)
else: raise Exception("Podaj login i hasło.") # Nadawanie komunikatu elif args.send: if args.username and args.password: if args.to: to_id = Message.get_user_id_by_username(cursor, args.to) if User.load_user_by_id(cursor, to_id): from_id = Message.get_user_id_by_username( cursor, args.username) m = Message() m.from_id = from_id m.to_id = to_id m.text = args.send m.creation_date = datetime.now().strftime( "%Y-%m-%d %H:%M:%S") m.save_to_db(cursor) conn.commit() print("Wysłano komunikat.") else: raise Exception("Nie ma takiego użytkownika!") else: raise Exception("Nie podano adresata!") else: raise Exception("Błąd użytkownika lub hasła!") else: raise Exception("Nie podano komunikatu!") cursor.close() conn.close()
for l in list_of_messages_to_user: print(l) else: print("Provided Username or password is incorrect") elif (args.username and args.password) and args.to and args.send: u = User.load_user_by_email(cursor, args.username) t = User.load_user_by_email(cursor, args.to) from_id = u.id to_id = t.id if check_password(args.password, u.hashed_password): if t: new = Message() new.to_id = to_id new.from_id = from_id new.text = args.send new.creation_date = datetime.datetime.now() new.save_to_db(cursor) cnx.commit() else: print("The reciepient does not exist ") else: print("Provided Username or password is incorrect") elif (args.username and args.password) and args.to and not args.send: print("The message has not been provided.") else: print( "Please type the following command to check all available options:\n python3 message_management.py --help" ) cursor.close() cnx.close()
def solution(options): # -u -p -l parameters (List all messages for this user) if options.username and options.password and options.list and not options.send and not options.to: # Check login and pass user_collection = [] try: user_collection = User.load_all_ids_usernames(cnx_cursor[1]) except: print("Failed to load user data.") # Check login for item in user_collection: if options.username == item.username: # Load one user user_data = User.load_user_by_id(cnx_cursor[1], item.id) # Check if password is correct if models.hasher.check_password( options.password, user_data.hashed_password) is False: return "Wrong password." # Collect all messages message_collection = Message.load_all_messages_for_user( cnx_cursor[1], item.id) # Display print("### Messages for you:") for element in message_collection: print("From:", element.from_user, "\nDate:", element.creation_date, "\nMessage:", element.message_content, "\n") return "###" return "No such user found." # -u -p -s -t parameters (Send message to specified user) elif options.username and options.password and options.send and not options.list: user_collection = [] try: user_collection = User.load_all_ids_usernames(cnx_cursor[1]) except: print("Failed to load user data.") # Check login for item in user_collection: if options.username == item.username: # Load one user user_data = User.load_user_by_id(cnx_cursor[1], item.id) # Check if password is correct if models.hasher.check_password( options.password, user_data.hashed_password) is False: return "Wrong password." # See if options.to is provided; if not, display no recipient. Likely to be redundant since parameters # are checked by the parser. if options.to is False: return "No recipient provided in argument -t" # See if recipient is in database for member in user_collection: if options.to == member.username: recipient_id = member.id # Check message length <= 255 if len(options.send) > 255: return "Message too long. Maximum is 255 characters." if len(options.send) < 1: return "Nothing to send. Please type message after parameter -s" # Send message new_mssg = Message() new_mssg.from_id = item.id new_mssg.to_id = recipient_id new_mssg.message_content = options.send new_mssg.creation_date = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') new_mssg.save_to_db(cnx_cursor[1]) return "Message sent." return "No recipient found in database." return "No such user found." else: return "No function for those parameters found. Try -u -p -s -t for sending or -u -p -l for listing messages."