def register(): user = User.query.all() checkAdmin = 1 for u in user: if u.is_admin == True: checkAdmin = 0 break if current_user.is_authenticated: return redirect(url_for('check')) form = RegistrationForm() if form.validate_on_submit(): if form.username.data == 'test': flash('This username cannot be taken', 'danger') else: 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('Wait for admin approval', 'info') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form, check=checkAdmin)
def admin_register(key): if '1800' == key: user = User.query.all() check = 1 for u in user: if u.is_admin == True: check = 0 break if check == 1: if current_user.is_authenticated: return redirect(url_for('checkUser')) 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, is_admin=True, is_active=True, is_manager=True) db.session.add(user) db.session.commit() flash('You are now admin.', 'info') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form) elif check == 0: flash('Admin account already exists', 'info') return redirect(url_for('login')) else: render_template('error.html', error=404)
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('Akun berhasil dibuat! Silahkan Login', 'is-success') return redirect(url_for('auth.login')) return render_template('auth/register.html', title='Register', form=form)
def change_pass(): form = ChangePasswordForm() if form.validate_on_submit(): if bcrypt.check_password_hash(current_user.password, form.current_password.data): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') current_user.password = hashed_password db.session.commit() flash('Password changed successfully', 'success') return redirect(url_for('account')) else: flash('Unsuccessful. Try again', 'danger') return render_template('change_pass.html', form=form)
def add_users(): form=AddUsersForm() if current_user.user_type == 'admin': if form.validate_on_submit(): current_num = len(User.query.filter_by(user_type=form.user_type.data).all()) log_id = f"{form.user_type.data[0:2]}{current_num+1}" password = bcrypt.generate_password_hash('123456').decode('utf-8') new_user = User(login_id=log_id, user_type=form.user_type.data, name=form.name.data, email=form.email.data, school=current_user.school, password=password) db.session.add(new_user) db.session.commit() flash(f'{new_user.login_id} is added', 'success') return redirect(url_for('add_users')) return render_template('add_users.html', form=form, title='Account')
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('auth.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('Password Anda telah diupdate! silahkan login', 'is-success') return redirect(url_for('auth.login')) return render_template('auth/reset_token.html', title='Reset Password', 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('Your account has been created! You are now able to log in', 'is-success') return redirect(url_for('admin.login')) return render_template('admin/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(): current_num = len(User.query.filter_by(user_type='admin').all()) log_id = f"ad{current_num+1}" hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') admin = User(login_id=log_id,user_type='admin', email=form.email.data, name=form.name.data, school=form.school.data, password=hashed_password) db.session.add(admin) db.session.commit() login_user(admin) flash(f'{form.school.data} is registered!', 'success') flash(f'Your login id is {admin.login_id}', 'success') return redirect(url_for('about')) return render_template('register.html', title='Register', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('login')) user = User.verify_reset_token(token) if user is None: flash('That is an 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).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('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def on_model_change(self, form, model, is_create): model.password = bcrypt.generate_password_hash( model.password).decode('utf-8') return current_user.is_authenticated