Browse Source

refactoring register and login user moved to user status folder

current_wdka_release
crunk 3 years ago
parent
commit
0cd5278272
  1. 103
      verse/start.py
  2. 1
      verse/statuspengguna/helper.py
  3. 20
      verse/statuspengguna/loginuser.py
  4. 68
      verse/statuspengguna/registeruser.py

103
verse/start.py

@ -8,29 +8,22 @@ from datetime import timedelta
from flask import ( from flask import (
render_template, render_template,
redirect, redirect,
request,
flash, flash,
url_for, url_for,
session, session,
abort,
send_from_directory, send_from_directory,
) )
from sqlalchemy.exc import ( from sqlalchemy.exc import (
IntegrityError, IntegrityError,
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError, InvalidRequestError,
) )
from flask_login import ( from flask_login import (
login_user,
logout_user, logout_user,
login_required, login_required,
current_user, current_user,
) )
from werkzeug.routing import BuildError from flask_bcrypt import generate_password_hash
from flask_bcrypt import generate_password_hash, check_password_hash
from flask_wtf.csrf import CSRFError from flask_wtf.csrf import CSRFError
from app import create_app, db, login_manager from app import create_app, db, login_manager
@ -38,7 +31,6 @@ from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
# Forms! # Forms!
from forms.loginform import LoginForm
from forms.registerform import RegisterForm from forms.registerform import RegisterForm
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm from forms.distribusiform import DistribusiForm
@ -46,6 +38,8 @@ from forms.themeform import ThemeForm
from forms.editorform import EditorForm from forms.editorform import EditorForm
from statuspengguna.helper import AreFilesUploaded from statuspengguna.helper import AreFilesUploaded
from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser
# Tada! # Tada!
from distribusi.cli import build_argparser from distribusi.cli import build_argparser
@ -72,81 +66,6 @@ def index():
return render_template("index.html", distribusies=distribusies) return render_template("index.html", distribusies=distribusies)
@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 user is None:
loginform.password.errors.append("Invalid email or password!")
return render_template("login.html", loginform=loginform)
if check_password_hash(user.password, 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): # noqa: F821
return abort(400)
return redirect(next or url_for("index"))
else:
flash("Invalid email or password!", "danger")
loginform.password.errors.append("Invalid email or password!")
return render_template("login.html", loginform=loginform)
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
password = registerform.confirmpassword.data
newuser = User(
email=email,
password=generate_password_hash(password),
)
db.session.add(newuser)
db.session.commit()
flash("Account Succesfully created", "success")
login_user(newuser)
return redirect(url_for("index"))
except InvalidRequestError:
db.session.rollback()
registerform.email.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
except IntegrityError:
db.session.rollback()
registerform.email.errors.append("User already exists!")
flash("User already exists!", "warning")
except DataError:
db.session.rollback()
registerform.email.errors.append("Invalid Entry")
flash("Invalid Entry", "warning")
except InterfaceError:
db.session.rollback()
registerform.email.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except DatabaseError:
db.session.rollback()
registerform.email.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except BuildError:
db.session.rollback()
registerform.email.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
return render_template("register.html", registerform=registerform)
@APP.route("/distribusi", methods=["GET", "POST"]) @APP.route("/distribusi", methods=["GET", "POST"])
@login_required @login_required
def distribusi(): def distribusi():
@ -213,7 +132,9 @@ def upload():
flash("Something went wrong!", "danger") flash("Something went wrong!", "danger")
except IntegrityError: except IntegrityError:
db.session.rollback() db.session.rollback()
uploadform.sitename.errors.append("distribusi name already exists!") uploadform.sitename.errors.append(
"distribusi name already exists!"
)
flash("distribusi name already exists!", "warning") flash("distribusi name already exists!", "warning")
zipfilename = "{}.zip".format(newdistribusi.distribusiname) zipfilename = "{}.zip".format(newdistribusi.distribusiname)
@ -304,6 +225,18 @@ def logout():
return redirect(url_for("index")) return redirect(url_for("index"))
@APP.route("/login", methods=["GET", "POST"])
def login():
result = LoginUser()
return result
@APP.route("/register", methods=["GET", "POST"])
def register():
result = RegisterUser()
return result
@APP.errorhandler(CSRFError) @APP.errorhandler(CSRFError)
def handle_csrf_error(e): def handle_csrf_error(e):
return render_template("csrf_error.html", reason=e.description), 400 return render_template("csrf_error.html", reason=e.description), 400

1
verse/statuspengguna/helper.py

@ -3,6 +3,7 @@ from flask_login import current_user
from usermodel import User from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
def AreFilesUploaded(): def AreFilesUploaded():
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
distribusi = Distribusis.query.filter_by(userid=user.id).first() distribusi = Distribusis.query.filter_by(userid=user.id).first()

20
verse/statuspengguna/loginuser.py

@ -1,7 +1,15 @@
import os from flask import (
from flask_login import current_user render_template,
redirect,
request,
flash,
url_for,
abort,
)
from usermodel import User from usermodel import User
from forms.loginform import LoginForm from forms.loginform import LoginForm
from flask_login import login_user
from flask_bcrypt import check_password_hash
def LoginUser(): def LoginUser():
@ -15,10 +23,14 @@ def LoginUser():
if check_password_hash(user.password, loginform.password.data): if check_password_hash(user.password, loginform.password.data):
login_user(user) login_user(user)
flash("Logged in successfully.", "success") flash("Logged in successfully.", "success")
next = request.args.get("next")
if next is not None and not is_safe_url(next): # noqa: F821
return abort(400)
return redirect(next or url_for("index"))
else: else:
flash("Invalid email or password!", "danger") flash("Invalid email or password!", "danger")
loginform.password.errors.append("Invalid email or password!") loginform.password.errors.append("Invalid email or password!")
return loginform return render_template("login.html", loginform=loginform)
except Exception as e: except Exception as e:
flash(e, "danger") flash(e, "danger")
return loginform return render_template("login.html", loginform=loginform)

68
verse/statuspengguna/registeruser.py

@ -0,0 +1,68 @@
from flask import (
render_template,
redirect,
flash,
url_for,
)
from sqlalchemy.exc import (
IntegrityError,
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from werkzeug.routing import BuildError
from usermodel import User
from forms.registerform import RegisterForm
from flask_login import login_user
from flask_bcrypt import generate_password_hash
from app import db
def RegisterUser():
registerform = RegisterForm()
if registerform.validate_on_submit():
try:
email = registerform.email.data
password = registerform.confirmpassword.data
newuser = User(
email=email,
password=generate_password_hash(password),
)
db.session.add(newuser)
db.session.commit()
flash("Account Succesfully created", "success")
login_user(newuser)
return redirect(url_for("index"))
except InvalidRequestError:
db.session.rollback()
registerform.email.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
except IntegrityError:
db.session.rollback()
registerform.email.errors.append("User already exists!")
flash("User already exists!", "warning")
except DataError:
db.session.rollback()
registerform.email.errors.append("Invalid Entry")
flash("Invalid Entry", "warning")
except InterfaceError:
db.session.rollback()
registerform.email.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except DatabaseError:
db.session.rollback()
registerform.email.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except BuildError:
db.session.rollback()
registerform.email.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
return render_template("register.html", registerform=registerform)
Loading…
Cancel
Save