Exemplo n.º 1
0
def login():
    """Log user into site"""

    # If user is already logged in, return to home screen:
    if session.get('user_id') != None:
        return redirect('/')

    # If reached via POST by submitting login form:
    if request.method == 'POST':

        # Get input from login form:
        username = request.form.get('username')
        password = request.form.get('password')

        # Check that login has been filled out:
        if not username or not password:
            flash('Please enter username AND password to Log in!')
            return render_template('login.html')

        # Query database for username:
        user_info = User.query.filter_by(username=username).first()

        # Check username exists and password is correct:
        if not user_info or not check_password_hash(user_info.pass_hash,
                                                    password):
            flash('Invalid username and/or password! Please try again!')
            return render_template('login.html')

        # Otherwise load user session and redirect to homepage:
        load_user(user_info, session)

        #flash('Log in Successful! Welcome back to Flack Teams!')
        return redirect('/')

    # If User reaches Route via GET (e.g. clicking login link):
    else:
        return render_template('login.html')
Exemplo n.º 2
0
def send_message(_cursor, user, to_user, message_text):
    """
    Sends message using Message-class methods. Launched by main().

    :param _cursor: parameter passed with connector decorator
    :param user: User class object , passed by main()
    :param to_user: recipient user id, string type
    :param message_text: message text, string type
    :return: function prints success statement if message is sent
    """
    recipient = load_user(id=to_user)
    if not recipient:
        print('Recipient ID not found, please check and try again')
        return
    new_message = Message()
    new_message.to_id = recipient.id
    new_message.text = message_text
    new_message.from_id = user.id
    new_message.save_to_db(_cursor)
    print('Message sent!')
Exemplo n.º 3
0
def register():
    """Register user for the website"""

    # If user is already logged in, return to home screen:
    if session.get('user_id') != None:
        return redirect('/')

    # If reached via POST by submitting form - try to register new user:
    if request.method == 'POST':

        # Get input from registration form:
        username = request.form.get('username')
        screen_name = request.form.get('screenname')
        password = request.form.get('password')
        confirm = request.form.get('confirmation')
        profile_img = request.form.get('profile')
        file = None

        # If form is incomplete, return and flash apology:
        if not all([username, screen_name, password, confirm, profile_img]):
            flash('Please fill in all fields to register!')
            return render_template('register.html')

        # If password and confirmation do not match, return and flash apology:
        elif password != confirm:
            flash('Password and confirmation did not match! Please try again.')
            return render_template('register.html')

        # Ensure password meets password requirements:
        elif not validate_pass(password):
            flash(
                'Password must be eight characters long with at least one number and one letter!'
            )
            return render_template('register.html')

        # If any input is just whitespace chars, ask for new input:
        if is_whitespace(username) or is_whitespace(
                screen_name) or is_whitespace(password):
            flash('Please fill in all fields to register!')
            return render_template('register.html')

        # Check that file is uploaded if own profile img selected:
        if profile_img == 'user_upload':

            result = check_img_upload()

            if not result[0]:
                flash(result[1])
                return render_template('register.html')
            else:
                file = result[1]

        # Otherwise information from registration is complete
        # Check username does not already exist, if it does then ask for a different name:
        user_query = User.query.filter_by(username=username).first()

        if user_query:
            flash(
                'Sorry but that username is already in use, please pick a different username!'
            )
            return render_template('register.html')

        # Otherwise add user to database using hashed password:
        pass_hash = generate_password_hash(password)

        # Add new user to users table:
        new_user = User(username=username,
                        screen_name=screen_name,
                        pass_hash=pass_hash,
                        profile_img=profile_img)
        db.session.add(new_user)
        db.session.commit()

        # Put unique user ID and username into session:
        user_info = User.query.filter_by(username=username).first()
        load_user(user_info, session)

        # If user uploaded a custom image file, add its path to DB, and save in Images folder:
        if file:
            save_user_img(file, app)
            user_info.profile_img = session['profile_img']
            db.session.commit()

        # Go to main chat page
        return redirect('/')

    # If User reaches Route via GET (e.g. clicking registration link):
    else:
        return render_template('register.html')
Exemplo n.º 4
0
def main(parser):
    """
    Main function of program. Collects all arguments from parser parameter.
    Then loads User class object with given username.

    Scenarios:
        1. --username , --password are only given:
            a) Save new user - launches save_new_user() if load_user() returns None (no user with given username in DB)
            b) If user is in DB - print statement informing that more arguments have to be passed.
        2. --username, --password, --edit, --newpass are given:
            a) Changes password - launches change_password() only if logging_user() is successful.
            b) Returns None if logging has failed, logging_user() prints fail reason.
        3. --username, --password, --delete are given:
            a) Deletes user - launches delete_password() only if logging_user() is successful.
            b) Returns None if logging has failed, logging_user() prints fail reason.
        4. --list is given:
            Launches load_all_users_in_db() which prints all users in DB
        5. Else scenario:
            In any other case - function prints --help

    :param parser: ArgumentParser class. Created in set_parser_arguments()
    :return: function has no return
    """
    args = parser.parse_args()
    username = args.username
    password = args.password
    new_pass = args.newpass
    users_list = args.list
    delete = args.delete
    edit = args.edit

    user = load_user(username=username)

    # Scenario no. 1
    if args_required(username, password) and args_to_be_empty(
            new_pass, users_list, delete, edit):
        if not user:
            return save_new_user(username, password)
        else:
            print('Please add arguments, your query is empty')
            return

    # Scenario no. 2
    elif args_required(username, password, edit,
                       new_pass) and args_to_be_empty(delete, users_list):
        if logging_user(user, password):
            return change_password(user, new_pass)
        else:
            return

    # Scenario no. 3
    elif args_required(username, password, delete) and args_to_be_empty(
            new_pass, users_list, edit):
        if logging_user(user, password):
            return delete_user(user)
        else:
            return

    # Scenario no. 4
    elif args_required(users_list) and args_to_be_empty(
            username, password, delete, edit, new_pass):
        return load_all_users_in_db()

    # Scenario no. 5
    else:
        print(
            """You have used wrong arguments combination. See below scenarios:
        -u USERNAME -p PASSWORD | creates new user
        -u USERNAME -p PASSWORD -n NEWPASS -e | sets new password
        -u USERNAME -p PASSWORD -d | deletes user
        -l | prints all users
        For more see below""")
        return parser.print_help()
Exemplo n.º 5
0
def main(parser):
    """
    Main function of program. Collects all arguments from parser parameter.
    Then loads User class object with given username.

    Shield conditions:
        1. Checks if given username is in DB, if not user is None
        2. Try to log in user. Stops function if false (logging_user() prints fail statements)

    Scenarios:
        1. --username , --password -l are given:
            List all messages which were sent by user and messages which he received (and did not delete)
        2. --username, --password, --to, --send are given:
            Sends message to targer user
        3. --username, --password, --delete are given:
            Deletes message
        4. Else scenario:
            In any other case - function prints --help


    :param parser: ArgumentParser class. Created in set_parser_arguments()
    :return: depends on scenario - returns requested function
    """
    args = parser.parse_args()
    username = args.username
    password = args.password
    messages_list = args.list
    to_user = args.to
    message_text = args.send
    delete = args.delete

    user = load_user(username)

    # Shield conditions
    if not user:
        print('Invalid login')
        return
    if logging_user(user, password) is False:
        return

    # Scenario no. 1
    if args_required(username, password, messages_list) and args_to_be_empty(
            to_user, message_text, delete):
        return load_user_messages(user)

    # Scenario no. 2
    elif args_required(username, password, to_user,
                       message_text) and args_to_be_empty(
                           messages_list, delete):
        return send_message(user, to_user, message_text)

    # Scenario no. 3
    elif args_required(username, password, delete) and args_to_be_empty(
            messages_list, to_user, message_text):
        return delete_message(user, delete)

    # Scenario no. 4
    else:
        print(
            """You have used wrong arguments combination. See below scenarios:
        -u USERNAME -p PASSWORD -l | lists all messages, sent and received
        -u USERNAME -p PASSWORD -to TO -s SEND| sends new message -to target user id with -s message text
        -u USERNAME -p PASSWORD -d | deletes message with ID passed in -d argument
        For more see below""")
        return parser.print_help()