Browse Source

update theme after initial upload

current_wdka_release
crunk 3 years ago
parent
commit
8214b714ba
  1. 27
      verse/selector.py
  2. 34
      verse/start.py
  3. 31
      verse/statuspengguna/helper.py
  4. 2
      verse/templates/distribusiworkflow/editcss.html
  5. 4
      verse/templates/distribusiworkflow/launch.html
  6. 2
      verse/templates/distribusiworkflow/theme.html
  7. 2
      verse/templates/editor.html
  8. 3
      verse/themes/userstyles/.gitignore
  9. 10
      verse/upload.py

27
verse/selector.py

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

34
verse/start.py

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

31
verse/statuspengguna/helper.py

@ -13,12 +13,25 @@ def IsZipUploaded(distribusiname):
if os.path.exists(os.path.join(userfolder, zipfilename)): if os.path.exists(os.path.join(userfolder, zipfilename)):
print("folder with zipfile found, file uploaded") print("folder with zipfile found, file uploaded")
return True 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") print("distribusi folder is empty")
return False return False
def IsCssSelected(distribusiname): def IsCssSelected(distribusiname):
userfolder = os.path.join("stash", distribusiname) userfolder = os.path.join("themes/userstyles", distribusiname)
if os.path.exists(userfolder): if os.path.exists(userfolder):
for file in os.listdir(userfolder): for file in os.listdir(userfolder):
if file.endswith(".css"): if file.endswith(".css"):
@ -33,9 +46,6 @@ def HasDistribusi():
if distribusi is None: if distribusi is None:
print("no distribusi found") print("no distribusi found")
return False return False
if distribusi.visible is False:
print("distribusi not visible")
return False
return True return True
@ -43,8 +53,6 @@ def CurrentDistribusi():
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
if user.currentdistribusi is None: if user.currentdistribusi is None:
return "None" return "None"
if user.currentdistribusi == "new":
return "new"
return user.currentdistribusi return user.currentdistribusi
@ -67,6 +75,17 @@ def ResetUserState():
flash("An error occured !", "danger") 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 IsThemeSelected
# def IsCustomThemePresent # def IsCustomThemePresent
# def IsDistribusiLive # def IsDistribusiLive

2
verse/templates/distribusiworkflow/editcss.html

@ -1,6 +1,6 @@
<div id="edit" class="workflow"> <div id="edit" class="workflow">
<h3>(Optional) Step 3: Edit</h3> <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> <a href="/editor"><p>Go to CSS editor</p></a>
{% else %} {% else %}
<p> <p>

4
verse/templates/distribusiworkflow/launch.html

@ -1,10 +1,10 @@
<div id="launch" class="workflow"> <div id="launch" class="workflow">
<h3>Step 4: Distribusi</h3> <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> your content public.</p>
<form method="POST" enctype="multipart/form-data" action="{{ url_for('distribusi') }}"> <form method="POST" enctype="multipart/form-data" action="{{ url_for('distribusi') }}">
{{ distribusiform.csrf_token }} {{ distribusiform.csrf_token }}
{% if files_uploaded %} {% if files_uploaded or distribusi_live %}
<fieldset class="button required"> <fieldset class="button required">
{{ distribusiform.submit }} {{ distribusiform.submit }}
</fieldset> </fieldset>

2
verse/templates/distribusiworkflow/theme.html

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

2
verse/templates/editor.html

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

3
verse/themes/userstyles/.gitignore

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

10
verse/upload.py

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

Loading…
Cancel
Save