コード例 #1
0
def register_post():
    """
    This function collects all of the necessary field data,
    validates the entered data to some standard, then uses
    the backend registration calls to save the new user to
    our database or requests valid data.
    """
    # All of the inforamtion used to register a user gotten from the form
    email = request.form.get('email')
    name = request.form.get('name')
    password = request.form.get('password')
    password2 = request.form.get('password2')
    error_message = None

    # The users name with out spaces, to be used for validation
    nameNoSpace = name.replace(" ", "")

    '''
    A series of conditionals checking if the user's registration input is
    valid or not.
    If it is not an accurate error message will be displayed.
    If it is valid the user will be registered and redirected to the login page.
    '''

    # Check if passwords match
    if password != password2:
        error_message = "Passwords format is incorrect"
    # Check for valid email
    elif not valid.validate_email_address(email):
        error_message = "Email format is incorrect"
    # Check for valid password
    elif not valid.validate_password(password):
        error_message = "Password format is incorrect"
    # Check that the length of the name is proper length
    elif len(name) > 19 or len(name)<3:
        error_message = "Username format is incorrect"
    # Check that there are no non alphanumeric characters other then space
    elif not nameNoSpace.isalnum():
        error_message = "Username format is incorrect"
    # Check that there is no space at begining or end
    elif name[0] == ' ' or name[len(name) - 1] == ' ':
        error_message = "Username format is incorrect"
    else:
        user = usr.get_user(email)
        # Check if email has already been used
        if user:
            error_message = "This email has ALREADY been used"
        # Register User
        elif usr.register_user(email, name, password, password2, 500000):
            error_message = "Failed to store user info."

    # if there is any error messages when registering new user
    # at the backend, go back to the register page.
    if error_message:
        return render_template('register.html', message=error_message)
    else:
        return redirect('/login')
コード例 #2
0
    def wrapped_inner():

        # check did we store the key in the session
        if 'logged_in' in session:
            email = session['logged_in']
            user = usr.get_user(email)
            if user:
                # if the user exists, call the inner_function
                # with user as parameter
                return inner_function(user)
        else:
            # else, redirect to the login page
            return redirect('/login')
コード例 #3
0
def buy_post():
    ticket_name = request.form.get('ticket_name')  # Set ticket name from form
    email = session['logged_in']  # Set email from logged in session
    quantity = request.form.get('quantity')  # Set quanity from from
    errors = []  # Create an empty arry to store errors
    buy_msg = 'Failed to buy the ticket(s): '  # Set buy_msg to a failed message

    user = usr.get_user(email)
    balance = user.balance / 100

    # Check that the ticket name meets requirements
    if not valid.validate_name(ticket_name):
        errors.append(
            "The name of the ticket must be no more than 60 characters using alphanumeric characters with no spaces at the begining and end"
        )
    # Check that the quantity is valid
    if not quantity.isdigit():
        errors.append("Quantity must be entered using 0-9")
    elif not valid.validate_quantity(quantity) or int(quantity) == 0:
        errors.append("You may only buy between 0 and 100 tickets inclusive")

    # If no errors in input
    if len(errors) == 0:

        # Try to buy the ticket
        ticket = tic.buy_ticket(ticket_name, quantity, balance, email)

        # If successfully bought change buy msg to show that
        if ticket is None:
            buy_msg = 'Successfully bought the ticket(s).'

        # If failed add to errors
        else:
            errors.append(ticket)
            buy_msg = buy_msg + str(ticket)

    # If there are errors in input create message
    elif len(errors) > 0:
        buy_msg += ", ".join(
            errors) + "."  #adding all of the errors to the update message

    resp = make_response(redirect('/', code=303))
    resp.set_cookie('buy_msg', buy_msg)
    return resp
コード例 #4
0
def test_user_get_user_invalid(self, *_):
    test_email = "*****@*****.**"            ## Test a non existant email
    assert users.get_user(test_email) is None       ## Validate that get user returns None
コード例 #5
0
def test_user_get_user_valid(self, *_):
    test_email = "*****@*****.**"           ## Create the test email
    assert users.get_user(test_email) is test_user  ## Validate that get user returns the user