def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_user_picture(form.picture.data) current_user.user_picture = picture_file current_user.username = form.username.data current_user.email = form.email.data hashed_password = bcrypt.generate_password_hash( form.confirm_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_bp.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email user_picture = url_for('static', filename='profile_pics/' + current_user.user_picture) return render_template('account.html', title='Account', user_picture=user_picture, form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): hashed = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed, confirmed=False) db.session.add(user) db.session.commit() session['reg_username'] = user.username token = generate_confirmation_token(form.email.data) confirm_url = url_for('users.confirm_email', token=token) html = render_template('confirmation_email.html', user=user, confirm_url=confirm_url) subject = "Welcome to Ask.it!" send_email(user.email, subject, html) flash('A confirmation email has been sent via email.', 'success') return redirect(url_for('users.tfa', user=user)) return render_template('register.html', form=form)
def signup(): if request.method == "POST": form = request.form pass_hash = bcrypt.generate_password_hash(form["password"]).decode('utf-8') Id = profiledb.insert_one({ "fname": form["fname"], "lname": form["lname"], "email": form["email"], "username": "", "pfpURL": "/static/img/profile_pictures/default.png", "phone": form["phone"], "password": pass_hash, "meals": 0, "followers": 0, "following": 0, "level": "beginner", "role": "student", "post_update": False, "task_update": False, "notification_update": False, }) session.clear() session["userId"] = str(Id.inserted_id) session["wall_update"] = updatedb.find_one()["postId"] return redirect(url_for('profile_create')) return render_template('signup.html')
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): msg = Message('Thanks for Registering!', sender='*****@*****.**', recipients=[str(form.email.data)]) msg.body = "Hi there! Thanks for registering to Cat Wiki!\n\nYour username is: " + str( form.username.data ) + "\n\nThank you for using our website, we hope you have an excellent day!" mail.send(msg) hashed = bcrypt.generate_password_hash( form.password.data).decode("utf-8") user = User(username=form.username.data, email=form.email.data, password=hashed) user.save() return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def create_user(cls, username, email, password): hashed_password = bcrypt.generate_password_hash( password=password ).decode('utf-8') user = cls(username=username, password=hashed_password, email=email) db.session.add(user) db.session.commit()
def setUp(self): db.create_all() test_user = User(username=self.test_username, password=bcrypt.generate_password_hash( self.test_password), type=self.test_type) db.session.add(test_user) db.session.commit()
def register(): form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') employee = Employee(name=form.name.data, emp_id=form.emp_id.data, email=form.email.data, password=hashed_password) db.session.add(employee) db.session.commit() return render_template('verify_account_instructions.html') return render_template('register.html', form=form)
def account(): username_form = UpdateUsernameForm() password_form = UpdatePasswordForm() profile_pic_form = UpdateProfilePicForm() if password_form.validate_on_submit(): hashed = bcrypt.generate_password_hash( password_form.new_password.data).decode("utf-8") msg = Message('Password Change', sender='*****@*****.**', recipients=[str(temp.email)]) msg.body = "Your password has been updated! Please reply to this e-mail if you did not request this change." mail.send(msg) current_user.modify(password=hashed) current_user.save() return redirect(url_for('users.account')) if username_form.validate_on_submit(): temp = User.objects(username=current_user.username).first() current_user.username = username_form.username.data msg = Message('Username Change', sender='*****@*****.**', recipients=[str(temp.email)]) msg.body = "Your username has been updated!\nYour new username is: " + str( username_form.username.data) mail.send(msg) current_user.modify(username=username_form.username.data) current_user.save() return redirect(url_for('users.account')) if profile_pic_form.validate_on_submit(): img = profile_pic_form.propic.data filename = secure_filename(img.filename) if current_user.profile_pic.get() is None: current_user.profile_pic.put(img.stream, content_type='images/png') else: current_user.profile_pic.replace(img.stream, content_type='images/png') current_user.save() return redirect(url_for('users.account')) image = images(current_user.username) return render_template("account.html", title="Account", username_form=username_form, password_form=password_form, profile_pic_form=profile_pic_form, image=image)
def register(): if current_user.is_authenticated: current_app.logger.info( "User is authenticated. Redirect to main page.") return redirect(url_for("main.index")) form = RegistrationForm() if form.validate_on_submit(): current_app.logger.info( "POST Request hit at /register and form has been validated") current_app.logger.info("Hashing password...") hashed = bcrypt.generate_password_hash( form.password.data).decode("utf-8") current_app.logger.info("Creating new user") user = User(username=form.username.data, password=hashed, interest1=form.firstInterest.data, interest2=form.secondInterest.data, interest3=form.thirdInterest.data) current_app.logger.info("Adding new user to database") db.session.add(user) db.session.commit() current_app.logger.info("Getting GIFS for User {user.username}") firstInterestGIFs = getGIFs(user.interest1) for url in firstInterestGIFs: userGIF = UserGIF(link=url, user=user) db.session.add(userGIF) secondInterestGIFs = getGIFs(user.interest2) for url in secondInterestGIFs: userGIF = UserGIF(link=url, user=user) db.session.add(userGIF) thirdInterestGIFs = getGIFs(user.interest3) for url in thirdInterestGIFs: userGIF = UserGIF(link=url, user=user) db.session.add(userGIF) db.session.commit() current_app.logger.info( "User has been added, as well as their GIF Interests. Redirect to login page." ) #return redirect(url_for("users.login")) session['reg_username'] = user.username return redirect(url_for('users.tfa')) return render_template("register.html", title="Register", form=form)
def create_new_user(): if request.method == 'POST': data = request.json print(data) # check if username/email/password is missing or empty string if not data.get('username') or not data.get('email') or not data.get( 'password'): return jsonify({ 'message': 'username email password cannot be empty, ', 'success': False }), vf.res_code['BAD_REQ'] # check if email exists existing_email = mongo.db.users.find_one({'email': data.get('email')}) if existing_email: return jsonify({ 'message': 'email exists', 'success': False }), vf.vf.res_code['BAD_REQ'] # use bcrypt to hash password and update password_hash = bcrypt.generate_password_hash( data.get('password')).decode('utf-8') timestamp = str(vf.get_timestamp()) new_data = { 'username': data.get('username'), 'email': data.get('email'), 'password': password_hash, 'date_join': timestamp, 'last_login': timestamp } # create new user new_user = mongo.db.users.insert_one(new_data) _id = new_user.inserted_id token = create_access_token(identity=str(_id)) resp = make_response( jsonify({ 'message': 'user registered successfully', 'success': True, 'x-token': token }), vf.res_code['SUCCESS']) resp.headers['x-token'] = token return resp else: return jsonify({ 'message': 'bad request', 'success': False }), vf.res_code['BAD_REQ']
def account(): session['reg_username'] = current_user.username userForm = UpdateUsernameForm() emailForm = UpdateEmailForm() passwordForm = UpdatePasswordForm() print(request.form) if request.method == 'POST': if userForm.validate_on_submit(): current_user.username = userForm.username.data db.session.commit() msg = Message( "Updated Account Username", recipients=[current_user.email], body="Hello user at " + current_user.email + ", you have chosen to change your username to " + current_user.username + "." + "\nIf this was not you, please login to your account to change your account information." ) mail.send(msg) return redirect(url_for('users.account')) elif emailForm.is_submitted() and emailForm.validate_on_submit(): current_user.email = emailForm.email.data db.session.commit() html = render_template('update_email.html', username=current_user.username) subject = "Updated Email" send_email(current_user.email, subject, html) return redirect(url_for('users.account')) elif passwordForm.is_submitted() and passwordForm.validate_on_submit(): hashed = bcrypt.generate_password_hash( passwordForm.new_password.data).decode('utf-8') user = User.query.filter_by(username=current_user.username).first() user.password = hashed db.session.commit() msg = Message( "Updated Account Password", recipients=[current_user.email], body="Hello " + current_user.username + ", you have chosen to change your password." + "\nIf this was not you, please contact us to recover your account information." ) mail.send(msg) logout_user() return redirect(url_for('users.login')) userForm.username.data = current_user.username emailForm.email.data = current_user.email return render_template('account.html', title='Account', userForm=userForm, emailForm=emailForm, passwordForm=passwordForm)
def account(): form = UpdateForm() if form.validate_on_submit(): current_user.username = form.username.data current_user.password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') db.session.commit() return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.password.data = current_user.password return render_template('account.html', title='Account', form=form, current_user=current_user)
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.lower(), 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)
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() #pass a one time message to front end, category is success flash('Your account have now been created, you are now able to login', '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(): 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('login')) return render_template('register.html', title='Register', form=form)
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)
def change_password(): #change password changePasswordForm = ChangePasswordForm() changePasswordForm.validate() errors = changePasswordForm.errors if len(errors) == 0: #change user password hashed_password = bcrypt.generate_password_hash( changePasswordForm.new_password.data).decode('utf-8') current_user.password = hashed_password db.session.commit() flash('Your password has been changed!', 'success') return redirect_json(route='accounts.settings') else: return form_errors_400(changePasswordForm)
def update_user(username, password, type, auto_create=False): user = User.query.filter_by(username=username).first() if not user: if auto_create: return create_user(username, password, type) else: return validation_util.error_message( message="Cannot update user! Invalid User Details") else: user.username = username if password: user.password = bcrypt.generate_password_hash(password) if type: user.type = type db.session.commit() return validation_util.success_message(data=user_schema.dump(user))
def register(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): #user = User.query.filter_by(username=form.username.data).first() hashed = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed) db.session.add(user) db.session.commit() return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def create_user(username, password, type): user = User.query.filter_by(username=username).first() if user: return validation_util.error_message(message="User Already Registered") else: err = validation_util.validate({ "username": username, "password": password, "type": type }) if err: return err user = User(username=username, password=bcrypt.generate_password_hash(password), type=type) db.session.add(user) db.session.commit() return validation_util.success_message(data=user_schema.dump(user))
def add_coordo(): form = AddCoordoForm() if form.validate_on_submit(): password = "".join( random.sample('abcdefghijklmnoparstuvwxyz_./:#{[123456789', 12)) nom = dt.datetime.now().strftime('%f') hashed_password = bcrypt.generate_password_hash(password).decode( 'utf-8') coordo = Coordinateur(nom="NewCoordo-" + nom, email=form.email.data, password=hashed_password) db.session.add(coordo) db.session.commit() send_email_invitation(coordo) flash(f"Email d'invitation envoyé à {form.email.data}", 'info') return redirect(url_for('accueillants.liste_accueillants')) return render_template('add_coordo.html', title='Ajouter un coordinateur', form=form)
def register(): if current_user.is_authenticated: flash('You are already logged in', 'success') return redirect(url_for('home')) form = RegistrationFrom() 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! Your are now able to log in!", 'success') return redirect(url_for('login')) return render_template(template_name_or_list='sign_in.html', form=form, title="Register")
def register(token): if current_user.is_authenticated: return redirect(url_for('main.index')) coordo = Coordinateur.verify_reset_token(token) if coordo is None: flash('Lien invalide ou expiré', 'warning') return redirect(url_for('main.index')) form = RegistrationForm() form.email.data = coordo.email if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') coordo.password = hashed_password coordo.nom = form.username.data db.session.commit() flash(f'Compte créé pour {form.username.data} !', 'success') return redirect(url_for('coordinateurs.login')) return render_template('register.html', title='Register', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('accueillants.liste_accueillants')) coordo = Coordinateur.verify_reset_token(token) if coordo is None: flash('Lien invalide ou expiré', 'warning') return redirect(url_for('coordinateurs.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') coordo.password = hashed_password db.session.commit() flash(f'Votre mot de passe a été modifié.', 'success') return redirect(url_for('coordinateurs.login')) return render_template('reset_token.html', title='Changement de mot de passe', form=form)
def create_db(): db.drop_all() db.create_all() db.session.commit() # Add the admin hashed_password = bcrypt.generate_password_hash(PASS_ADMIN).decode('utf-8') coordo = Coordinateur(nom="admin", email="*****@*****.**", password=hashed_password) db.session.add(coordo) db.session.commit() # Get the data credentials = GOOGLE_APP_CREDS client = connect_to_drive(credentials) coordo_sheet = client.open("flask-Coordo/Mediation").worksheet( "Accueillants") liste_accueillants_raw = coordo_sheet.get_all_values() # Handle poorly formatted emails emails_re = "([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)" tel_re = "([0-9][0-9])" liste_accueillants_to_add = \ [Accueillant( disponibilite=row[0], nom=row[1].title(), tel=''.join("; " if i % 15 == 0 else char for i, char in enumerate( ".".join(re.findall(tel_re, row[2])), 1)), adresse=row[3], email="; ".join(re.findall(emails_re, row[4])).lower(), next_action=row[5], remarques=row[6]) for i, row in enumerate(liste_accueillants_raw) if i > 1] for acc in liste_accueillants_to_add: try: db.session.add(acc) db.session.commit() except: db.session.close()
def setUp(self): db.create_all() test_user = User(username=self.root_username, password=bcrypt.generate_password_hash( self.root_password), type=self.user_type) db.session.add(test_user) db.session.commit() location_a = Location(lat=25, lon=50) location_b = Location(lat=15, lon=20) jogg_1 = Jogg(user_id=test_user.id, start_lat=location_a.lat, start_lon=location_a.lon, end_lat=location_b.lat, end_lon=location_b.lon, start_weather='dummy', end_weather='dummy') db.session.add(jogg_1) db.session.commit()
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): hashed = bcrypt.generate_password_hash( form.password.data).decode("utf-8") mongo_lock.acquire() user = User(username=form.username.data, email=form.email.data, phone_number='+' + str(form.phone.data), password=hashed) user.save() mongo_lock.release() session['new_username'] = user.username return redirect(url_for('users.tfa')) return render_template('register.html', title='Register', form=form)
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_token(token, timed=True) if user is None: flash('The token is invalid or has expired.', 'warning') return redirect_next_page() reset_password_form = ResetPasswordForm() if request.method == "POST": if reset_password_form.validate_on_submit(): print('reset form submitted') hashed_password = bcrypt.generate_password_hash( reset_password_form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Password has been updated!', 'success') #redirect does not work with ajax, so instead return json then use js to switch url return redirect_json(route="main.home") else: return form_errors_400(reset_password_form) return _render_template('accounts/reset_password.html')
def register(): register_form = RegisterationForm() if request.method == "POST" and confirm_post_request_form( request, register_form): if register_form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( register_form.password.data).decode('utf-8') user = User(username=register_form.username.data, password=hashed_password, email=register_form.email.data) #add email verification here db.session.add(user) db.session.commit() send_verification_email(user) #clear form fields flash( 'Your account has been created! Please activate your account before logging in.', 'success') return redirect_json(route="main.home") else: print(register_form.errors) return form_errors_400(register_form) return redirect_next_page()
def register(): if current_user.is_authenticated: return redirect(url_for('main.account')) 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) try: db.session.add(user) db.session.commit() login_user(user) from_user = "******" from_user_image = '655c9f17511a4133.png' message_date = get_cst() message_body = 'You are now registered!\nWelcome to the home of the Renewed Hope guild. From here you can apply to join the guild, update your information or delete your account.' message = UserMessages(from_user=from_user, from_user_image=from_user_image, message_date=message_date, message_body=message_body) message.user_id = current_user.id db.session.add(message) db.session.commit() flash(f'Your account has been created and you are now logged in', 'success') return redirect(url_for('main.account')) except: flash( f'There was a problem regiestering your account. Please try back later.', 'danger') return redirect(url_for('main.index')) return render_template( 'register.html', form=form, title="Register for access to the Renewed Hope Guild Website")