refactored user status functions to class

This commit is contained in:
crunk 2022-03-28 20:16:34 +02:00
parent f574f67dba
commit db84c3eb77
8 changed files with 99 additions and 140 deletions

View File

@ -20,13 +20,7 @@ from forms.themeform import ThemeForm
from forms.publicthemeform import PublicThemeForm from forms.publicthemeform import PublicThemeForm
# UserPengguna # UserPengguna
from statuspengguna.helper import ( from statuspengguna.helper import UserHelper
IsZipUploaded,
CurrentDistribusi,
HasDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from app import db from app import db
@ -35,7 +29,7 @@ def DistribusiSelector():
uploadform = UploadForm() uploadform = UploadForm()
selectorform = SelectorForm() selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames() selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
if selectorform.validate_on_submit(): if selectorform.validate_on_submit():
if selectorform.new.data: if selectorform.new.data:
SelectNewDistribusi() SelectNewDistribusi()
@ -46,7 +40,7 @@ def DistribusiSelector():
) )
if selectorform.update.data: if selectorform.update.data:
SelectUpdateDistribusi(selectorform.distribusis.data) SelectUpdateDistribusi(selectorform.distribusis.data)
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
uploadform = AutoFillInUploadForm(uploadform, current_distribusi) uploadform = AutoFillInUploadForm(uploadform, current_distribusi)
return RenderDistribusiTemplate( return RenderDistribusiTemplate(
@ -128,7 +122,7 @@ def DistribusiSelected():
def SelectorVisible(): def SelectorVisible():
has_distribusi = HasDistribusi() has_distribusi = UserHelper.has_distribusi()
distribusi_selected = DistribusiSelected() distribusi_selected = DistribusiSelected()
if distribusi_selected: if distribusi_selected:
return False return False
@ -142,14 +136,14 @@ def RenderDistribusiTemplate(selectorform, uploadform, current_distribusi):
themeform = ThemeForm() themeform = ThemeForm()
publicthemeform = PublicThemeForm() publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes() publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
# because the user has chosen to update his distribusi, we assume # because the user has chosen to update his distribusi, we assume
# no selected css. # no selected css.
css_selected = False css_selected = False
selectorvisible = SelectorVisible() selectorvisible = SelectorVisible()
limit_reached = DistribusiLimitReached() limit_reached = UserHelper.distribusi_limit_reached()
template = render_template( template = render_template(
"distribusi.html", "distribusi.html",
uploadform=uploadform, uploadform=uploadform,

View File

@ -20,14 +20,7 @@ from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
# UserPengguna # UserPengguna
from statuspengguna.helper import ( from statuspengguna.helper import UserHelper
IsZipUploaded,
IsCssSelected,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from distribusiselector import SelectorVisible from distribusiselector import SelectorVisible
# Forms! # Forms!
@ -46,7 +39,7 @@ from distribusi.distribusi import distribusify
def DistribusiWorkflow(): def DistribusiWorkflow():
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
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( distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi distribusiname=current_distribusi
@ -135,10 +128,10 @@ def RenderDistribusiTemplate(distribusiform, current_distribusi):
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames() selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
selectorvisible = SelectorVisible() selectorvisible = SelectorVisible()
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = IsCssSelected(current_distribusi) css_selected = UserHelper.is_css_selected(current_distribusi)
limit_reached = DistribusiLimitReached() limit_reached = UserHelper.distribusi_limit_reached()
template = render_template( template = render_template(
"distribusi.html", "distribusi.html",
uploadform=uploadform, uploadform=uploadform,

View File

@ -12,14 +12,9 @@ from sqlalchemy.exc import (
InvalidRequestError, InvalidRequestError,
) )
from app import db from app import db
from statuspengguna.helper import (
IsZipUploaded,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from distribusimodel import Distribusis from distribusimodel import Distribusis
from statuspengguna.helper import UserHelper
from distribusisinfo import DistribusisInfo from distribusisinfo import DistribusisInfo
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
@ -32,7 +27,7 @@ from forms.selectorform import SelectorForm
def Editor(): def Editor():
editorform = EditorForm() editorform = EditorForm()
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
if editorform.validate_on_submit(): if editorform.validate_on_submit():
ValidateEditCssForm(editorform, current_distribusi) ValidateEditCssForm(editorform, current_distribusi)
return RenderDistribusiTemplate(current_distribusi) return RenderDistribusiTemplate(current_distribusi)
@ -134,11 +129,11 @@ def RenderDistribusiTemplate(current_distribusi):
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes() publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm() selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = True css_selected = True
selectorvisible = False selectorvisible = False
limit_reached = DistribusiLimitReached() limit_reached = UserHelper.distribusi_limit_reached()
template = render_template( template = render_template(
"distribusi.html", "distribusi.html",
uploadform=uploadform, uploadform=uploadform,
@ -161,8 +156,8 @@ def RenderEditorTemplate(editorform, current_distribusi):
cssplaceholder = CssPlaceholder(current_distribusi) cssplaceholder = CssPlaceholder(current_distribusi)
editorform.css.data = cssplaceholder editorform.css.data = cssplaceholder
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
template = render_template( template = render_template(
"editor.html", "editor.html",
files_uploaded=files_uploaded, files_uploaded=files_uploaded,

View File

@ -32,11 +32,13 @@ from distribusiselector import DistribusiSelector
from uploadpage import UploadPage from uploadpage import UploadPage
# UserPengguna # UserPengguna
from statuspengguna.helper import ResetUserState from statuspengguna.helper import UserHelper
from statuspengguna.loginuser import LoginUser from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser from statuspengguna.registeruser import RegisterUser
from statuspengguna.forgotpassword import ForgotPassword from statuspengguna.forgotpassword import ForgotPassword
from statuspengguna.resetpassword import ResetPassword from statuspengguna.resetpassword import ResetPassword
# Distribusi Information
from distribusisinfo import DistribusisInfo from distribusisinfo import DistribusisInfo
APP = create_app() APP = create_app()
@ -53,7 +55,7 @@ def session_handler():
@APP.route("/") @APP.route("/")
def index(): def index():
ResetUserState() UserHelper.reset_user_state()
# http://localhost:5000/themes/publicthemes/RomeroTape/blueskies.css # http://localhost:5000/themes/publicthemes/RomeroTape/blueskies.css
uploadform = UploadForm() uploadform = UploadForm()
distribusis = DistribusisInfo.visibledistribusis() distribusis = DistribusisInfo.visibledistribusis()

View File

@ -14,85 +14,72 @@ from distribusisinfo import DistribusisInfo
from app import db from app import db
def IsZipUploaded(distribusiname): class UserHelper:
userfolder = os.path.join("stash", distribusiname) def is_zip_uploaded(distribusiname):
if os.path.exists(userfolder): userfolder = os.path.join("stash", distribusiname)
zipfilename = "{}.zip".format(distribusiname) if os.path.exists(userfolder):
if os.path.exists(os.path.join(userfolder, zipfilename)): zipfilename = "{}.zip".format(distribusiname)
print("folder with zipfile found, file uploaded") if os.path.exists(os.path.join(userfolder, zipfilename)):
return True print("folder with zipfile found, file uploaded")
print("distribusi folder has no zipfile")
return False
def IsDistribusiLive(distribusiname):
userfolder = os.path.join("stash", distribusiname)
if os.path.exists(userfolder):
zipfilename = "{}.zip".format(distribusiname)
if os.path.exists(os.path.join(userfolder, zipfilename)):
print("folder with zipfile found, file uploaded")
return False
if len(os.listdir(userfolder)) > 0:
return True
print("distribusi folder is empty")
return False
def IsCssSelected(distribusiname):
userfolder = os.path.join("themes/userthemes", distribusiname)
if os.path.exists(userfolder):
for file in os.listdir(userfolder):
if file.endswith(".css"):
return True return True
print("distribusi folder has no css file") print("distribusi folder has no zipfile")
return False
def HasDistribusi():
user = User.query.filter_by(email=current_user.email).first()
distribusi = Distribusis.query.filter_by(userid=user.id).first()
if distribusi is None:
print("no distribusi found")
return False return False
return True
def is_distribusi_live(distribusiname):
userfolder = os.path.join("stash", distribusiname)
if os.path.exists(userfolder):
zipfilename = "{}.zip".format(distribusiname)
if os.path.exists(os.path.join(userfolder, zipfilename)):
print("folder with zipfile found, file uploaded")
return False
if len(os.listdir(userfolder)) > 0:
return True
print("distribusi folder is empty")
return False
def CurrentDistribusi(): def is_css_selected(distribusiname):
user = User.query.filter_by(email=current_user.email).first() userfolder = os.path.join("themes/userthemes", distribusiname)
if user.currentdistribusi is None: if os.path.exists(userfolder):
return "None" for file in os.listdir(userfolder):
return user.currentdistribusi if file.endswith(".css"):
return True
print("distribusi folder has no css file")
return False
def has_distribusi():
def ResetUserState():
"""reset user state upon visiting index, so that distribusi workflow can
be done correctly"""
if not current_user.is_authenticated:
return
try:
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
user.currentdistribusi = None distribusi = Distribusis.query.filter_by(userid=user.id).first()
db.session.commit() if distribusi is None:
except (InvalidRequestError, DataError, InterfaceError, DatabaseError): print("no distribusi found")
db.session.rollback() return False
flash("An error occured !", "danger")
def DistribusiLimitReached():
user = User.query.filter_by(email=current_user.email).first()
distribusiamount = len(
DistribusisInfo.getuserdistribusis(user.email)
)
if user.tutor and distribusiamount > 14:
print("tutor already has 15 distribusis")
return True return True
if not user.tutor and distribusiamount > 4:
print("user already has 5 distribusis")
return True
return False
def current_distribusi():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return "None"
return user.currentdistribusi
def reset_user_state():
"""reset user state upon visiting index, so that distribusi workflow can
be done correctly"""
if not current_user.is_authenticated:
return
try:
user = User.query.filter_by(email=current_user.email).first()
user.currentdistribusi = None
db.session.commit()
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
flash("An error occured !", "danger")
# def IsThemeSelected def distribusi_limit_reached():
# def IsCustomThemePresent user = User.query.filter_by(email=current_user.email).first()
# def IsDistribusiLive distribusiamount = len(DistribusisInfo.getuserdistribusis(user.email))
if user.tutor and distribusiamount > 14:
print("tutor already has 15 distribusis")
return True
if not user.tutor and distribusiamount > 4:
print("user already has 5 distribusis")
return True
return False

View File

@ -1,13 +1,8 @@
import os import os
import shutil import shutil
from flask import render_template from flask import render_template
from statuspengguna.helper import (
IsZipUploaded, from statuspengguna.helper import UserHelper
IsCssSelected,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from distribusisinfo import DistribusisInfo from distribusisinfo import DistribusisInfo
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
@ -21,7 +16,7 @@ def ThemeSelector():
themeform = ThemeForm() themeform = ThemeForm()
publicthemeform = PublicThemeForm() publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes() publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
if themeform.validate_on_submit(): if themeform.validate_on_submit():
copycssfile = os.path.join( copycssfile = os.path.join(
"themes", "themes",
@ -53,11 +48,11 @@ def RenderDistribusiTemplate(themeform, publicthemeform, current_distribusi):
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
selectorform = SelectorForm() selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = IsCssSelected(current_distribusi) css_selected = UserHelper.is_css_selected(current_distribusi)
selectorvisible = False selectorvisible = False
limit_reached = DistribusiLimitReached() limit_reached = UserHelper.distribusi_limit_reached()
template = render_template( template = render_template(
"distribusi.html", "distribusi.html",
uploadform=uploadform, uploadform=uploadform,

View File

@ -14,7 +14,7 @@ from app import db
from usermodel import User from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
from statuspengguna.helper import CurrentDistribusi from statuspengguna.helper import UserHelper
from distribusiselector import SelectCurrentDistribusi from distribusiselector import SelectCurrentDistribusi
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
@ -67,7 +67,7 @@ def UploadUpdatedFiles(uploadfolder):
uploadform = UploadForm() uploadform = UploadForm()
if uploadform.validate_on_submit(): if uploadform.validate_on_submit():
try: try:
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
distribusi = Distribusis.query.filter_by( distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi distribusiname=current_distribusi
).first() ).first()

View File

@ -6,14 +6,7 @@ from forms.publicthemeform import PublicThemeForm
from forms.selectorform import SelectorForm from forms.selectorform import SelectorForm
# UserPengguna # UserPengguna
from statuspengguna.helper import ( from statuspengguna.helper import UserHelper
IsZipUploaded,
CurrentDistribusi,
HasDistribusi,
IsDistribusiLive,
IsCssSelected,
DistribusiLimitReached,
)
from upload import UploadNewDistribusi, UploadUpdatedFiles from upload import UploadNewDistribusi, UploadUpdatedFiles
from distribusisinfo import DistribusisInfo from distribusisinfo import DistribusisInfo
@ -30,16 +23,16 @@ def UploadPage(uploadfolder):
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames() selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
selectorvisible = SelectorVisible() selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi() current_distribusi = UserHelper.current_distribusi()
if current_distribusi == "new" or HasDistribusi() is False: if current_distribusi == "new" or HasDistribusi() is False:
uploadform = UploadNewDistribusi(uploadfolder) uploadform = UploadNewDistribusi(uploadfolder)
else: else:
uploadform = UploadUpdatedFiles(uploadfolder) uploadform = UploadUpdatedFiles(uploadfolder)
files_uploaded = IsZipUploaded(uploadform.sitename.data) files_uploaded = UserHelper.is_zip_uploaded(uploadform.sitename.data)
distribusi_live = IsDistribusiLive(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = IsCssSelected(current_distribusi) css_selected = UserHelper.is_css_selected(current_distribusi)
limit_reached = DistribusiLimitReached() limit_reached = UserHelper.distribusi_limit_reached()
template = render_template( template = render_template(
"distribusi.html", "distribusi.html",
uploadform=uploadform, uploadform=uploadform,