update theme after initial upload

This commit is contained in:
crunk 2022-03-18 17:32:49 +01:00
parent 6507b05c64
commit 8214b714ba
9 changed files with 76 additions and 39 deletions

View File

@ -6,39 +6,25 @@ from flask_login import current_user
from usermodel import User
from distribusimodel import Distribusis
from forms.selectorform import SelectorForm
from forms.selectorform import SelectorForm
from statuspengguna.helper import HasDistribusi
from statuspengguna.helper import DistribusiSelected
from statuspengguna.helper import SelectCurrentDistribusi
from app import db
def SelectNewDistribusi():
print("make a new distribusi")
selectorform = SelectorForm()
user = User.query.filter_by(email=current_user.email).first()
try:
user.currentdistribusi = "new"
db.session.commit()
except:
db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
SelectCurrentDistribusi("new")
return selectorform
def SelectUpdateDistribusi(distribusiname):
print(f"Update this distribusi {distribusiname}")
selectorform = SelectorForm()
user = User.query.filter_by(email=current_user.email).first()
try:
user.currentdistribusi = distribusiname
db.session.commit()
except:
db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
SelectCurrentDistribusi(distribusiname)
return selectorform
@ -55,6 +41,11 @@ def DeleteDistribusi(distribusiname):
db.session.commit()
userfolder = os.path.join("stash", distribusi.distribusiname)
shutil.rmtree(userfolder)
cssfolder = os.path.join(
"themes/userstyles",
distribusi.distribusiname
)
shutil.rmtree(cssfolder)
except:
db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!")

View File

@ -47,6 +47,7 @@ from statuspengguna.helper import (
IsCssSelected,
CurrentDistribusi,
HasDistribusi,
IsDistribusiLive,
ResetUserState,
)
from statuspengguna.loginuser import LoginUser
@ -70,7 +71,7 @@ def index():
ResetUserState()
uploadform = UploadForm()
distribusis = Distribusis.query.filter(
Distribusis.distribusiname.isnot(None)
Distribusis.visible.isnot(False)
).all()
distribusies = {}
for distribusi in distribusis:
@ -109,6 +110,7 @@ def distribusi():
selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi()
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
user = User.query.filter_by(email=current_user.email).first()
distribusi = Distribusis.query.filter_by(
@ -128,15 +130,17 @@ def distribusi():
os.remove(os.path.join(userfolder, zipfilename))
# To Do: Make sure nothing can be executed from the upload folder
cssfile = ""
for filename in os.listdir(userfolder):
cssfolder = os.path.join("themes/userstyles", distribusi.distribusiname)
for filename in os.listdir(cssfolder):
if filename.endswith(".css"):
cssfile = os.path.join(userfolder, filename)
cssfile = os.path.join(cssfolder, filename)
parser = build_argparser()
args = parser.parse_args(["-s", cssfile])
distribusify(args, userfolder)
distribusi.visible = True
user.currentdistribusi = None
os.remove(cssfile)
db.session.commit()
return redirect(url_for("index"))
template = render_template(
@ -146,6 +150,7 @@ def distribusi():
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)
@ -168,6 +173,7 @@ def upload():
else:
uploadform = UploadUpdatedFiles(APP.config["UPLOAD_FOLDER"])
files_uploaded = IsZipUploaded(uploadform.sitename.data)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
template = render_template(
"distribusi.html",
@ -176,6 +182,7 @@ def upload():
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)
@ -193,15 +200,20 @@ def theme():
selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi()
print(current_distribusi)
files_uploaded = IsZipUploaded(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
if themeform.validate_on_submit():
newuserfolder = os.path.join("stash", current_distribusi)
newcssfolder = os.path.join("themes/userstyles", current_distribusi)
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
copycssfile = os.path.join(
"themes",
"{}.css".format(themeform.theme.data),
)
shutil.copy(copycssfile, newuserfolder)
shutil.copy(copycssfile, newcssfolder)
css_selected = IsCssSelected(current_distribusi)
template = render_template(
@ -211,6 +223,7 @@ def theme():
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)
@ -223,6 +236,7 @@ def editor():
editorform = EditorForm()
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()
@ -233,7 +247,8 @@ def editor():
htmlplaceholder = f.read()
print(htmlplaceholder)
if editorform.validate_on_submit():
userfolder = os.path.join("stash", current_distribusi)
userfolder = os.path.join("themes/userstyles", current_distribusi)
os.mkdir(newuserfolder)
cssfilename = "{}.css".format(editorform.cssname.data)
with open(os.path.join(userfolder, cssfilename), "w") as cssfile:
cssfile.write(editorform.css.data)
@ -242,6 +257,7 @@ def editor():
template = render_template(
"editor.html",
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
editorform=editorform,
htmlplaceholder=htmlplaceholder,
)
@ -280,7 +296,10 @@ def selector():
uploadform.tags.data = distribusi.tags
files_uploaded = IsZipUploaded(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
distribusi_live = IsDistribusiLive(current_distribusi)
# because the user has chosen to update his distribusi, we assume
# no selected css.
css_selected = False
selectorvisible = SelectorVisible()
template = render_template(
@ -290,6 +309,7 @@ def selector():
themeform=themeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
)

View File

@ -13,12 +13,25 @@ def IsZipUploaded(distribusiname):
if os.path.exists(os.path.join(userfolder, zipfilename)):
print("folder with zipfile found, file uploaded")
return True
print("distribusi folder has no zipfile")
return False
def IsDistribusiLive(distribusiname):
userfolder = os.path.join("stash", distribusiname)
if os.path.exists(userfolder):
zipfilename = "{}.zip".format(distribusiname)
if os.path.exists(os.path.join(userfolder, zipfilename)):
print("folder with zipfile found, file uploaded")
return False
if len(os.listdir(userfolder)) > 0:
return True
print("distribusi folder is empty")
return False
def IsCssSelected(distribusiname):
userfolder = os.path.join("stash", distribusiname)
userfolder = os.path.join("themes/userstyles", distribusiname)
if os.path.exists(userfolder):
for file in os.listdir(userfolder):
if file.endswith(".css"):
@ -33,9 +46,6 @@ def HasDistribusi():
if distribusi is None:
print("no distribusi found")
return False
if distribusi.visible is False:
print("distribusi not visible")
return False
return True
@ -43,8 +53,6 @@ def CurrentDistribusi():
user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None:
return "None"
if user.currentdistribusi == "new":
return "new"
return user.currentdistribusi
@ -67,6 +75,17 @@ def ResetUserState():
flash("An error occured !", "danger")
def SelectCurrentDistribusi(distribusiname):
user = User.query.filter_by(email=current_user.email).first()
try:
user.currentdistribusi = distribusiname
db.session.commit()
except:
db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger")
# def IsThemeSelected
# def IsCustomThemePresent
# def IsDistribusiLive

View File

@ -1,6 +1,6 @@
<div id="edit" class="workflow">
<h3>(Optional) Step 3: Edit</h3>
{% if files_uploaded %}
{% if files_uploaded or distribusi_live %}
<a href="/editor"><p>Go to CSS editor</p></a>
{% else %}
<p>

View File

@ -1,10 +1,10 @@
<div id="launch" class="workflow">
<h3>Step 4: Distribusi</h3>
<p>Run distribusi on your files, this will generate your website and make
<p>Run distribusi on your files. This will generate your website and make
your content public.</p>
<form method="POST" enctype="multipart/form-data" action="{{ url_for('distribusi') }}">
{{ distribusiform.csrf_token }}
{% if files_uploaded %}
{% if files_uploaded or distribusi_live %}
<fieldset class="button required">
{{ distribusiform.submit }}
</fieldset>

View File

@ -8,7 +8,7 @@
{{ themeform.theme.label }}
{{ themeform.theme }}
</fieldset>
{% if files_uploaded %}
{% if files_uploaded or distribusi_live %}
<fieldset class="button required">
{{ themeform.save }}
</fieldset>

View File

@ -19,7 +19,7 @@
</fieldset>
</div>
<div class="editarea">
{% if files_uploaded %}
{% if files_uploaded or distribusi_live %}
<fieldset class="required">
{{ editorform.cssname.label }}
{{ editorform.cssname }}

3
verse/themes/userstyles/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*
*/
!.gitignore

View File

@ -1,16 +1,20 @@
import os
import shutil
from flask import flash
from flask_login import current_user
from sqlalchemy.exc import (
IntegrityError,
InvalidRequestError,
)
from app import db
from usermodel import User
from distribusimodel import Distribusis
from flask_login import current_user
from statuspengguna.helper import CurrentDistribusi
from statuspengguna.helper import SelectCurrentDistribusi
from forms.uploadform import UploadForm
from app import db
def UploadNewDistribusi(uploadfolder):
@ -39,7 +43,7 @@ def UploadNewDistribusi(uploadfolder):
"distribusi name already exists!"
)
flash("distribusi name already exists!", "warning")
SelectCurrentDistribusi(newdistribusi.distribusiname)
zipfilename = "{}.zip".format(newdistribusi.distribusiname)
zipfile = uploadform.zipfile.data
zipfile.save(os.path.join(uploadfolder, zipfilename))