from flask import Blueprint, render_template, url_for, flash, redirect, request, current_app from chatBot.update.forms import LoginForm, UpdateForm from flask_login import login_user, current_user, logout_user, login_required import os from chatBot import bcrypt, db from chatBot.models import User update = Blueprint('update', __name__) @update.route("/register", methods=['GET', 'POST']) def register(): if current_user.is_authenticated: return redirect(url_for('update.upload')) form = LoginForm() 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(message = "Account created", category = "success") return redirect(url_for('update.login')) return render_template('login.html', title='login', form=form) @update.route("/login", methods=['GET', 'POST']) def login(): if current_user.is_authenticated: return redirect(url_for('update.upload')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user and form.username.data == 'TechTeam' and bcrypt.check_password_hash(pw_hash = user.password, password = form.password.data): login_user(user) next_page = request.args.get('next') flash(message = "Login successful", category = "success") return redirect(next_page) if next_page else redirect(url_for('update.upload')) flash(message = "Login unsuccessful", category = "danger") return render_template('login.html', title='Login', form=form) @update.route("/upload", methods=['GET', 'POST']) @login_required def upload(): form = UpdateForm() if form.validate_on_submit(): if form.file.data: file = form.file.data fileCount = len(os.listdir(current_app.config['PDF_PATH'])) file.save(current_app.config['PDF_PATH'] + str(fileCount + 1) + '.pdf') flash('Your file has been updated!', 'success') return render_template('update.html', title='Update', form=form)