distribusi-verse/verse/distribusikan/editor.py

200 lines
6.5 KiB
Python
Raw Normal View History

2022-03-18 21:43:19 +01:00
import os
2024-04-21 20:44:08 +02:00
import shutil
2022-03-19 19:48:56 +01:00
import bleach
2024-04-24 23:05:36 +02:00
from bleach_allowlist import all_styles
from flask import render_template
from sqlalchemy.exc import (
2024-05-28 23:02:17 +02:00
DatabaseError,
DataError,
InterfaceError,
InvalidRequestError,
)
2024-04-28 13:04:07 +02:00
from werkzeug.utils import secure_filename
from app import db
from distribusikan.distribusisinfo import DistribusisInfo
2022-03-18 21:43:19 +01:00
from forms.distribusiform import DistribusiForm
from forms.editorform import EditorForm
2024-04-21 20:44:08 +02:00
from forms.publicthemeform import PublicThemeForm
2022-03-18 21:43:19 +01:00
from forms.selectorform import SelectorForm
2024-04-21 20:44:08 +02:00
from forms.themeform import ThemeForm
from forms.uploadform import UploadForm
2024-04-28 15:34:29 +02:00
from models.distribusi_model import Distribusis
2024-04-21 20:44:08 +02:00
from statuspengguna.helper import UserHelper
2022-03-18 21:43:19 +01:00
def Editor():
2024-05-28 23:02:17 +02:00
editorform = EditorForm()
current_distribusi = UserHelper.current_distribusi()
if editorform.validate_on_submit():
ValidateEditCssForm(editorform, current_distribusi)
return RenderDistribusiTemplate(current_distribusi)
2022-03-18 21:43:19 +01:00
2024-05-28 23:02:17 +02:00
return RenderEditorTemplate(editorform, current_distribusi)
2022-03-18 21:43:19 +01:00
def ValidateEditCssForm(editorform, current_distribusi):
2024-05-28 23:02:17 +02:00
newcssfolder = os.path.join("themes/userthemes", current_distribusi)
if os.path.exists(newcssfolder):
shutil.rmtree(newcssfolder)
publicfolder = os.path.join("themes/publicthemes", current_distribusi)
if os.path.exists(publicfolder):
shutil.rmtree(publicfolder)
if editorform.public.data:
MakePublicTheme(editorform, current_distribusi)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, publicfolder)
CopyPublicToUserFolder(editorform, publicfolder, newcssfolder)
return
else:
WriteCssToFile(editorform, publicfolder)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, newcssfolder)
return
if editorform.cssname.data:
WriteCssToFile(editorform, newcssfolder)
def SaveUploadCssFile(editorform, newcssfolder):
2024-05-28 23:02:17 +02:00
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
cssfile = editorform.cssfile.data
cssfilename = f"{secure_filename(editorform.cssname.data)}.css"
cssfile.save(os.path.join(newcssfolder, cssfilename))
openfile = open(os.path.join(newcssfolder, cssfilename), "r")
cleancss = bleach.clean(openfile.read(), all_styles)
cleancss = cleancss.replace(">", ">")
openfile.close()
cleanfile = open(os.path.join(newcssfolder, cssfilename), "w")
cleanfile.write(cleancss)
cleanfile.close()
def WriteCssToFile(editorform, newcssfolder):
2024-05-28 23:02:17 +02:00
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
2024-05-28 23:02:17 +02:00
cssfilename = f"{secure_filename(editorform.cssname.data)}.css"
cleancss = bleach.clean(editorform.css.data, all_styles)
cleancss = cleancss.replace(">", ">")
with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile:
cssfile.write(cleancss)
cssfile.close
2022-03-18 21:43:19 +01:00
2022-03-27 18:02:51 +02:00
def CopyPublicToUserFolder(editorform, publicfolder, newcssfolder):
2024-05-28 23:02:17 +02:00
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
copycssfile = os.path.join(
publicfolder, f"{secure_filename(editorform.cssname.data)}.css"
)
print(f"copying file: {copycssfile}")
print(f"to folder: {newcssfolder}")
shutil.copy(copycssfile, newcssfolder)
2022-03-27 18:02:51 +02:00
def MakePublicTheme(editorform, current_distribusi):
2024-05-28 23:02:17 +02:00
try:
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
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()
editorform.public.errors.append("Error connecting to the database")
except DatabaseError:
db.session.rollback()
editorform.public.errors.append("Error connecting to the database")
2022-03-18 21:43:19 +01:00
def RenderDistribusiTemplate(current_distribusi):
2024-05-28 23:02:17 +02:00
uploadform = UploadForm()
distribusiform = DistribusiForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm()
files_uploaded = UserHelper.is_zip_uploaded(current_distribusi)
distribusi_live = UserHelper.is_distribusi_live(current_distribusi)
css_selected = True
selectorvisible = False
limit_reached = UserHelper.distribusi_limit_reached()
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
publicthemeform=publicthemeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
limit_reached=limit_reached,
)
return template
2022-03-18 21:43:19 +01:00
def RenderEditorTemplate(editorform, current_distribusi):
2024-05-28 23:02:17 +02:00
htmlplaceholder = HtmlPlaceholder()
2022-03-18 21:43:19 +01:00
2024-05-28 23:02:17 +02:00
cssplaceholder = CssPlaceholder(current_distribusi)
editorform.css.data = cssplaceholder
2022-03-18 21:43:19 +01:00
2024-05-28 23:02:17 +02:00
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,
distribusi_live=distribusi_live,
editorform=editorform,
htmlplaceholder=htmlplaceholder,
)
return template
2022-03-18 21:43:19 +01:00
def CssPlaceholder(current_distribusi):
2024-05-28 23:02:17 +02:00
cssplaceholder = "Try out your CSS here"
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
if distribusi is not None and distribusi.publictheme is not None:
cssplaceholder = GetPublicCssFile(distribusi)
else:
with open("themes/editor/placeholder.css") as f:
cssplaceholder = f.read()
return cssplaceholder
2022-03-18 21:43:19 +01:00
def HtmlPlaceholder():
2024-05-28 23:02:17 +02:00
htmlplaceholder = "Write some test HTML here"
with open("themes/editor/placeholder.html") as f:
htmlplaceholder = f.read()
return htmlplaceholder
def GetPublicCssFile(distribusi):
2024-05-28 23:02:17 +02:00
cssplaceholder = ""
publicthemefolder = os.path.join(
"themes/publicthemes", distribusi.distribusiname
)
for filename in os.listdir(publicthemefolder):
if filename.endswith(".css"):
cssfile = os.path.join(publicthemefolder, filename)
with open(cssfile) as f:
cssplaceholder = f.read()
return cssplaceholder