distribusi-verse/verse/editor.py

166 lines
5.2 KiB
Python
Raw Normal View History

2022-03-18 21:43:19 +01:00
import os
2022-03-19 19:48:56 +01:00
import bleach
2022-03-19 20:16:00 +01:00
import shutil
from flask import render_template
2022-03-19 19:48:56 +01:00
from werkzeug.utils import secure_filename
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
2022-03-19 14:35:08 +01:00
from app import db
2022-03-18 21:43:19 +01:00
from statuspengguna.helper import (
IsZipUploaded,
CurrentDistribusi,
IsDistribusiLive,
DistribusiLimitReached,
2022-03-18 21:43:19 +01:00
)
2022-03-19 14:35:08 +01:00
from distribusimodel import Distribusis
2022-03-19 16:19:51 +01:00
from distribusisinfo import DistribusisInfo
2022-03-18 21:43:19 +01:00
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
2022-03-19 17:08:04 +01:00
from forms.publicthemeform import PublicThemeForm
2022-03-18 21:43:19 +01:00
from forms.editorform import EditorForm
from forms.selectorform import SelectorForm
def Editor():
2022-03-18 21:43:19 +01:00
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/userthemes", current_distribusi)
2022-03-19 20:16:00 +01:00
shutil.rmtree(newcssfolder)
publicfolder = os.path.join("themes/publicthemes", current_distribusi)
shutil.rmtree(publicfolder)
if editorform.public.data:
MakePublicTheme(editorform, current_distribusi)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, publicfolder)
else:
WriteCssToFile(editorform, publicfolder)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, newcssfolder)
return
if editorform.cssname.data:
WriteCssToFile(editorform, newcssfolder)
def SaveUploadCssFile(editorform, newcssfolder):
2022-03-18 21:43:19 +01:00
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
cssfile = editorform.cssfile.data
2022-03-19 19:48:56 +01:00
cssfilename = f"{secure_filename(editorform.cssname.data)}.css"
cssfile.save(os.path.join(newcssfolder, cssfilename))
2022-03-21 20:08:56 +01:00
openfile = open(os.path.join(newcssfolder, cssfilename), "r")
2022-03-19 19:48:56 +01:00
cleancss = bleach.clean(openfile.read())
2022-03-19 20:01:31 +01:00
openfile.close()
2022-03-21 20:08:56 +01:00
cleanfile = open(os.path.join(newcssfolder, cssfilename), "w")
2022-03-19 20:01:31 +01:00
cleanfile.write(cleancss)
cleanfile.close()
def WriteCssToFile(editorform, newcssfolder):
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
2022-03-19 19:48:56 +01:00
cssfilename = f"{secure_filename(editorform.cssname.data)}.css"
cleancss = bleach.clean(editorform.css.data)
2022-03-18 21:43:19 +01:00
with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile:
2022-03-19 19:48:56 +01:00
cssfile.write(cleancss)
2022-03-18 21:43:19 +01:00
cssfile.close
def MakePublicTheme(editorform, current_distribusi):
try:
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
2022-03-19 19:48:56 +01:00
distribusi.publictheme = secure_filename(editorform.cssname.data)
db.session.commit()
except InvalidRequestError:
db.session.rollback()
editorform.public.errors.append("Something went wrong!")
except DataError:
db.session.rollback()
editorform.public.errors.append("Invalid Entry")
except InterfaceError:
db.session.rollback()
2022-03-19 16:19:51 +01:00
editorform.public.errors.append("Error connecting to the database")
except DatabaseError:
db.session.rollback()
2022-03-19 16:19:51 +01:00
editorform.public.errors.append("Error connecting to the database")
2022-03-18 21:43:19 +01:00
def RenderDistribusiTemplate(current_distribusi):
uploadform = UploadForm()
distribusiform = DistribusiForm()
themeform = ThemeForm()
2022-03-19 17:08:04 +01:00
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
2022-03-18 21:43:19 +01:00
selectorform = SelectorForm()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = True
selectorvisible = False
limit_reached = DistribusiLimitReached()
2022-03-18 21:43:19 +01:00
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
2022-03-19 17:08:04 +01:00
publicthemeform=publicthemeform,
2022-03-18 21:43:19 +01:00
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
limit_reached=limit_reached,
2022-03-18 21:43:19 +01:00
)
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