Exemplo n.º 1
0
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """
    form = RegistrationForm(request.form)
    if request.method == "POST":
        error = None
        if form.validate():
            username = request.form["username"]
            password = request.form["password"]
            db = get_db()
            if (db.execute("SELECT id FROM user WHERE username = ?",
                           (username, )).fetchone() is not None):
                error = f"User {username} is already registered."

            if error is None:
                # the name is available, store it in the database and go to
                # the login page
                db.execute(
                    "INSERT INTO user (username, password) VALUES (?, ?)",
                    (username, generate_password_hash(password)),
                )
                db.commit()
                return redirect(url_for("auth.login"))
        if error is not None:
            flash(error)

    return render_template("auth/register.html", form=form)
Exemplo n.º 2
0
def register():
    if (current_user.is_authenticated):
        return redirect(url_for('routes.home'))
    form = RegistrationForm(request.form)
    # If the user is trying to register
    if (form.validate_on_submit()):
        print('Register request')
        # Checks that the information in the form is valid
        valid = True
        if (check_for_dup_email(form.email.data)):
            valid = False
            flash('That email is already taken', 'email')
        if (check_for_dup_username(form.username.data)):
            valid = False
            flash('That username is already taken', 'username')
        print(valid)
        if (valid):
            try:
                user = User(first_name=form.firstname.data,
                            last_name=form.lastname.data,
                            email=form.email.data,
                            username=form.username.data,
                            pass_hash=generate_password_hash(
                                form.password.data))
                # Add the user account into the database
                db.session.add(user)
                db.session.commit()
                return redirect(url_for('routes.login'))
            except:
                return "There was an issue getting you registered"
        else:
            return render_template("register.html", form=form)
    # If the user is visiting the webpage
    else:
        return render_template("register.html", form=form)
Exemplo n.º 3
0
def register():
    """ Register user

    
    """
    form = RegistrationForm(request.form)
    if request.method == 'POST' and form.validate():
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']
        request_ip = request.remote_addr

        db = get_db()
        error = None

        if db.execute('SELECT id FROM user WHERE username=? OR email=?',
                      (username, email)).fetchone() is not None:
            error = 'Username or email num is already registered'

        if error is None:
            db.execute(
                'INSERT INTO user (username, password, email, request_ip)'
                ' VALUES (?, ?, ?, ?)',
                (username, generate_password_hash(password), email,
                 request_ip))
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html', form=form)
Exemplo n.º 4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        temp_user = User(username=form.username.data, pwd=form.password.data)
        db.session.add(temp_user)
        db.session.commit()
        flash('Account Created! You are now able to log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register Page', form=form)
Exemplo n.º 5
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('.index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.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='NEW USER', form=form)
Exemplo n.º 6
0
def new():
    form = RegistrationForm(request.form)
    if request.method == 'POST' and form.validate():
        u = User()
        u.firstName = request.form['firstName']
        u.lastName = request.form['lastName']
        u.email = request.form['email']
        db_session.add(u)
        db_session.commit()
        flash('New user created.')
        return redirect(url_for('index'))
    else:
        return render_template('new.html', form=form)
Exemplo n.º 7
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('index'))
    return render_template('register.html', title='Register', form=form)
Exemplo n.º 8
0
def register():
    if (current_user.is_authenticated):
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pwd = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pwd)
        db.session.add(user)
        db.session.commit()
        flash("Your Account has been created successfully!", 'success')
        return redirect(url_for('login'))
    return render_template("register.html", title='Register', form=form)
Exemplo n.º 9
0
def register():
    form = RegistrationForm(request.form)

    if request.method == 'POST' and form.validate():

        username = form.username.data
        password = form.password.data

        db = get_db()
        db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                   (username, generate_password_hash(password)))
        db.commit()
        flash('Thank you for registering')
        return redirect(url_for('auth.login'))

    return render_template('auth/register.html', form=form)
Exemplo n.º 10
0
def edit(id):
    u = db_session.query(User).filter_by(id=id).first()
    if request.method == 'GET':
        form = RegistrationForm(firstName=u.firstName,
                                lastName=u.lastName,
                                email=u.email)
        return render_template('edit.html', user=u, form=form)
    else:
        form = RegistrationForm(request.form)
        if form.validate():
            u.firstName = request.form['firstName']
            u.lastName = request.form['lastName']
            u.email = request.form['email']
            flash('User updated')
            db_session.commit()
            return redirect(url_for('index'))
        else:
            return render_template('edit.html', form=form)
Exemplo n.º 11
0
    def signup(self, request, scheme_id=False):
        """
        Signs up the user into the system given a username and password.
        :param request: The request is passed through so we can access the form
                        contained inside it.
        :param scheme_id: This limits the choices of signup to a particular id
        :return: A view based on wether the signup was sucessfull (redirecting
                to /login or back to the sign-up screen if it was
                unsuccesful)
        """
        try:
            # if scheme_id was set, only let user signup to it
            if scheme_id:
                schemes = self._scheme_handler.get_active_scheme_data()
                scheme_options = [(s['scheme_id'], s['scheme_name'])
                                  for s in schemes
                                  if (s['scheme_id'] == int(scheme_id))]
            else:
                scheme_options = self._get_scheme()

            registration_form = RegistrationForm(request.form)
            # preload form with possible scheme choices for user
            registration_form.scheme_id.choices = scheme_options

        except Exception as e:
            self._log.exception("Invalid registration form")
            raise abort(500)

        try:
            if request.method == "POST":  # if the registation form was submitted
                if registration_form.password.data != registration_form.confirm_password.data:
                    flash(
                        "Password don't match. Make sure the 'password' and 'confirm passowrd' fields match"
                    )
                    return render_template("signup.html",
                                           registration_form=registration_form)

                if registration_form.validate_on_submit(
                ):  # if the form was valid
                    scheme_id = registration_form.scheme_id.data
                    first_name = registration_form.first_name.data
                    last_name = registration_form.last_name.data
                    k_number = registration_form.k_number.data
                    is_mentor = registration_form.is_mentor.data
                    hashed_password = generate_password_hash(
                        registration_form.password.data)

                    if self._student_handler.user_exist(scheme_id, k_number):
                        flash("User already exists")
                        return render_template(
                            "signup.html", registration_form=registration_form)

                    self._student_handler.insert_student(
                        scheme_id, k_number, first_name, last_name, "na", 2018,
                        "Prefer not to say", (1 if is_mentor else 0),
                        hashed_password, False, 1)

                    send_email_confirmation_to_user(
                        scheme_id=scheme_id,
                        k_number=k_number,
                        secret_key=current_app.config["SECRET_KEY"])

                    # redirect to profile page, where he must insert his preferences
                    return redirect("/dashboard")
                else:
                    flash(
                        "Error logging in, please check the data that was entered correctly"
                    )
                    return render_template("signup.html",
                                           registration_form=registration_form)

            return render_template("signup.html",
                                   registration_form=registration_form)

        except Exception as e:
            self._log.exception("Could not parse registration form")
            raise abort(500)