Exemplo n.º 1
0
 def register(self):
     u = User(username=self.username.data,
              email=self.email.data,
              password=bcrypt.generate_password_hash(
                  self.password.data).decode('utf-8'))
     db.session.add(u)
     db.session.commit()
Exemplo n.º 2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        # bcrypt.generate_password_hash(form.password.data) - returns bytes
        # bcrypt.generate_password_hash(form.password.data).decode('utf-8') - returns string
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        user = User(email=form.email.data,
                    username=form.username.data,
                    password=hashed_password)
        # TODO delete
        user.active = True

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

        profile = Profile(user_id=user.id, user=user)
        db.session.add(profile)
        db.session.commit()

        user.send_verification_email()
        # 'success' is the name of the BootStrap class for message.
        flash(f'A confirmation email has been sent to {form.email.data}',
              'success')
        return redirect(url_for('users.login'))
    return render_template('users/register.html', title='Register', form=form)
Exemplo n.º 3
0
def reset_token(token):
    if current_user.is_authenticated:
        flash('Youre already logged in.', 'success')
        return redirect(url_for('home'))

    user = User.verify_reset_token(token)
    if user is None:
        flash('This is an invalid or expired token, please try again.',
              'warning')
        return redirect(url_for('reset_request'))
    form = ResetPasswordForm()

    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash('Your password has been reset, log in to verify the change!',
              'success')
        return redirect(url_for('login'))

    return render_template('reset_token.html',
                           title='Reset Password',
                           form=form)


#@app.route() ///UPLOAD IMAGES\\\ https://www.youtube.com/watch?v=6WruncSoCdI
def reset_token(token):
    """reset password's token

    Arguments:
        token {Token} -- Object

    Returns:
        page -- If user is authenticated then redirect to home page.
        If user is none then shows warning.else show a message with updated password and
        redirect to login page.
    """
    if current_user.is_authenticated:
        return redirect(url_for("papers.home"))
    user = User.verify_reset_token(token)
    if user is None:
        flash("That is an invalid or expired token", "warning")
        return redirect(url_for("users.reset_request"))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode("utf-8")
        user.password = hashed_password
        db.session.commit()
        flash("Your password has been updated! You are now able to log in",
              "success")
        return redirect(url_for("users.login"))
    return render_template(
        "users/reset_token.html",
        title="Reset Password",
        form=form,
        js_files=["js/users/reset_password.js"],
    )
def register():
    """Registeration of user

    Returns:
        HTML function/ page -- If user is authenticated then redirect to papers.home page.
        after submitting the form for registration go to login page.
        and when this page is load returns HTML function.
    """
    if current_user.is_authenticated:
        return redirect(url_for("papers.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(
        "users/register.html",
        title="Register",
        form=form,
        css_files=["css/users/register.css"],
        js_files=["js/users/register.js"],
        btn_name="Back",
    )
Exemplo n.º 6
0
def reset_token_pw(token):

    # If user is already logged in, return to home page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    # Verify if URL includes valid token
    user = User.verify_reset_token_pw(token)

    # Handle if URL is not valid
    if user is None:
        flash('Token invalid or expired.', 'warning')  # display message
        return redirect(url_for('users.reset_request_pw'))

    # Set form variable to password reset page form
    form = ResetPasswordForm()

    # Only run following code if form response passes checks (in users/forms.py)
    if form.validate_on_submit():

        # Add user to db
        hashed_pw = bcrypt.generate_password_hash(form.password.data).decode(
            'utf-8')  # encrypt password
        user.password = hashed_pw  # set new password
        db.session.commit()  # save changes

        # Inform user that password has been reset
        flash(f'Password reset for {form.username.data}!', 'success')
        return redirect(url_for('users.login'))

    return render_template('reset_token_pw.html',
                           title='Reset Password',
                           form=form)  # key variables for .html
Exemplo n.º 7
0
    def test_user1_following_user2(self):
        hashed_password = bcrypt.generate_password_hash(
            self.TEST_USER_PASSWORD).decode('utf-8')

        self.user1 = User(email='*****@*****.**',
                          username='******',
                          password=hashed_password)
        self.user2 = User(email='*****@*****.**',
                          username='******',
                          password=hashed_password)

        self.user1.active = True
        self.user2.active = True

        db.session.add(self.user1)
        db.session.add(self.user2)
        db.session.commit()

        self.profile1 = Profile(user_id=self.user1.id, user=self.user1)
        self.profile2 = Profile(user_id=self.user2.id, user=self.user2)

        db.session.add(self.profile1)
        db.session.add(self.profile2)
        db.session.commit()

        self.profile1.follow(self.profile2)
        self.assertIn(self.profile2, self.profile1.followed.all())
        self.assertIn(self.profile1, self.profile2.followers.all())
        self.assertNotIn(self.profile2, self.profile1.followers.all())
        self.assertNotIn(self.profile1, self.profile2.followed.all())
        self.assertTrue(self.profile1.is_following(self.profile2))
        self.assertFalse(self.profile2.is_following(self.profile1))
        self.assertTrue(self.profile2.is_followed_by(self.profile1))
        self.assertFalse(self.profile1.is_followed_by(self.profile2))
Exemplo n.º 8
0
def encrypt_password(psw: str):
    """
    Encrypts a password and return the encrypted string.

    :param psw: The string to be encrypted.
    :return: A encrypted string.
    """
    return bcrypt.generate_password_hash(psw).decode("utf-8")
Exemplo n.º 9
0
def signup(creds):
    password_hash = bcrypt.generate_password_hash(
        password=creds["password"]).decode('utf-8')
    dbcursor.execute(
        "INSERT INTO user (fname, lname, email, uname, password) VALUES (%s, %s, %s, %s, %s)",
        (creds["fname"], creds["lname"], creds["email"], creds["uname"],
         password_hash))
    db.commit()
    return 200
Exemplo n.º 10
0
def changepwd():
    form = updatePassword()
    if form.validate_on_submit():
        hashPwd = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        current_user.password = hashPwd
        db.session.commit()
        flash('Password updated', 'success')
    image_file = ('profile_pics/' + current_user.picture)
    return render_template('changepwd.html', title= 'Change Password', form = form, image_file=image_file)
Exemplo n.º 11
0
class BaseTestCase(TestCase):
    TEST_USER_EMAIL = '*****@*****.**'
    TEST_USER_USERNAME = '******'
    TEST_USER_PASSWORD = '******'
    TEST_HASHED_PASSWORD = bcrypt.generate_password_hash(
        TEST_USER_PASSWORD).decode('utf-8')

    def create_app(self):
        from flaskapp import create_app
        from flaskapp.config import TestConfig

        app = create_app(TestConfig)
        return app

    def setUp(self):
        """
        Will be called before every test
        """
        db.create_all()

        user = User(
            email=self.TEST_USER_EMAIL,
            username=self.TEST_USER_USERNAME,
            password=self.TEST_HASHED_PASSWORD,
        )
        user.active = True

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

        profile = Profile(user_id=user.id, user=user)

        db.session.add(profile)
        db.session.commit()

    def tearDown(self):
        """
        Will be called after every test
        """
        db.session.remove()
        db.drop_all()

    def login(self, email, password):
        return self.client.post(
            '/login/',
            data=dict(email=email, password=password),
            follow_redirects=True,
        )

    def logout(self):
        return self.client.get(
            '/logout/',
            follow_redirects=True,
        )
Exemplo n.º 12
0
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')
        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)
Exemplo n.º 13
0
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')
      user = User(username=form.username.data, email=form.email.data, password=hashed_password)
      db.session.add(user)
      db.session.commit()
      flash('Tilisi on luotu! Nyt voit kirjautua sisään!', 'success')
      return redirect(url_for('login'))
    return render_template('register.html', title='Rekisteröityminen', form=form)
Exemplo n.º 14
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        newUser = User(username=form.username.data,
                       email=form.email.data, password=hashed_password)
        db.session.add(newUser)
        db.session.commit()
        flash(f'Your account has been created! You can login now', 'success')
        return redirect(url_for('home'))
    return render_template('register.html', title='Register', form=form)
Exemplo n.º 15
0
def registrar():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrarForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = Usuario(email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Su cuenta ha sido registrada! ya puedes ingresar', 'success')
        return redirect(url_for('user.login'))
    return render_template('register.html', title='Registro', form=form)
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    # Bei erfolgreicher Registration -> success-alert via Bootstrap
    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'Dein Account wurde erfolgreich registriert. Jetzt anmelden!', 'success')
        return redirect(url_for('login'))
    return render_template('registration.html', title='Registration', form=form)
Exemplo n.º 17
0
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')
        user = User(username=form.username.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash("Your account has been created, You can now login!", "success")
        return redirect(url_for('login'))
    return render_template("register.html", form=form)
Exemplo n.º 18
0
 def test_view_user_profile(self):
     from flaskapp import User, db, bcrypt, app
     from bs4 import BeautifulSoup
     # Add 2 users
     admin = User(
         username='******',
         email='*****@*****.**',
         password=bcrypt.generate_password_hash("admin").decode('utf-8'))
     susan = User(
         username='******',
         email='*****@*****.**',
         password=bcrypt.generate_password_hash("susan").decode('utf-8'))
     db.session.add(admin)
     db.session.add(susan)
     db.session.commit()
     # Try to view susan's profile without logging in
     test_client = app.test_client()
     response = test_client.get('/user/susan/profile',
                                follow_redirects=True)
     response_soup = BeautifulSoup(response.data, 'html.parser')
     # Unsuccessful
     self.assertEqual(response_soup.title.string, u'Login - UMD Connect')
Exemplo n.º 19
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        new_user = User(username=form.username.data,
                        email=form.email.data,
                        password=hashed_pw)
        db.session.add(new_user)
        db.session.commit()
        flash(f"Account with email: {form.email.data} is created.", 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Exemplo n.º 20
0
 def test_create_user(self):
     from flaskapp import User, db, bcrypt
     # Add a user
     admin = User(
         username='******',
         email='*****@*****.**',
         password=bcrypt.generate_password_hash("admin").decode('utf-8'))
     db.session.add(admin)
     db.session.commit()
     # Check that the user was added.
     user = User.query.filter_by(username="******").first()
     self.assertEqual(user.username, "admin")
     self.assertEqual(user.email, "*****@*****.**")
Exemplo n.º 21
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("home"))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode("utf-8")
        user = User(username=form.username.data, password=hashed_pw)
        db.session.add(user)
        db.session.commit()
        flash('Account Created', "success")
        return redirect(url_for('login'))
    return render_template("register.html", form=form)
Exemplo n.º 22
0
def password_change(username):
    form = ChangePasswordForm()
    if form.validate_on_submit():
        if form.password.data:
            hashed_password = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            current_user.password = hashed_password
            db.session.commit()
        flash('Your account has been updated!', 'success')
        return redirect(
            url_for('users.profile', username=current_user.username))
    return render_template('users/profile_password_change.html',
                           title='Change password',
                           form=form)
Exemplo n.º 23
0
def update_profile():
    form = UpdateForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=current_user.email).first()
        if bcrypt.check_password_hash(user.password, form.old_password.data):
            if form.new_email.data:
                user.email = form.new_email.data
            if form.username.data:
                user.username = form.username.data
            if form.description.data:
                user.description = form.description.data
            if form.picture.data:
                picture_file = save_picture(form.picture.data)
                current_user.image_file = picture_file
            if form.skills_bus.data and not current_user.business:
                current_user.business = True
            elif not form.skills_bus.data and current_user.business:
                current_user.business = False
            if form.skills_lit.data and not current_user.literature:
                current_user.literature = True
            elif not form.skills_lit.data and current_user.literature:
                current_user.literature = False
            if form.skills_tech.data and not current_user.technology:
                current_user.technology = True
            elif not form.skills_tech.data and current_user.technology:
                current_user.technology = False
            if form.skills_art.data and not current_user.art:
                current_user.art = True
            elif not form.skills_art.data and current_user.art:
                current_user.art = False
            if form.skills_music.data and not current_user.music:
                current_user.music = True
            elif not form.skills_music.data and current_user.music:
                current_user.music = False
            if form.new_password.data:
                hashed_password = bcrypt.generate_password_hash(
                    form.new_password.data).decode('utf-8')
                user.password = hashed_password

            db.session.commit()
            flash(f'Your account has been updated.', 'success')
            return redirect(url_for('profile', username=current_user.username))
        else:
            flash('Incorrect. Please check password', 'danger')
    image_file = url_for('static',
                         filename='profile_pics/' + current_user.image_file)
    return render_template('update-profile.html',
                           title='Update Profile',
                           form=form,
                           image_file=image_file)
Exemplo n.º 24
0
def CreateAdmin(downloadimages):
    username = "******"
    email = "*****@*****.**"
    password = "******"
    admin = "y"
    db.create_all()
    hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
    user = User(username=username,
                email=email,
                password=hashed_password,
                admin=admin)
    db.session.add(user)
    db.session.commit()
    PopulateDatabase(downloadimages)
Exemplo n.º 25
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        hash_pass = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hash_pass)
        db.session.add(user)
        db.session.commit()
        flash('Account Have Been Created!, You Are Now Able To Login',
              'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Exemplo n.º 26
0
def register():

    form = RegisterForm()
    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()
        login_user(user)
        flash(f'account created for {form.username.data}!', 'success')
        return redirect(url_for('posts'))
    return render_template("register.html", title="Register", form=form)
Exemplo n.º 27
0
def signup():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = SignupForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        new_user = User(username=form.username.data,
                        email=form.email.data,
                        password=hashed_password)
        db.session.add(new_user)
        db.session.commit()
        flash('Your account has been created, you can signup now.', 'success')
        return redirect(url_for('users.login'))
    return render_template('users/signup.html', form=form)
Exemplo n.º 28
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash('Your password has been updated! You are now able to log in.', 'success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html', title='Reset password', form=form)
Exemplo n.º 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,
                    password=hashed_password,
                    is_admin=True)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('users.login'))
        flash('Registration succesfull', 'success')
    return render_template('master/register.html', form=form, title='register')
Exemplo n.º 30
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = registerForm()
    if form.validate_on_submit():
        hashPassword = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username = form.username.data , email = form.email.data, password = hashPassword)
        if(user.email == User.query.filter_by(email = user.email).first()):
            flash('Email already exist', 'danger')
            return redirect(url_for('login'))
        db.session.add(user)
        db.session.commit()
        flash('Account has been created', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title = 'Sign up', form = form)