refactoring for public theme list

This commit is contained in:
crunk 2022-03-19 16:19:51 +01:00
parent dbcdbb6750
commit 4d754e226e
9 changed files with 209 additions and 84 deletions

View File

@ -0,0 +1,85 @@
import os
import shutil
from flask import flash
from flask_login import current_user
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from usermodel import User
from distribusimodel import Distribusis
from forms.selectorform import SelectorForm
from statuspengguna.helper import HasDistribusi
from app import db
def SelectNewDistribusi():
print("make a new distribusi")
selectorform = SelectorForm()
SelectCurrentDistribusi("new")
return selectorform
def SelectUpdateDistribusi(distribusiname):
print(f"Update this distribusi {distribusiname}")
selectorform = SelectorForm()
SelectCurrentDistribusi(distribusiname)
return selectorform
def DeleteDistribusi(distribusiname):
print(f"delete this distribusi {distribusiname}")
selectorform = SelectorForm()
try:
user = User.query.filter_by(email=current_user.email).first()
distribusi = Distribusis.query.filter_by(
distribusiname=distribusiname
).first()
if distribusi.userid is user.id:
db.session.delete(distribusi)
db.session.commit()
userfolder = os.path.join("stash", distribusi.distribusiname)
shutil.rmtree(userfolder)
cssfolder = os.path.join(
"themes/userthemes", distribusi.distribusiname
)
shutil.rmtree(cssfolder)
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
return selectorform
def SelectCurrentDistribusi(distribusiname):
if not current_user.is_authenticated:
return
user = User.query.filter_by(email=current_user.email).first()
try:
user.currentdistribusi = distribusiname
db.session.commit()
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
flash("An error occured !", "danger")
def DistribusiSelected():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return False
return True
def SelectorVisible():
has_distribusi = HasDistribusi()
distribusi_selected = DistribusiSelected()
if distribusi_selected:
return False
if not has_distribusi:
return False
return True

24
verse/distribusisinfo.py Normal file
View File

@ -0,0 +1,24 @@
from flask_login import current_user
from usermodel import User
from distribusimodel import Distribusis
class DistribusisInfo:
def userdistribusis():
distribusinames = []
user = User.query.filter_by(email=current_user.email).first()
for distribusi in Distribusis.query.filter_by(userid=user.id).all():
distribusinames.append(distribusi.distribusiname)
return distribusinames
def publicthemes():
publicthemes = []
distribusis = Distribusis.query.filter(
Distribusis.publictheme.isnot(None)
).all()
for distribusi in distribusis:
publicthemes.append(
f"{distribusi.publictheme} used in {distribusi.distribusiname}"
)
return publicthemes

View File

@ -14,6 +14,8 @@ from statuspengguna.helper import (
IsDistribusiLive, IsDistribusiLive,
) )
from distribusimodel import Distribusis from distribusimodel import Distribusis
from distribusisinfo import DistribusisInfo
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm from forms.themeform import ThemeForm
@ -51,7 +53,7 @@ def SaveUploadCssFile(editorform, newcssfolder):
if not os.path.exists(newcssfolder): if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder) os.mkdir(newcssfolder)
cssfile = editorform.cssfile.data cssfile = editorform.cssfile.data
cssfile.save(os.path.join(newcssfolder, editorform.cssfile.data.filename)) cssfile.save(os.path.join(newcssfolder, editorform.cssname.data))
def WriteCssToFile(editorform, newcssfolder): def WriteCssToFile(editorform, newcssfolder):
@ -69,35 +71,28 @@ def MakePublicTheme(editorform, current_distribusi):
distribusi = Distribusis.query.filter_by( distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi distribusiname=current_distribusi
).first() ).first()
distribusi.publictheme = editorform.public.data distribusi.publictheme = editorform.cssname.data
db.session.commit() db.session.commit()
except InvalidRequestError: except InvalidRequestError:
db.session.rollback() db.session.rollback()
editorform.public.errors.append("Something went wrong!") editorform.public.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
except DataError: except DataError:
db.session.rollback() db.session.rollback()
editorform.public.errors.append("Invalid Entry") editorform.public.errors.append("Invalid Entry")
flash("Invalid Entry", "warning")
except InterfaceError: except InterfaceError:
db.session.rollback() db.session.rollback()
editorform.public.errors.append( editorform.public.errors.append("Error connecting to the database")
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except DatabaseError: except DatabaseError:
db.session.rollback() db.session.rollback()
editorform.public.errors.append( editorform.public.errors.append("Error connecting to the database")
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
def RenderDistribusiTemplate(current_distribusi): def RenderDistribusiTemplate(current_distribusi):
uploadform = UploadForm() uploadform = UploadForm()
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
themeform = ThemeForm() themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm() selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi) files_uploaded = IsZipUploaded(current_distribusi)

View File

@ -17,4 +17,9 @@ class ThemeForm(FlaskForm):
("wdka", "WdkA"), ("wdka", "WdkA"),
], ],
) )
publicthemes = RadioField(
"Public themes from other distribusi-verse users:"
)
save = SubmitField("Save") save = SubmitField("Save")

View File

@ -1,7 +1,6 @@
"""This is the main flask distribusi page""" """This is the main flask distribusi page"""
import os import os
import zipfile import zipfile
import shutil
from datetime import timedelta from datetime import timedelta
from flask import ( from flask import (
@ -30,15 +29,16 @@ from forms.themeform import ThemeForm
from forms.selectorform import SelectorForm from forms.selectorform import SelectorForm
# CRUD! # CRUD!
from selector import ( from distribusiselector import (
SelectNewDistribusi, SelectNewDistribusi,
SelectUpdateDistribusi, SelectUpdateDistribusi,
DeleteDistribusi, DeleteDistribusi,
SelectorVisible, SelectorVisible,
) )
from editor import Editor
# Upload # Interface!
from editor import Editor
from themeselector import ThemeSelector
from upload import UploadNewDistribusi, UploadUpdatedFiles from upload import UploadNewDistribusi, UploadUpdatedFiles
# UserPengguna # UserPengguna
@ -52,6 +52,7 @@ from statuspengguna.helper import (
) )
from statuspengguna.loginuser import LoginUser from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser from statuspengguna.registeruser import RegisterUser
from distribusisinfo import DistribusisInfo
# Tada! # Tada!
from distribusi.cli import build_argparser from distribusi.cli import build_argparser
@ -104,8 +105,9 @@ def distribusi():
uploadform = UploadForm() uploadform = UploadForm()
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
themeform = ThemeForm() themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm() selectorform = SelectorForm()
selectorform.distribusis.choices = distribusisfields() selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
selectorvisible = SelectorVisible() selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi() current_distribusi = CurrentDistribusi()
@ -164,8 +166,9 @@ def distribusi():
def upload(): def upload():
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
themeform = ThemeForm() themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm() selectorform = SelectorForm()
selectorform.distribusis.choices = distribusisfields() selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
selectorvisible = SelectorVisible() selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi() current_distribusi = CurrentDistribusi()
if current_distribusi == "new" or HasDistribusi() is False: if current_distribusi == "new" or HasDistribusi() is False:
@ -192,42 +195,7 @@ def upload():
@APP.route("/theme", methods=["GET", "POST"]) @APP.route("/theme", methods=["GET", "POST"])
@login_required @login_required
def theme(): def theme():
uploadform = UploadForm() return ThemeSelector()
distribusiform = DistribusiForm()
themeform = ThemeForm()
selectorform = SelectorForm()
selectorform.distribusis.choices = distribusisfields()
selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi()
print(current_distribusi)
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
if themeform.validate_on_submit():
newcssfolder = os.path.join("themes/userthemes", current_distribusi)
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
copycssfile = os.path.join(
"themes",
"{}.css".format(themeform.theme.data),
)
shutil.copy(copycssfile, newcssfolder)
css_selected = IsCssSelected(current_distribusi)
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)
return template
@APP.route("/editor", methods=["GET", "POST"]) @APP.route("/editor", methods=["GET", "POST"])
@ -243,15 +211,18 @@ def selector():
uploadform = UploadForm() uploadform = UploadForm()
distribusiform = DistribusiForm() distribusiform = DistribusiForm()
themeform = ThemeForm() themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm() selectorform = SelectorForm()
selectorform.distribusis.choices = distribusisfields() selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
current_distribusi = CurrentDistribusi() current_distribusi = CurrentDistribusi()
if selectorform.validate_on_submit(): if selectorform.validate_on_submit():
if selectorform.new.data: if selectorform.new.data:
selectorform = SelectNewDistribusi() selectorform = SelectNewDistribusi()
if selectorform.delete.data: if selectorform.delete.data:
selectorform = DeleteDistribusi(selectorform.distribusis.data) selectorform = DeleteDistribusi(selectorform.distribusis.data)
selectorform.distribusis.choices = distribusisfields() selectorform.distribusis.choices = (
DistribusisInfo.userdistribusis()
)
if selectorform.update.data: if selectorform.update.data:
selectorform = SelectUpdateDistribusi( selectorform = SelectUpdateDistribusi(
selectorform.distribusis.data selectorform.distribusis.data
@ -328,14 +299,6 @@ def load_user(user_id):
return User.query.get(int(user_id)) return User.query.get(int(user_id))
def distribusisfields():
distribusinames = []
user = User.query.filter_by(email=current_user.email).first()
for distribusi in Distribusis.query.filter_by(userid=user.id).all():
distribusinames.append(distribusi.distribusiname)
return distribusinames
if __name__ == "__main__": if __name__ == "__main__":
APP.debug = True APP.debug = True
APP.run(port=5000) APP.run(port=5000)

View File

@ -63,13 +63,6 @@ def CurrentDistribusi():
return user.currentdistribusi return user.currentdistribusi
def DistribusiSelected():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return False
return True
def ResetUserState(): 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"""
@ -84,16 +77,7 @@ def ResetUserState():
flash("An error occured !", "danger") flash("An error occured !", "danger")
def SelectCurrentDistribusi(distribusiname):
if not current_user.is_authenticated:
return
user = User.query.filter_by(email=current_user.email).first()
try:
user.currentdistribusi = distribusiname
db.session.commit()
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
flash("An error occured !", "danger")
# def IsThemeSelected # def IsThemeSelected

View File

@ -8,7 +8,11 @@
{{ themeform.theme.label }} {{ themeform.theme.label }}
{{ themeform.theme }} {{ themeform.theme }}
</fieldset> </fieldset>
{% if files_uploaded or distribusi_live %} <fieldset class="required">
{{ themeform.publicthemes.label }}
{{ themeform.publicthemes }}
</fieldset>
{% if files_uploaded or distribusi_live %}
<fieldset class="button required"> <fieldset class="button required">
{{ themeform.save }} {{ themeform.save }}
</fieldset> </fieldset>

66
verse/themeselector.py Normal file
View File

@ -0,0 +1,66 @@
import os
import shutil
from flask import render_template
from statuspengguna.helper import (
IsZipUploaded,
IsCssSelected,
CurrentDistribusi,
IsDistribusiLive,
)
from distribusisinfo import DistribusisInfo
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
from forms.selectorform import SelectorForm
def ThemeSelector():
themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
current_distribusi = CurrentDistribusi()
if themeform.validate_on_submit():
newcssfolder = os.path.join("themes/userthemes", current_distribusi)
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
if themeform.theme.data:
copycssfile = os.path.join(
"themes",
f"{themeform.theme.data}.css",
)
if themeform.publicthemes.data:
publictheme = themeform.publicthemes.data
copycssfile = os.path.join(
"themes/publicthemes",
f"{distribusiname}",
f"{themename}.css",
)
shutil.copy(copycssfile, newcssfolder)
return RenderDistribusiTemplate(current_distribusi)
def RenderDistribusiTemplate(current_distribusi):
uploadform = UploadForm()
distribusiform = DistribusiForm()
themeform = ThemeForm()
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
selectorvisible = False
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)
return template

View File

@ -15,7 +15,7 @@ from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
from statuspengguna.helper import CurrentDistribusi from statuspengguna.helper import CurrentDistribusi
from statuspengguna.helper import SelectCurrentDistribusi from distribusiselector import SelectCurrentDistribusi
from forms.uploadform import UploadForm from forms.uploadform import UploadForm
@ -77,7 +77,6 @@ def UploadUpdatedFiles(uploadfolder):
except (InvalidRequestError, DataError, InterfaceError, DatabaseError): except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!") uploadform.sitename.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
zipfilename = "{}.zip".format(distribusi.distribusiname) zipfilename = "{}.zip".format(distribusi.distribusiname)
zipfile = uploadform.zipfile.data zipfile = uploadform.zipfile.data