def register():
    form = RegistrationForm()
    # Check if user is already logged in with current_user variable from flask login module downloaded
    if current_user.is_authenticated:
        return redirect('home')

    # WTForms checks if request is POST
    if form.validate_on_submit():
        # Hash users password
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # Create new instance of user with User class declared in models
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)

        # Add user to the database using SQLAlchemy
        db.session.add(user)
        db.session.commit()

        # Show user successful registration message
        flash(f'Your account has been created! You are now able to log in.',
              'success')
        return redirect(url_for('users.login'))

    # If method is GET return register form template
    return render_template('users/register.html', title='Register', form=form)
Beispiel #2
0
def register():
    try:
        if current_user.is_authenticated:
            return redirect(url_for('main.home'))
        form = RegistrationForm()
        if form.validate_on_submit():

            cus = stripe.Customer.create(email=form.email.data.lower())

            hashed_password = bcrypt.generate_password_hash(form.password.data)
            user = User(name=form.name.data,
                        email=form.email.data.lower(),
                        password=hashed_password,
                        customer_id=cus.id)
            db.session.add(user)
            db.session.commit()
            token = generate_confirmation_token(user.email)
            confirm_url = url_for('users.confirm_email',
                                  token=token,
                                  _external=True)
            html = render_template("activate.html", confirm_url=confirm_url)
            subject = "Please confirm your email"
            send_email(user.email, subject, html)

            login_user(user)

            flash(
                'Your account has been created! A confirmation email has been sent.',
                'success')
            return redirect(url_for('users.account'))
        return render_template("register.html", title="Register", form=form)
    except Exception as e:
        SendErrMail(e)
Beispiel #3
0
def register():
    # if user is already loged in and authenticated they will redirect to home page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():

        # hashing the password that user enterd
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # creting the user with given details and hashed password
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)

        # adding user to db and commiting changes
        db.session.add(user)
        db.session.commit()

        flash(f"Account has been created! you are now able to log in",
              "success")
        return redirect(url_for('users.login'))

    return render_template('register.html', title='Register Now', form=form)
Beispiel #4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    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"""
                Conta criada com sucesso!\n
                Bem-vindo {form.username.data} ao meu blog!
            """,
            'success'
        )
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Registrar', form=form)
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        username = form.username.data
        email = form.email.data

        if (User.query.filter_by(username=username).first()):
            flash('username is already exist', 'danger')
            return render_template('register.html',
                                   form=form,
                                   title='Register')
        if (User.query.filter_by(email=email).first()):
            flash('Email is already exist', 'danger')
            return render_template('register.html',
                                   form=form,
                                   title='Register')

        user = User(username=username, email=email, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to login',
              'success')
        return redirect(url_for('users.login'))

    return render_template('register.html', form=form, title='Register')
Beispiel #6
0
def register():
    """Register route and render form. Validate user."""
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    # check if form validated properly (when we come back to the form after submit)
    if form.validate_on_submit():
        # if validate ok, show message to user. use bootstrap alert style class: success
        # Create account:
        #  hash the pw
        hsh_pw = bcrypt_flask.generate_password_hash(form.password.data,
                                                     12).decode('utf-8')
        # create user with form-data
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hsh_pw)
        db.session.add(user)
        db.session.commit()
        # We're ok:
        flash('Account created. Please login.', 'success')
        # redirect to home page
        return redirect(url_for('users.login'))
    # if no validate render form
    return render_template('register.html', title='Register', form=form)
Beispiel #7
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    # create and instance of RegistrationForm
    registerForm = RegistrationForm()

    # Submit validation with flash message notifications
    if registerForm.validate_on_submit():
        # hash password into string format
        hashed_pw = bcrypt.generate_password_hash(
            registerForm.password.data).decode('utf-8')
        # create user
        user = User(username=registerForm.username.data,
                    email=registerForm.email.data,
                    password=hashed_pw)
        # add user to database
        db.session.add(user)
        db.session.commit()
        # flash message
        flash(
            f'Your account has successfully been created. You can now log in!',
            'success')
        # redirect to login page
        return redirect(url_for('users.login'))

    return render_template('register.html',
                           title='Register',
                           form=registerForm)
Beispiel #8
0
def register():
    if current_user.is_authenticated:
        # flash(f'You are already registered!')
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # user ={
        #     "username": form.username.data,
        #     "email":form.email.data,
        #     "password":hashed_password
        # }
        # return render_template('session.html', user= user)

        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Your Account has been created! You are now able to log in',
              'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #9
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        # First, I will hash the password to store it safely
        hashed_pwd = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        # Then, I will create a new User model with the data of the register form, to store it in the DB
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pwd)
        # Add user to DB and commit changes
        db.session.add(user)
        db.session.commit()
        # Give feedback to the user
        flash(
            f'Your account has been created {form.username.data}! You are now able to log in',
            'success'
        )  # second argument is category, passing a bootstrap 4 class
        return redirect(
            url_for('users.login'))  # passing name of function, not of route
    return render_template(
        'users/register.html', title='Register',
        form=form)  # last argument is to pass it to pass data to the template
Beispiel #10
0
def register():
    """
    To register the account for new users
    :return: if authenticated, redirect to homepage.
    if registration is submitted, redirect to login page
    At default, render registration page, title, form, year_dict, month_dict,
    side_posts
    """
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Your account has been created!', 'success')
        return redirect(url_for('users.login'))
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('register.html', title='Register', form=form,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)
Beispiel #11
0
def register():
    user_count = db.session.query(User).count()

    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    form.country.choices = [(i['name'], i['name']) for i in countries]

    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,
                    country=form.country.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        send_confirmation_email(user)
        flash('A confirmation email has been sent to you by email.', 'info')
        # flash('Your account has been created! You are now able to log in',
        #     'success')  # messages that pop up, 'success' is used for bootstrap
        subscribe_user(user.email, "*****@*****.**",
                       app.config['MAIL_API_KEY'])
        return redirect(url_for('users.login'))
    return render_template('register.html',
                           title='Register',
                           form=form,
                           user_count=user_count)
Beispiel #12
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(name=form.name.data,
                    surname=form.surname.data,
                    birthdate=form.birthdate.data,
                    email=form.email.data,
                    password=hashed_password)
        send_mail(form.email.data,
                  'You have registered succesfully',
                  'mail',
                  name=form.name.data,
                  username=form.email.data,
                  password=form.password.data)

        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #13
0
def register():
    # redirect to home if user already logged in
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()

    # check if the inputs validate successfully
    if form.validate_on_submit():
        # calculate hash of the entered password
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # create the user in the DB
        user = User(name=form.name.data,
                    email=form.email.data,
                    password=hashed_password)

        # write the new user to the DB
        db.session.add(user)
        db.session.commit()

        # tell user that the account has been created
        flash(f'Your Account has been created. Welcome to Flask Blog',
              'positive')

        # redirect to login page
        return redirect(url_for('users.login'))

    return render_template('register.html', title='Register', form=form)
Beispiel #14
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        print(Users.query.all())
        username = form.username.data
        email = form.email.data
        password = form.password.data
        hashed_password = bcrypt.generate_password_hash(password).decode(
            "utf-8")
        if Users.query.filter_by(
                username=username).first() or Users.query.filter_by(
                    email=email).first():
            flash(
                f'Could not create account.Username or email is already taken!',
                'danger')
            return redirect(url_for('users.register'))
        else:
            user = Users(username=username,
                         password=hashed_password,
                         email=email)
            db.session.add(user)
            db.session.commit()
            flash(f'Account created for {form.username.data}!', 'success')
            print(Users.query.all())
            return redirect(url_for('users.login'))

        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('main.home'))
    return render_template('register.html', title='Register', form=form)
Beispiel #15
0
def register():
    # --------------------this is what happens if already loged in ------------
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    # ------------------ do the registration stuff ----------------------------
    form = RegistrationForm()
    if form.validate_on_submit():
        #gnerate a hashed password form the input data form form, decode UTF makes a string in intead of bytes
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('UTF-8')
        #generate a user from the form data with the hashed password
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        #add the user to the database
        db.session.add(user)
        #commit the changes of the database
        db.session.commit()
        #flash message pops up once
        #bootstrap has different allertstyles for succeses and warnings and errors
        #the flash function accepts a second argument which is the bootstrap name that the alert should have
        #dont forget to update the template to show the flash message
        flash(f'Your account has been created! Now you are able to login!',
              'success')
        #after succesfull submit the user should be be redirected automatically to login
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #16
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        balance = 0
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        if form.designation.data == 'L' or form.designation.data == 'M' or form.designation.data == 'SSE':
            balance = 1000
        user = User(username=form.username.data,
                    email=form.email.data,
                    designation=form.designation.data,
                    team=form.team.data,
                    password=hashed_password,
                    balance=balance)
        db.session.add(user)
        db.session.commit()

        flash(f'Your account is created. You will now be able to login!',
              'success')
        return redirect(url_for('users.login'))

    counts = User.query.order_by(desc(User.earned)).limit(5)
    return render_template('register.html',
                           title='Register',
                           form=form,
                           counts=counts)
Beispiel #17
0
def register():
	if current_user.is_authenticated:
		return redirect(url_for('main.home'))
	registration_form = RegistrationForm()
	if registration_form.validate_on_submit():
		create_user(registration_form)
		flash("Account created!",'success')
		return redirect(url_for('users.login'))
	return render_template('register.html',title="register",form=registration_form)
Beispiel #18
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #19
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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).save()
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('main.home'))
    return render_template('register.html', title='Register', form=form)
Beispiel #20
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('Your account has been created! You are now able to log in!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #21
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Hesabınız oluşturuldu. Giriş yapabilirsiniz.', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Kayıt ol', form=form, legend='Kayıt ol')
Beispiel #22
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Account created successfully. You can now log in!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form, prof_pic=firebase_storage.prof_img(current_user))
Beispiel #23
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hash_pw = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hash_pw)
        db.session.add(user)
        db.session.commit()
        flash('Başarılı üyelik', 'success')
        return redirect(url_for('users.login'))
    return render_template("register.html", title="Üye Ol", form=form)
Beispiel #24
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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'Your Account created successfully..! You are now able to login.','success')
        return redirect(url_for('users.login'))
    return render_template("register.html",title="Register",form=form) 
Beispiel #25
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("main.home"))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') # deocde to have a string and not bites
        user = User(username=form.username.data, email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created! You are now able to log in.', 'success')
        return redirect(url_for("users.login"))
    return render_template('register.html', title='Register', form=form)
Beispiel #26
0
def register_page():
    if current_user.is_authenticated:
        return redirect(url_for('main.home_page'))
    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'Your account has been created!', 'success')
        return redirect(url_for('login_page'))
    return render_template('register.html', title='Sign Up', form=form)
Beispiel #27
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Your account has been created! You are now able to log in', 'success')  # flash takes second argument, it might be 1of3 bootsrap classess: success, warning, error
        return redirect(url_for('users.login'))  # login is name of function!
    return render_template('register.html', title='Register', form=form)  # IMPORTANT form=form!!!!
Beispiel #28
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Akun berhasil dibuat! Kamu dapat masuk!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Daftar', form=form)
Beispiel #29
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #30
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    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('Thanks for registerting with us {}!'.format(form.username.data), 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #31
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('posts.dashboard'))
    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('注册成功! ', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='注册', form=form)