distribusi-verse/verse/distribusikan/css_editor_page.py

199 lines
6.7 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
2024-12-21 13:18:00 +01:00
from app import db, APP
from distribusikan.distribusis_info import DistribusisInfo
from distribusikan.forms.distribusiform import DistribusiForm
from distribusikan.forms.editorform import EditorForm
from distribusikan.forms.publicthemeform import PublicThemeForm
from distribusikan.forms.selectorform import SelectorForm
from distribusikan.forms.themeform import ThemeForm
from distribusikan.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 css_editor_page():
2024-05-28 23:02:17 +02:00
editorform = EditorForm()
current_distribusi = UserHelper.current_distribusi()
if editorform.validate_on_submit():
validate_edit_css_form(editorform, current_distribusi)
return render_distribusi_template(current_distribusi)
2022-03-18 21:43:19 +01:00
return render_editor_template(editorform, current_distribusi)
2022-03-18 21:43:19 +01:00
def validate_edit_css_form(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:
make_public_theme(editorform, current_distribusi)
2024-05-28 23:02:17 +02:00
if editorform.cssfile.data:
save_upload_css_file(editorform, publicfolder)
copy_public_to_user_folder(editorform, publicfolder, newcssfolder)
2024-05-28 23:02:17 +02:00
return
else:
write_css_to_file(editorform, publicfolder)
2024-05-28 23:02:17 +02:00
if editorform.cssfile.data:
save_upload_css_file(editorform, newcssfolder)
2024-05-28 23:02:17 +02:00
return
if editorform.cssname.data:
write_css_to_file(editorform, newcssfolder)
def save_upload_css_file(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 write_css_to_file(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
def copy_public_to_user_folder(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"
)
2024-12-21 13:18:00 +01:00
APP.logger.info(f"copying file:{copycssfile} to folder:{newcssfolder}")
2024-05-28 23:02:17 +02:00
shutil.copy(copycssfile, newcssfolder)
2022-03-27 18:02:51 +02:00
def make_public_theme(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")
def render_distribusi_template(current_distribusi):
2024-05-28 23:02:17 +02:00
uploadform = UploadForm()
distribusiform = DistribusiForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.public_themes()
2024-05-28 23:02:17 +02:00
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 render_editor_template(editorform, current_distribusi):
html_placeholder = get_html_placeholder()
2022-03-18 21:43:19 +01:00
css_placeholder = get_css_placeholder(current_distribusi)
editorform.css.data = css_placeholder
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,
html_placeholder=html_placeholder,
2024-05-28 23:02:17 +02:00
)
return template
2022-03-18 21:43:19 +01:00
def get_css_placeholder(current_distribusi):
css_placeholder = "Try out your CSS here"
2024-05-28 23:02:17 +02:00
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
if distribusi is not None and distribusi.publictheme is not None:
css_placeholder = get_public_css_file(distribusi)
2024-05-28 23:02:17 +02:00
else:
with open("themes/editor/placeholder.css") as f:
css_placeholder = f.read()
return css_placeholder
2022-03-18 21:43:19 +01:00
def get_html_placeholder():
html_placeholder = "Write some test HTML here"
2024-05-28 23:02:17 +02:00
with open("themes/editor/placeholder.html") as f:
html_placeholder = f.read()
return html_placeholder
def get_public_css_file(distribusi):
css_placeholder = ""
2024-05-28 23:02:17 +02:00
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:
css_placeholder = f.read()
return css_placeholder