You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.7 KiB
200 lines
6.7 KiB
3 years ago
|
import os
|
||
7 months ago
|
import shutil
|
||
|
|
||
3 years ago
|
import bleach
|
||
7 months ago
|
from bleach_allowlist import all_styles
|
||
|
from flask import render_template
|
||
7 months ago
|
from sqlalchemy.exc import (
|
||
6 months ago
|
DatabaseError,
|
||
|
DataError,
|
||
|
InterfaceError,
|
||
|
InvalidRequestError,
|
||
7 months ago
|
)
|
||
7 months ago
|
from werkzeug.utils import secure_filename
|
||
|
|
||
|
from app import db
|
||
5 months ago
|
from distribusikan.distribusis_info import DistribusisInfo
|
||
5 months ago
|
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
|
||
7 months ago
|
from models.distribusi_model import Distribusis
|
||
7 months ago
|
from statuspengguna.helper import UserHelper
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def css_editor_page():
|
||
6 months ago
|
editorform = EditorForm()
|
||
|
current_distribusi = UserHelper.current_distribusi()
|
||
|
if editorform.validate_on_submit():
|
||
5 months ago
|
validate_edit_css_form(editorform, current_distribusi)
|
||
|
return render_distribusi_template(current_distribusi)
|
||
3 years ago
|
|
||
5 months ago
|
return render_editor_template(editorform, current_distribusi)
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def validate_edit_css_form(editorform, current_distribusi):
|
||
6 months ago
|
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:
|
||
5 months ago
|
make_public_theme(editorform, current_distribusi)
|
||
6 months ago
|
if editorform.cssfile.data:
|
||
5 months ago
|
save_upload_css_file(editorform, publicfolder)
|
||
|
copy_public_to_user_folder(editorform, publicfolder, newcssfolder)
|
||
6 months ago
|
return
|
||
|
else:
|
||
5 months ago
|
write_css_to_file(editorform, publicfolder)
|
||
6 months ago
|
|
||
|
if editorform.cssfile.data:
|
||
5 months ago
|
save_upload_css_file(editorform, newcssfolder)
|
||
6 months ago
|
return
|
||
|
if editorform.cssname.data:
|
||
5 months ago
|
write_css_to_file(editorform, newcssfolder)
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def save_upload_css_file(editorform, newcssfolder):
|
||
6 months ago
|
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()
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def write_css_to_file(editorform, newcssfolder):
|
||
6 months ago
|
if not os.path.exists(newcssfolder):
|
||
|
os.mkdir(newcssfolder)
|
||
3 years ago
|
|
||
6 months ago
|
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
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def copy_public_to_user_folder(editorform, publicfolder, newcssfolder):
|
||
6 months ago
|
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)
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def make_public_theme(editorform, current_distribusi):
|
||
6 months ago
|
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")
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def render_distribusi_template(current_distribusi):
|
||
6 months ago
|
uploadform = UploadForm()
|
||
|
distribusiform = DistribusiForm()
|
||
|
themeform = ThemeForm()
|
||
|
publicthemeform = PublicThemeForm()
|
||
5 months ago
|
publicthemeform.publicthemes.choices = DistribusisInfo.public_themes()
|
||
6 months ago
|
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
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def render_editor_template(editorform, current_distribusi):
|
||
|
html_placeholder = get_html_placeholder()
|
||
3 years ago
|
|
||
5 months ago
|
css_placeholder = get_css_placeholder(current_distribusi)
|
||
|
editorform.css.data = css_placeholder
|
||
3 years ago
|
|
||
6 months ago
|
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,
|
||
5 months ago
|
html_placeholder=html_placeholder,
|
||
6 months ago
|
)
|
||
|
return template
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def get_css_placeholder(current_distribusi):
|
||
|
css_placeholder = "Try out your CSS here"
|
||
6 months ago
|
distribusi = Distribusis.query.filter_by(
|
||
|
distribusiname=current_distribusi
|
||
|
).first()
|
||
|
if distribusi is not None and distribusi.publictheme is not None:
|
||
5 months ago
|
css_placeholder = get_public_css_file(distribusi)
|
||
6 months ago
|
else:
|
||
|
with open("themes/editor/placeholder.css") as f:
|
||
5 months ago
|
css_placeholder = f.read()
|
||
|
return css_placeholder
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def get_html_placeholder():
|
||
|
html_placeholder = "Write some test HTML here"
|
||
6 months ago
|
with open("themes/editor/placeholder.html") as f:
|
||
5 months ago
|
html_placeholder = f.read()
|
||
|
return html_placeholder
|
||
3 years ago
|
|
||
|
|
||
5 months ago
|
def get_public_css_file(distribusi):
|
||
|
css_placeholder = ""
|
||
6 months ago
|
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:
|
||
5 months ago
|
css_placeholder = f.read()
|
||
|
return css_placeholder
|