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,7 +14,8 @@ from distribusisinfo import DistribusisInfo
from app import db from app import db
def IsZipUploaded(distribusiname): class UserHelper:
def is_zip_uploaded(distribusiname):
userfolder = os.path.join("stash", distribusiname) userfolder = os.path.join("stash", distribusiname)
if os.path.exists(userfolder): if os.path.exists(userfolder):
zipfilename = "{}.zip".format(distribusiname) zipfilename = "{}.zip".format(distribusiname)
@ -24,8 +25,7 @@ def IsZipUploaded(distribusiname):
print("distribusi folder has no zipfile") print("distribusi folder has no zipfile")
return False return False
def is_distribusi_live(distribusiname):
def IsDistribusiLive(distribusiname):
userfolder = os.path.join("stash", distribusiname) userfolder = os.path.join("stash", distribusiname)
if os.path.exists(userfolder): if os.path.exists(userfolder):
zipfilename = "{}.zip".format(distribusiname) zipfilename = "{}.zip".format(distribusiname)
@ -37,8 +37,7 @@ def IsDistribusiLive(distribusiname):
print("distribusi folder is empty") print("distribusi folder is empty")
return False return False
def is_css_selected(distribusiname):
def IsCssSelected(distribusiname):
userfolder = os.path.join("themes/userthemes", distribusiname) userfolder = os.path.join("themes/userthemes", distribusiname)
if os.path.exists(userfolder): if os.path.exists(userfolder):
for file in os.listdir(userfolder): for file in os.listdir(userfolder):
@ -47,8 +46,7 @@ def IsCssSelected(distribusiname):
print("distribusi folder has no css file") print("distribusi folder has no css file")
return False return False
def has_distribusi():
def HasDistribusi():
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()
if distribusi is None: if distribusi is None:
@ -56,15 +54,13 @@ def HasDistribusi():
return False return False
return True return True
def current_distribusi():
def CurrentDistribusi():
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None: if user.currentdistribusi is None:
return "None" return "None"
return user.currentdistribusi return user.currentdistribusi
def reset_user_state():
def ResetUserState():
"""reset user state upon visiting index, so that distribusi workflow can """reset user state upon visiting index, so that distribusi workflow can
be done correctly""" be done correctly"""
if not current_user.is_authenticated: if not current_user.is_authenticated:
@ -77,12 +73,9 @@ def ResetUserState():
db.session.rollback() db.session.rollback()
flash("An error occured !", "danger") flash("An error occured !", "danger")
def distribusi_limit_reached():
def DistribusiLimitReached():
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
distribusiamount = len( distribusiamount = len(DistribusisInfo.getuserdistribusis(user.email))
DistribusisInfo.getuserdistribusis(user.email)
)
if user.tutor and distribusiamount > 14: if user.tutor and distribusiamount > 14:
print("tutor already has 15 distribusis") print("tutor already has 15 distribusis")
return True return True
@ -90,9 +83,3 @@ def DistribusiLimitReached():
print("user already has 5 distribusis") print("user already has 5 distribusis")
return True return True
return False return False
# def IsThemeSelected
# def IsCustomThemePresent
# def IsDistribusiLive

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,