def guard_update(id): user = User.query.get_or_404(id) form = UpdateMessStaffForm() if form.validate_on_submit(): user.college_id = form.college_id.data user.firstname = form.firstname.data user.lastname = form.lastname.data user.address = form.address.data if form.password.data and form.confirm_password.data: hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Account information has been updated!', 'success') return redirect(url_for('guard_list')) elif request.method == 'GET': form.college_id.data = user.college_id form.firstname.data = user.firstname form.lastname.data = user.lastname form.address.data = user.address return render_template('admin/update_guard_info.html', title='Update', form=form, user=user)
def guard_create(): form = CreateMessStaffForm() if form.validate_on_submit(): password = ''.join( random.choices(string.ascii_uppercase + string.digits, k=8)) hashed_password = bcrypt.generate_password_hash(password).decode( 'utf-8') user = User(firstname=form.firstname.data, lastname=form.lastname.data, college_id=form.college_id.data, phone=form.phone.data, address=form.address.data, email=form.email.data, password=password, user_type=2) db.session.add(user) db.session.commit() msg = Message("Password for HosteLite", sender="*****@*****.**", recipients=["*****@*****.**"]) msg.body = "The password for your hostelite account is: " + password + "\nPlease reset your password as soon as you login." mail.send(msg) flash('Account created successfully.', 'success') return redirect(url_for('guard_list')) return render_template('admin/guard_create.html', form=form)
def update_guard_account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.password.data and form.confirm_password.data: current_user.email = form.email.data current_user.phone = form.phone.data current_user.address = form.address.data hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') current_user.password = hashed_password db.session.commit() flash('Password changed! Login with new password.', 'success') return redirect(url_for('logout')) else: current_user.email = form.email.data current_user.phone = form.phone.data current_user.address = form.address.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('update_guard_account')) elif request.method == 'GET': form.email.data = current_user.email form.address.data = current_user.address form.phone.data = current_user.phone return render_template('guard/update.html', title='Account', form=form)
def individual_applications(id): if request.method == 'POST': room = Room.query.filter(Room.capacity > 0).order_by( Room.capacity).first() if room: application = Application.query.get_or_404(id) password = ''.join( random.choices(string.ascii_uppercase + string.digits, k=8)) hashed_password = bcrypt.generate_password_hash(password).decode( 'utf-8') user = User(college_id=application.college_id, firstname=application.firstname, lastname=application.lastname, email=application.email, password=hashed_password, phone=application.phone, address=application.address, course=application.course, department=application.department) room.capacity -= 1 application.status = 1 user.room_no = room.room_no db.session.add(user) db.session.commit() user = User.query.filter_by( college_id=application.college_id).first() if not room.student1_id: room.student1_id = user.id elif not room.student2_id: room.student2_id = user.id else: room.student3_id = user.id db.session.commit() msg = Message("Password for HosteLite", sender="*****@*****.**", recipients=["*****@*****.**"]) msg.body = "The password for your hostelite account is: " + password + "\nPlease reset your password as soon as you login." mail.send(msg) return redirect(url_for('display_applications')) application = Application.query.get_or_404(id) return render_template('admin/individual_application.html', application=application)