Example #1
0
def register():
    form = RegisterForm(request.form)
    if form.validate_on_submit():
        # Create new User instance
        user = User(
            name=form.username.data,
            email=form.email.data,
            password=form.password.data,
        )

        try:
            user.save()
        except UniquenessViolation, err:
            if err.column_name == "email":
                form.email.errors.append("There is already an account registered with that email address.")
            elif err.column_name == "name":
                form.username.errors.append("There is already an account registered with that username.")
        else:
            login_user(user)

            # Flash a message that gets displayed only once.
            flash("Welcome to the herd.")

            return redirect(url_for("users.profile"))
        finally:
Example #2
0
def account():
    if not current_user.is_authenticated:
        abort(401)

    form = RegisterForm()
    form.submit.label.text = 'Update user info'

    if request.method == 'GET':
        form.username.data = current_user.username
        form.phone_number.data = current_user.phone_number
        form.first_name.data = current_user.first_name
        form.last_name.data = current_user.last_name

    if form.validate_on_submit():
        current_user.username = form.username.data
        current_user.phone_number = form.phone_number.data
        current_user.first_name = form.first_name.data
        current_user.last_name = form.last_name.data

        if form.moderator_token.data == MODERATOR_TOKEN:
            current_user.superuser = True
            flash(
                'Successfully activated moderator token. You now can create/update/delete items from the application database.'
            )
            print(current_user)

        db.session.commit()

        flash('User info has been successfully changed')
        redirect(url_for('users.account'))

    return render_template('users/account.html', form=form)
Example #3
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        print(form.errors)
        if form.password.data == form.confirm_password.data:
            user = User(username=form.username.data,
                        email=form.email.data,
                        password=generate_password_hash(form.password.data,
                                                        method='sha256'))
            db.session.add(user)
            role = Role.query.filter_by(name="Generic").first()
            if role is not None:
                user.roles.append(role)
            try:
                db.session.commit()
                flash("Successfully registered")
            except IntegrityError as err:
                db.session.rollback()
                if "UNIQUE constraint failed" in str(err):
                    flash("error, username already exists (%s)" %
                          form.username.data)
                else:
                    flash("unknown error adding user")
        else:
            flash("Passwords don't match")
    return render_template("register.html", form=form, title="Register")
Example #4
0
def register():
    if current_user.is_authenticated:
        flash('You have already logged in')
        return redirect(url_for('main.index'))

    form = RegisterForm()

    if form.validate_on_submit():
        username = form.username.data

        password = form.password.data
        password = hashlib.md5(password.encode()).hexdigest()

        first_name = form.first_name.data
        last_name = form.last_name.data
        phone_number = form.phone_number.data

        user_info = {
            'username': username,
            'password': password,
            'first_name': first_name,
            'last_name': last_name,
            'phone_number': phone_number,
        }

        user = User(**user_info)

        db.session.add(user)
        db.session.commit()

        flash('You Now May Log In')

        return redirect(url_for('users.login'))
    return render_template('users/register.html', form=form)
Example #5
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(form.name.data, form.email.data, form.password.data)
        db.session.add(user)
        db.session.commit()
        login_user(user)
        flash('Thanks for registering')
        return redirect(url_for('users.home'))
    return render_template("register.html", form=form)
Example #6
0
def create_user():
    form = RegisterForm()
    if not form.validate_on_submit():
        return {'errors': form.errors}, 400
    new_user = User(form.full_name.data, form.email.data, form.password.data)
    db.session.add(new_user)
    db.session.commit()
    token = generate_confirmation_token(new_user.Email)
    subject = 'DailyDiet | Email Confirmation'
    confirm_url = url_for('users.confirm_email', token=token, _external=True)
    html = html = render_template('users/activate.html', confirm_url=confirm_url)
    send_email(new_user.Email, subject, html)
    return {'msg': 'Account created successfully!'}, 201
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        new_user = User(name=form.name.data,
                        last_name=form.last_name.data,
                        username=form.username.data,
                        email=form.email.data,
                        phone_number=form.phone_number.data,
                        password=form.password.data,
                        phone_number_is_Confirm=False,
                        email_is_Confirm=False)
        db.session.add(new_user)
        db.session.commit()
        return redirect(url_for('users.login'))
    return render_template('users/register.html', form=form)
Example #8
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        # Create hashed password
        hashed_password = generate_password_hash(form.password.data)
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        # database submission
        db.session.add(user)
        db.session.commit()
        flash('Thank you for registering, now please log in')
        return redirect(url_for('.login'))

    return render_template('users/register.html', form=form)
Example #9
0
def register():
    form = RegisterForm()
    #
    if form.validate_on_submit():
        pass
    #     #check if user exists
    #     user = queries.check_username(username=form.username.data)
    #
    #     if user is not None:
    #         queries.create_user(username=form.username.data, password=generate_password_hash(form.password.data))
    #         flash('Thanks for registering! Now you can login!')
    #         return redirect(url_for('login.html'))
    #
    #     flash('That user already exists')

    return render_template('register.html', form=form)
Example #10
0
def register():
    form = RegisterForm(request.form)
    if form.validate_on_submit():
        user = User.query.filter(User.username == form.username.data).first()
        if user is not None:
            flash('This username is already taken', 'error')
            user = User.query.filter(User.email == form.email.data).first()
            if user is not None:
                flash('This email address is already taken', 'error')
            return render_template('register.html', form=form)
        user = User(form.username.data, form.email.data)
        user.set_password(form.password.data)
        db_session.add(user)
        db_session.commit()
        login_user(user)
        return redirect(url_for('main.hello'))
    return render_template('register.html', form=form)
Example #11
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        hashed_password = generate_password_hash(form.password.data,
                                                 method='pbkdf2:sha256',
                                                 salt_length=10)
        user = Users(
            username=form.username.data,
            password=hashed_password,
            email=form.email.data,
            first_name=form.first_name.data,
            last_name=form.last_name.data,
        )
        db.session.add(user)
        db.session.flush()
        if user.id:
            db.session.commit()
        else:
            db.session.rollback()
            error = 'Error creating user'
            return render_template('users/register.html', form=form)
        return "User Registered"
    return render_template('users/register.html', form=form)
Example #12
0
    def post(self):
        form = RegisterForm()
        if not form.validate_on_submit():
            return self.get()

        if User.get_by(email=request.form['email']):
            return 'Email уже использован'

        birth_date = datetime.strptime(request.form["birth_date"],
                                       "%Y-%m-%d").date()
        user = User(request.form['name'],
                    request.form['surname'],
                    request.form['fathername'],
                    request.form['email'],
                    request.form['password'],
                    sex=request.form['sex'],
                    birth_date=birth_date)
        self.send_email(user)
        user.save(add=True)

        print('Зарегистрирован пользователь:', user)
        login_user(user)
        return redirect(url_for('users.profile'))