diff --git a/verse/selector.py b/verse/selector.py new file mode 100644 index 0000000..b51c3f8 --- /dev/null +++ b/verse/selector.py @@ -0,0 +1,61 @@ +from flask import ( + render_template, + redirect, + request, + flash, + url_for, + abort, +) + +from usermodel import User +from flask_login import current_user + +from forms.loginform import LoginForm +from forms.uploadform import UploadForm +from forms.distribusiform import DistribusiForm +from forms.themeform import ThemeForm +from forms.editorform import EditorForm +from forms.selectorform import SelectorForm + +from statuspengguna.helper import HasDistribusi +from statuspengguna.helper import DistribusiSelected + +from app import db + + +def SelectNewDistribusi(): + print("make a new distribusi") + user = User.query.filter_by(email=current_user.email).first() + try: + user.currentdistribusi = "new" + db.session.commit() + except: + db.session.rollback() + selectorform.new.errors.append("Unknown error occured!") + flash("An error occured !", "danger") + + +def SelectUpdateDistribusi(distribusiname): + print(f"Update this distribusi {distribusiname}") + user = User.query.filter_by(email=current_user.email).first() + try: + user.currentdistribusi = distribusiname + db.session.commit() + except: + db.session.rollback() + selectorform.new.errors.append("Unknown error occured!") + flash("An error occured !", "danger") + + +def DeleteDistribusi(distribusiname): + print(f"delete this distribusi {distribusiname}") + + +def SelectorVisible(): + has_distribusi = HasDistribusi() + distribusi_selected = DistribusiSelected() + if distribusi_selected: + return False + if not has_distribusi: + return False + return True diff --git a/verse/start.py b/verse/start.py index 8f4a62b..b0cb0fe 100644 --- a/verse/start.py +++ b/verse/start.py @@ -36,8 +36,15 @@ from forms.themeform import ThemeForm from forms.editorform import EditorForm from forms.selectorform import SelectorForm +# CRUD! +from selector import SelectNewDistribusi +from selector import SelectUpdateDistribusi +from selector import DeleteDistribusi +from selector import SelectorVisible + + from statuspengguna.helper import AreFilesUploaded -from statuspengguna.helper import HasDistribusi +from statuspengguna.helper import CurrentDistribusi from statuspengguna.loginuser import LoginUser from statuspengguna.registeruser import RegisterUser @@ -76,10 +83,14 @@ def distribusi(): selectorform.distribusis.choices = distribusisfields() files_uploaded = AreFilesUploaded() - has_distribusi = HasDistribusi() + selectorvisible = SelectorVisible() + current_distribusi = CurrentDistribusi() user = User.query.filter_by(email=current_user.email).first() - distribusi = Distribusis.query.filter_by(userid=user.id).first() + distribusi = Distribusis.query.filter_by( + distribusiname=current_distribusi + ).first() + if distribusiform.validate_on_submit(): zipfilename = "{}.zip".format(distribusi.distribusiname) userfolder = os.path.join("stash", distribusi.distribusiname) @@ -101,6 +112,7 @@ def distribusi(): args = parser.parse_args(["-s", cssfile]) distribusify(args, userfolder) distribusi.visible = True + user.currentdistribusi = None db.session.commit() return redirect(url_for("index")) template = render_template( @@ -110,7 +122,7 @@ def distribusi(): themeform=themeform, selectorform=selectorform, files_uploaded=files_uploaded, - has_distribusi=has_distribusi, + selectorvisible=selectorvisible, ) return template @@ -125,7 +137,7 @@ def upload(): selectorform.distribusis.choices = distribusisfields() files_uploaded = AreFilesUploaded() - has_distribusi = HasDistribusi() + selectorvisible = SelectorVisible() if uploadform.validate_on_submit(): user = User.query.filter_by(email=current_user.email).first() @@ -139,6 +151,7 @@ def upload(): year=uploadform.academicyear.data, tags=uploadform.tags.data, ) + user.currentdistribusi = uploadform.sitename.data db.session.add(newdistribusi) db.session.commit() except InvalidRequestError: @@ -171,7 +184,7 @@ def upload(): themeform=themeform, selectorform=selectorform, files_uploaded=files_uploaded, - has_distribusi=has_distribusi, + selectorvisible=selectorvisible, ) return template @@ -186,12 +199,11 @@ def theme(): selectorform.distribusis.choices = distribusisfields() files_uploaded = AreFilesUploaded() - has_distribusi = HasDistribusi() + selectorvisible = SelectorVisible() + current_distribusi = CurrentDistribusi() if themeform.validate_on_submit(): - user = User.query.filter_by(email=current_user.email).first() - distribusi = Distribusis.query.filter_by(userid=user.id).first() - newuserfolder = os.path.join("stash", distribusi.distribusiname) + newuserfolder = os.path.join("stash", current_distribusi) copycssfile = os.path.join( "themes", "{}.css".format(themeform.theme.data), @@ -205,7 +217,7 @@ def theme(): themeform=themeform, selectorform=selectorform, files_uploaded=files_uploaded, - has_distribusi=has_distribusi, + selectorvisible=selectorvisible, ) return template @@ -215,9 +227,10 @@ def theme(): def editor(): editorform = EditorForm() files_uploaded = AreFilesUploaded() - user = User.query.filter_by(email=current_user.email).first() + current_distribusi = CurrentDistribusi() + if editorform.validate_on_submit(): - userfolder = os.path.join("stash", user.distribusiname) + userfolder = os.path.join("stash", current_distribusi) cssfilename = "{}.css".format(editorform.cssname.data) with open(os.path.join(userfolder, cssfilename), "w") as cssfile: cssfile.write(editorform.css.data) @@ -234,12 +247,23 @@ def editor(): @APP.route("/selector", methods=["GET", "POST"]) @login_required def selector(): + selectorform = SelectorForm() + uploadform = UploadForm() + distribusiform = DistribusiForm() + themeform = ThemeForm() selectorform = SelectorForm() selectorform.distribusis.choices = distribusisfields() - user = User.query.filter_by(email=current_user.email).first() + + files_uploaded = AreFilesUploaded() + selectorvisible = SelectorVisible() + if selectorform.validate_on_submit(): - print(selectorform.distribusis.data) - print() + if selectorform.new.data: + SelectNewDistribusi() + if selectorform.delete.data: + DeleteDistribusi(selectorform.distribusis.data) + if selectorform.update.data: + SelectUpdateDistribusi(selectorform.distribusis.data) template = render_template( "distribusi.html", uploadform=uploadform, @@ -247,10 +271,11 @@ def selector(): themeform=themeform, selectorform=selectorform, files_uploaded=files_uploaded, - has_distribusi=has_distribusi, + selectorvisible=selectorvisible, ) return template + @APP.route("/stash/") def distribusistash(path): return send_from_directory("stash", path) diff --git a/verse/statuspengguna/helper.py b/verse/statuspengguna/helper.py index 84ce7d8..0c2782b 100644 --- a/verse/statuspengguna/helper.py +++ b/verse/statuspengguna/helper.py @@ -28,6 +28,21 @@ def HasDistribusi(): return True +def CurrentDistribusi(): + user = User.query.filter_by(email=current_user.email).first() + if user.currentdistribusi is None: + return "None" + if user.currentdistribusi is "new": + return "new" + return user.currentdistribusi + + +def DistribusiSelected(): + user = User.query.filter_by(email=current_user.email).first() + if user.currentdistribusi is None: + return False + return True + # def IsThemeSelected # def IsCustomThemePresent # def IsDistribusiLive diff --git a/verse/templates/distribusi.html b/verse/templates/distribusi.html index 7c4b320..a36068c 100644 --- a/verse/templates/distribusi.html +++ b/verse/templates/distribusi.html @@ -13,25 +13,26 @@
- {% if has_distribusi %} - {% block selector %} - {% include "distribusiworkflow/selector.html" %} - {% endblock selector%} + {% if selectorvisible %} + {% block selector %} + {% include "distribusiworkflow/selector.html" %} + {% endblock selector%} + {% else %} + {% block upload %} + {% include "distribusiworkflow/upload.html" %} + {% endblock upload%} + + {% block theme %} + {% include "distribusiworkflow/theme.html" %} + {% endblock theme%} + + {% block editcss %} + {% include "distribusiworkflow/editcss.html" %} + {% endblock editcss%} + + {% block launch %} + {% include "distribusiworkflow/launch.html" %} + {% endblock launch%} {%endif%} - {% block upload %} - {% include "distribusiworkflow/upload.html" %} - {% endblock upload%} - - {% block theme %} - {% include "distribusiworkflow/theme.html" %} - {% endblock theme%} - - {% block editcss %} - {% include "distribusiworkflow/editcss.html" %} - {% endblock editcss%} - - {% block launch %} - {% include "distribusiworkflow/launch.html" %} - {% endblock launch%}
{% endblock main %} diff --git a/verse/templates/distribusiworkflow/selector.html b/verse/templates/distribusiworkflow/selector.html index bc343e2..715a7b0 100644 --- a/verse/templates/distribusiworkflow/selector.html +++ b/verse/templates/distribusiworkflow/selector.html @@ -1,7 +1,7 @@

Welcome back to your Distribusi

You have already uploaded a distribusi website, do you want to make a new one, update or delete?

-
+ {{ selectorform.csrf_token }}
{{ selectorform.distribusis.label }}