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

View File

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

View File

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

View File

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

View File

@ -14,85 +14,72 @@ from distribusisinfo import DistribusisInfo
from app import db
def IsZipUploaded(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 True
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"):
class UserHelper:
def is_zip_uploaded(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 True
print("distribusi folder has no css file")
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")
print("distribusi folder has no zipfile")
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():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return "None"
return user.currentdistribusi
def is_css_selected(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
print("distribusi folder has no css file")
return False
def ResetUserState():
"""reset user state upon visiting index, so that distribusi workflow can
be done correctly"""
if not current_user.is_authenticated:
return
try:
def has_distribusi():
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 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")
distribusi = Distribusis.query.filter_by(userid=user.id).first()
if distribusi is None:
print("no distribusi found")
return False
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 IsCustomThemePresent
# def IsDistribusiLive
def distribusi_limit_reached():
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
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 shutil
from flask import render_template
from statuspengguna.helper import (
IsZipUploaded,
IsCssSelected,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
)
from statuspengguna.helper import UserHelper
from distribusisinfo import DistribusisInfo
from forms.uploadform import UploadForm
@ -21,7 +16,7 @@ def ThemeSelector():
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
current_distribusi = CurrentDistribusi()
current_distribusi = UserHelper.current_distribusi()
if themeform.validate_on_submit():
copycssfile = os.path.join(
"themes",
@ -53,11 +48,11 @@ def RenderDistribusiTemplate(themeform, publicthemeform, current_distribusi):
distribusiform = DistribusiForm()
selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = UserHelper.is_css_selected(current_distribusi)
selectorvisible = False
limit_reached = DistribusiLimitReached()
limit_reached = UserHelper.distribusi_limit_reached()
template = render_template(
"distribusi.html",
uploadform=uploadform,

View File

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

View File

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