refactored css editor to own file

This commit is contained in:
crunk 2022-03-18 21:43:19 +01:00
parent f44778dde1
commit bb3397bb53
2 changed files with 93 additions and 30 deletions

91
verse/editor.py Normal file
View File

@ -0,0 +1,91 @@
import os
from flask import render_template
from statuspengguna.helper import (
IsZipUploaded,
CurrentDistribusi,
IsDistribusiLive,
)
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
from forms.editorform import EditorForm
from forms.selectorform import SelectorForm
def EditCss():
editorform = EditorForm()
current_distribusi = CurrentDistribusi()
if editorform.validate_on_submit():
ValidateEditCssForm(editorform, current_distribusi)
return RenderDistribusiTemplate(current_distribusi)
return RenderEditorTemplate(editorform, current_distribusi)
def ValidateEditCssForm(editorform, current_distribusi):
newcssfolder = os.path.join("themes/userstyles", current_distribusi)
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
cssfilename = "{}.css".format(editorform.cssname.data)
with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile:
cssfile.write(editorform.css.data)
cssfile.close
def RenderDistribusiTemplate(current_distribusi):
uploadform = UploadForm()
distribusiform = DistribusiForm()
themeform = ThemeForm()
selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = True
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
def RenderEditorTemplate(editorform, current_distribusi):
htmlplaceholder = HtmlPlaceholder()
cssplaceholder = CssPlaceholder()
editorform.css.data = cssplaceholder
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
template = render_template(
"editor.html",
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
editorform=editorform,
htmlplaceholder=htmlplaceholder,
)
return template
def CssPlaceholder():
cssplaceholder = "Try out your CSS here"
with open("themes/editor/placeholder.css") as f:
cssplaceholder = f.read()
return cssplaceholder
def HtmlPlaceholder():
htmlplaceholder = "Write some test HTML here"
with open("themes/editor/placeholder.html") as f:
htmlplaceholder = f.read()
return htmlplaceholder

View File

@ -27,7 +27,6 @@ from distribusimodel import Distribusis
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
from forms.editorform import EditorForm
from forms.selectorform import SelectorForm from forms.selectorform import SelectorForm
# CRUD! # CRUD!
@ -37,6 +36,7 @@ from selector import (
DeleteDistribusi, DeleteDistribusi,
SelectorVisible, SelectorVisible,
) )
from editor import EditCss
# Upload # Upload
from upload import UploadNewDistribusi, UploadUpdatedFiles from upload import UploadNewDistribusi, UploadUpdatedFiles
@ -233,35 +233,7 @@ def theme():
@APP.route("/editor", methods=["GET", "POST"]) @APP.route("/editor", methods=["GET", "POST"])
@login_required @login_required
def editor(): def editor():
editorform = EditorForm() return EditCss()
current_distribusi = CurrentDistribusi()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
cssplaceholder = "Try out your CSS here"
with open("themes/editor/placeholder.css") as f:
cssplaceholder = f.read()
editorform.css.data = cssplaceholder
htmlplaceholder = "Write some test HTML here"
with open("themes/editor/placeholder.html") as f:
htmlplaceholder = f.read()
print(htmlplaceholder)
if editorform.validate_on_submit():
cssfolder = os.path.join("themes/userstyles", current_distribusi)
os.mkdir(cssfolder)
cssfilename = "{}.css".format(editorform.cssname.data)
with open(os.path.join(cssfolder, cssfilename), "w") as cssfile:
cssfile.write(editorform.css.data)
cssfile.close
template = render_template(
"editor.html",
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
editorform=editorform,
htmlplaceholder=htmlplaceholder,
)
return template
@APP.route("/selector", methods=["GET", "POST"]) @APP.route("/selector", methods=["GET", "POST"])