|
|
|
"""This is the main flask distribusi page"""
|
|
|
|
from flask import (
|
|
|
|
render_template,
|
|
|
|
redirect,
|
|
|
|
request,
|
|
|
|
flash,
|
|
|
|
url_for,
|
|
|
|
session,
|
|
|
|
abort,
|
|
|
|
)
|
|
|
|
from sqlalchemy.exc import (
|
|
|
|
IntegrityError,
|
|
|
|
DataError,
|
|
|
|
DatabaseError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
|
|
|
)
|
|
|
|
from flask_login import (
|
|
|
|
login_user,
|
|
|
|
logout_user,
|
|
|
|
login_required,
|
|
|
|
current_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
from werkzeug.routing import BuildError
|
|
|
|
from flask_bcrypt import generate_password_hash, check_password_hash
|
|
|
|
from flask_wtf.csrf import CSRFError
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from app import create_app, db, login_manager
|
|
|
|
from usermodel import User
|
|
|
|
from loginform import LoginForm
|
|
|
|
from registerform import RegisterForm
|
|
|
|
|
|
|
|
APP = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
@APP.before_request
|
|
|
|
def session_handler():
|
|
|
|
session.permanent = True
|
|
|
|
APP.permanent_session_lifetime = timedelta(minutes=1)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/")
|
|
|
|
def index():
|
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
loginform = LoginForm()
|
|
|
|
if loginform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
user = User.query.filter_by(email=loginform.email.data).first()
|
|
|
|
if check_password_hash(user.pwd, loginform.password.data):
|
|
|
|
login_user(user)
|
|
|
|
flash("Logged in successfully.", "success")
|
|
|
|
next = request.args.get("next")
|
|
|
|
if next is not None and not is_safe_url(next):
|
|
|
|
return abort(400)
|
|
|
|
return redirect(next or url_for("index"))
|
|
|
|
else:
|
|
|
|
flash("Invalid Username or password!", "danger")
|
|
|
|
except Exception as e:
|
|
|
|
flash(e, "danger")
|
|
|
|
return render_template("login.html", loginform=loginform)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
registerform = RegisterForm()
|
|
|
|
if registerform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
email = registerform.email.data
|
|
|
|
pwd = registerform.confirmpassword.data
|
|
|
|
|
|
|
|
newuser = User(
|
|
|
|
email=email,
|
|
|
|
pwd=generate_password_hash(pwd),
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(newuser)
|
|
|
|
db.session.commit()
|
|
|
|
flash("Account Succesfully created", "success")
|
|
|
|
return redirect(url_for("login"))
|
|
|
|
|
|
|
|
except InvalidRequestError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Something went wrong!", "danger")
|
|
|
|
except IntegrityError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("User already exists!.", "warning")
|
|
|
|
except DataError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Invalid Entry", "warning")
|
|
|
|
except InterfaceError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Error connecting to the database", "danger")
|
|
|
|
except DatabaseError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("Error connecting to the database", "danger")
|
|
|
|
except BuildError:
|
|
|
|
db.session.rollback()
|
|
|
|
flash("An error occured !", "danger")
|
|
|
|
return render_template("register.html", registerform=registerform)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/distribusi")
|
|
|
|
@login_required
|
|
|
|
def distribusi():
|
|
|
|
return "distribusi"
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/admin")
|
|
|
|
@login_required
|
|
|
|
def admin():
|
|
|
|
return "admin"
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/logout")
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
|
|
|
|
|
|
|
@APP.errorhandler(CSRFError)
|
|
|
|
def handle_csrf_error(e):
|
|
|
|
return render_template("csrf_error.html", reason=e.description), 400
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
return User.query.get(int(user_id))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
APP.debug = True
|
|
|
|
APP.run(port=5000)
|