Beispiel #1
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("feed"))
    form = RegistrationForm()
    if form.validate_on_submit():
        try:
            print("registering")
            user = User(
                username=form.username.data,
                email=form.email.data,
            )
            user.set_password(form.password.data)
            db.session.add(user)
            db.session.commit()
            flash("Congratulations, You Are Now A Registered User!")
            return redirect(url_for("login"))
        except:
            db.session.rollback()
            user = User(
                username=form.username.data,
                email=form.email.data,
                profile_picture_url=form.profile_picture_url.data,
            )
            user.set_password(form.password.data)
            db.session.add(user)
            db.session.commit()
            flash("Congratulations, You Are Now A Registered User!")
            return redirect(url_for("login"))
    return render_template("register.html", title="Register", form=form)
Beispiel #2
0
def register():
    """ The controller to handle incoming GET and POST requests to the `/register` URL of the Flask web server.

    1. Checks to see if the user is already logged-in and authenticated. 
        a. If so, returns a response object that redirects the client to the '/index' route.
            - Results from a GET request from an authenticated user.

        b. If the user is not already logged-in and authenticated, makes the `RegistrationForm()` created using Flask-WTF available to the `templates/register` view by passing the view and the form as parameters to Flask's built-in `render_template()` function.
            - Results from a GET request from an unauthenticated user.

    2. If a valid username/pw/email combo is submitted, then an HTTP request is made to the remote SQL database requesting to insert a new row into the Users table. 

    4. The user is redirected to the `login` view. 

    Parameters
    ----------
    param1 : string
        The first parameter is the URL being requested by the client.

    Returns
    -------
    str
        The register page generated by the Jinja2 template.
    """
    if current_user.is_authenticated:
        return redirect(url_for("feed"))
    form = RegistrationForm()
    if form.validate_on_submit():
        try:
            user = User(username=form.username.data, email=form.email.data)
            user.set_password(form.password.data)
            db.session.add(user)
            db.session.commit()
            flash("Congratulations, you are now a registered user!")
            return redirect(url_for("login"))
        except:
            flash("Sorry, there was an error registering your account!")
            return redirect(url_for("register"))
    return render_template("register.html", title="Register", form=form)
Beispiel #3
0
 def test_password_hashing(self):
     u = User(username="******")
     u.set_password("cat")
     self.assertFalse(u.check_password("dog"))
     self.assertTrue(u.check_password("cat"))