def setUp(self): db.create_all() admin = User(username="******", email="ausers;[email protected]", password="******") user = User(username="******", email="*****@*****.**", password="******") db.session.add(admin) db.session.add(user) db.session.commit()
def create_user(username, password): if User.query.filter_by(username=username).first(): raise RuntimeError(f'{username} is already on DB') user = User(username=username, password=generate_password_hash(password), profile='admin', name='AdminUser') db.session.add(user) db.session.commit() return user
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 login(): if current_user.is_authenticated: return redirect(url_for('products_list')) form = LoginForm(request.form) error = None if request.method == 'POST' and form.validate(): username = form.username.data.lower().strip() password = form.password.data.lower().strip() user = mongo.db.users.find_one({"username": form.username.data}) if user and User.validate_login(user['password'], form.password.data): user_obj = User(user['username']) login_user(user_obj) return redirect(url_for('products_list')) else: error = 'Incorrect username or password.' return render_template('user/login.html', form=form, error=error)
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()
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 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
def reset_token_email(token): # Verify if URL includes valid token user = User.verify_auth_token_email(token) # Handle if URL is (not) valid if user is None: flash('Token invalid or expired.', 'warning') # display message return redirect(url_for('users.login')) form = ResetEmailForm() if form.validate_on_submit(): # Replace email with updated email current_user.email = current_user.temp_email # set new email current_user.temp_email = None # reset temp_email field db.session.commit() # save changes # Inform user that email has been changed flash(f'Email changed!', 'success') return redirect(url_for('users.login')) return render_template('reset_token_email.html', title='Reset Email', form=form) # key variables for .html
def test_make_unique_nickname(self): u = User(nickname = 'john',email = '*****@*****.**' ) db.session.add(u) db.session.commit() nickname = User.make_unique_nickname('john') self.assertNotEqual(nickname,'john') u = User(nickname = nickname ,email = '*****@*****.**') db.session.add(u) db.session.commit() nickname2 = User.make_unique_nickname('john') self.assertNotEqual(nickname2,nickname) self.assertNotEqual(nickname2,'john')
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", )
def make_coffee(name='Unknown'): user = User.get(1) coffee = '''A special coffee for you {}, it was made by {}, enjoy it!!! ( ) ( ___...(-------)-....___ .-"" ) ( ""-. .-'``'|-._ ) _.-| / .--.| `""---...........---""` | / / | | | | | | \ \ | | `\ `\ | | `\ `| | _/ /\ / (__/ \ / _..---""` \ /`""---.._ .-' \ / '-. : `-.__ __.-' : : ) ""---...---"" ( : '._ `"--...___...--"` _.' \""--..__ __..--""/ '._ """----.....______.....----""" _.' `""--..,,_____ _____,,..--""` `"""----"""` '''.format(name, user.name) print(coffee)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('home')) user = User.verify_reset_token(token) if user is None: flash("That is invalid or expired token.", "warning") return redirect(url_for('reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data) user = User(passwor=form.password.data) user.password = hashed_password db.session.commit() flash('Your password has been reset! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def add_users(number_of_users, group): while number_of_users > 0: alphabet = string.ascii_letters + string.digits random_key = ''.join(secrets.choice(alphabet) for i in range(5)) if User.query.filter_by(login=random_key).first(): continue new_user = User(login=random_key, group=group) db.session.add(new_user) number_of_users = number_of_users - 1 db.session.commit() if group.is_containing_sections: max_number = db.session.query(func.max( User.section_number)).filter_by(group=group).scalar() if max_number: new_user.section_number = max_number + 1 else: for i, user in enumerate(group.users, 1): user.section_number = i db.session.commit()
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() flash("Account registerd sucessfully. You can now login", 'success') return redirect(url_for('login')) return render_template("register.html", title="Register", 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') 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)
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)
def register(): form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): user = User(form.userid.data, form.username.data, form.password.data) db_session.add(user) return redirect(url_for('login')) else: print(request.method) return render_template('register.html', form=form)
def get(self, id=None): try: if id is None: return self.__query({}) return make_response(jsonify(User.get(id).as_dict())) except Exception as e: print(e) response_object = { 'status': 'fail', 'message': 'Try again, detlais: {}'.format(str(e)) } return make_response(jsonify(response_object), 500)
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)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, password=form.password.data) db.session.add(user) db.session.commit() flash(f'Account created for {form.username.data}!', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def sign_up(): form=SignUpForm() if request.method=='POST': name=request.form.get('name') email=request.form.get('email') password=request.form.get('password') passwd=bcypt.generate_password_hash(password) new_user=User(name=name,email=email,password=passwd) db.session.add(new_user) db.session.commit() flash('New User Added Successfully,You can Now log in') return redirect('/signup') return render_template('sign.html',form=form)
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)
def post(self): try: post_data = request.get_json() post_data['password'] = generate_hash(post_data.get( 'password', '')) user = User.create_from_dict(post_data) return make_response(jsonify(user.as_dict()), 201) except Exception as e: response_object = { 'status': 'fail', 'message': 'Some error occurred. Please try again.', } return make_response(jsonify(response_object), 500)
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)
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)
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)
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)
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)
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')
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)
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)
def test_avatar(self): u = User(nickname = 'john' ,email = '*****@*****.**') avatar = u.avatar(128) expected = 'http://en.gravatar.com/avatar/d4c74594d841139328695756648b6bd6' self.assertEqual(avatar[0:len(expected)],expected)