refactoring for public theme list
This commit is contained in:
parent
dbcdbb6750
commit
4d754e226e
85
verse/distribusiselector.py
Normal file
85
verse/distribusiselector.py
Normal 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
24
verse/distribusisinfo.py
Normal 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
|
@ -14,6 +14,8 @@ from statuspengguna.helper import (
|
||||
IsDistribusiLive,
|
||||
)
|
||||
from distribusimodel import Distribusis
|
||||
from distribusisinfo import DistribusisInfo
|
||||
|
||||
from forms.uploadform import UploadForm
|
||||
from forms.distribusiform import DistribusiForm
|
||||
from forms.themeform import ThemeForm
|
||||
@ -51,7 +53,7 @@ def SaveUploadCssFile(editorform, newcssfolder):
|
||||
if not os.path.exists(newcssfolder):
|
||||
os.mkdir(newcssfolder)
|
||||
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):
|
||||
@ -69,35 +71,28 @@ def MakePublicTheme(editorform, current_distribusi):
|
||||
distribusi = Distribusis.query.filter_by(
|
||||
distribusiname=current_distribusi
|
||||
).first()
|
||||
distribusi.publictheme = editorform.public.data
|
||||
distribusi.publictheme = editorform.cssname.data
|
||||
db.session.commit()
|
||||
|
||||
except InvalidRequestError:
|
||||
db.session.rollback()
|
||||
editorform.public.errors.append("Something went wrong!")
|
||||
flash("Something went wrong!", "danger")
|
||||
except DataError:
|
||||
db.session.rollback()
|
||||
editorform.public.errors.append("Invalid Entry")
|
||||
flash("Invalid Entry", "warning")
|
||||
except InterfaceError:
|
||||
db.session.rollback()
|
||||
editorform.public.errors.append(
|
||||
"Error connecting to the database"
|
||||
)
|
||||
flash("Error connecting to the database", "danger")
|
||||
editorform.public.errors.append("Error connecting to the database")
|
||||
except DatabaseError:
|
||||
db.session.rollback()
|
||||
editorform.public.errors.append(
|
||||
"Error connecting to the database"
|
||||
)
|
||||
flash("Error connecting to the database", "danger")
|
||||
editorform.public.errors.append("Error connecting to the database")
|
||||
|
||||
|
||||
def RenderDistribusiTemplate(current_distribusi):
|
||||
uploadform = UploadForm()
|
||||
distribusiform = DistribusiForm()
|
||||
themeform = ThemeForm()
|
||||
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
|
||||
selectorform = SelectorForm()
|
||||
|
||||
files_uploaded = IsZipUploaded(current_distribusi)
|
||||
|
@ -17,4 +17,9 @@ class ThemeForm(FlaskForm):
|
||||
("wdka", "WdkA"),
|
||||
],
|
||||
)
|
||||
|
||||
publicthemes = RadioField(
|
||||
"Public themes from other distribusi-verse users:"
|
||||
)
|
||||
|
||||
save = SubmitField("Save")
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""This is the main flask distribusi page"""
|
||||
import os
|
||||
import zipfile
|
||||
import shutil
|
||||
from datetime import timedelta
|
||||
|
||||
from flask import (
|
||||
@ -30,15 +29,16 @@ from forms.themeform import ThemeForm
|
||||
from forms.selectorform import SelectorForm
|
||||
|
||||
# CRUD!
|
||||
from selector import (
|
||||
from distribusiselector import (
|
||||
SelectNewDistribusi,
|
||||
SelectUpdateDistribusi,
|
||||
DeleteDistribusi,
|
||||
SelectorVisible,
|
||||
)
|
||||
from editor import Editor
|
||||
|
||||
# Upload
|
||||
# Interface!
|
||||
from editor import Editor
|
||||
from themeselector import ThemeSelector
|
||||
from upload import UploadNewDistribusi, UploadUpdatedFiles
|
||||
|
||||
# UserPengguna
|
||||
@ -52,6 +52,7 @@ from statuspengguna.helper import (
|
||||
)
|
||||
from statuspengguna.loginuser import LoginUser
|
||||
from statuspengguna.registeruser import RegisterUser
|
||||
from distribusisinfo import DistribusisInfo
|
||||
|
||||
# Tada!
|
||||
from distribusi.cli import build_argparser
|
||||
@ -104,8 +105,9 @@ def distribusi():
|
||||
uploadform = UploadForm()
|
||||
distribusiform = DistribusiForm()
|
||||
themeform = ThemeForm()
|
||||
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
|
||||
|
||||
selectorvisible = SelectorVisible()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
@ -164,8 +166,9 @@ def distribusi():
|
||||
def upload():
|
||||
distribusiform = DistribusiForm()
|
||||
themeform = ThemeForm()
|
||||
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
|
||||
selectorvisible = SelectorVisible()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
if current_distribusi == "new" or HasDistribusi() is False:
|
||||
@ -192,42 +195,7 @@ def upload():
|
||||
@APP.route("/theme", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def theme():
|
||||
uploadform = UploadForm()
|
||||
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
|
||||
return ThemeSelector()
|
||||
|
||||
|
||||
@APP.route("/editor", methods=["GET", "POST"])
|
||||
@ -243,15 +211,18 @@ def selector():
|
||||
uploadform = UploadForm()
|
||||
distribusiform = DistribusiForm()
|
||||
themeform = ThemeForm()
|
||||
themeform.publicthemes.choices = DistribusisInfo.publicthemes()
|
||||
selectorform = SelectorForm()
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
selectorform.distribusis.choices = DistribusisInfo.userdistribusis()
|
||||
current_distribusi = CurrentDistribusi()
|
||||
if selectorform.validate_on_submit():
|
||||
if selectorform.new.data:
|
||||
selectorform = SelectNewDistribusi()
|
||||
if selectorform.delete.data:
|
||||
selectorform = DeleteDistribusi(selectorform.distribusis.data)
|
||||
selectorform.distribusis.choices = distribusisfields()
|
||||
selectorform.distribusis.choices = (
|
||||
DistribusisInfo.userdistribusis()
|
||||
)
|
||||
if selectorform.update.data:
|
||||
selectorform = SelectUpdateDistribusi(
|
||||
selectorform.distribusis.data
|
||||
@ -328,14 +299,6 @@ def load_user(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__":
|
||||
APP.debug = True
|
||||
APP.run(port=5000)
|
||||
|
@ -63,13 +63,6 @@ def 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():
|
||||
"""reset user state upon visiting index, so that distribusi workflow can
|
||||
be done correctly"""
|
||||
@ -84,16 +77,7 @@ def ResetUserState():
|
||||
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
|
||||
|
@ -8,7 +8,11 @@
|
||||
{{ themeform.theme.label }}
|
||||
{{ themeform.theme }}
|
||||
</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">
|
||||
{{ themeform.save }}
|
||||
</fieldset>
|
||||
|
66
verse/themeselector.py
Normal file
66
verse/themeselector.py
Normal 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
|
@ -15,7 +15,7 @@ from usermodel import User
|
||||
from distribusimodel import Distribusis
|
||||
|
||||
from statuspengguna.helper import CurrentDistribusi
|
||||
from statuspengguna.helper import SelectCurrentDistribusi
|
||||
from distribusiselector import SelectCurrentDistribusi
|
||||
from forms.uploadform import UploadForm
|
||||
|
||||
|
||||
@ -77,7 +77,6 @@ def UploadUpdatedFiles(uploadfolder):
|
||||
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
||||
db.session.rollback()
|
||||
uploadform.sitename.errors.append("Something went wrong!")
|
||||
flash("Something went wrong!", "danger")
|
||||
|
||||
zipfilename = "{}.zip".format(distribusi.distribusiname)
|
||||
zipfile = uploadform.zipfile.data
|
||||
|
Loading…
Reference in New Issue
Block a user