From fc4b1a5c75e2a79979ddb33f8acb1ea947240815 Mon Sep 17 00:00:00 2001 From: crunk Date: Fri, 4 Mar 2022 19:08:55 +0100 Subject: [PATCH] The U and D part of CRUD --- verse/selector.py | 45 +++++---- verse/start.py | 94 ++++++++----------- verse/statuspengguna/helper.py | 34 +++++-- .../distribusiworkflow/selector.html | 10 +- verse/upload.py | 88 +++++++++++++++++ 5 files changed, 183 insertions(+), 88 deletions(-) create mode 100644 verse/upload.py diff --git a/verse/selector.py b/verse/selector.py index b51c3f8..207d8d5 100644 --- a/verse/selector.py +++ b/verse/selector.py @@ -1,20 +1,11 @@ -from flask import ( - render_template, - redirect, - request, - flash, - url_for, - abort, -) - -from usermodel import User +import os +import shutil +from flask import flash 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 usermodel import User +from distribusimodel import Distribusis + from forms.selectorform import SelectorForm from statuspengguna.helper import HasDistribusi @@ -25,30 +16,50 @@ 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.new.errors.append("Unknown error occured!") + selectorform.distribusis.errors.append("Unknown error occured!") flash("An error occured !", "danger") + 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.new.errors.append("Unknown error occured!") + selectorform.distribusis.errors.append("Unknown error occured!") flash("An error occured !", "danger") + return selectorform def DeleteDistribusi(distribusiname): print(f"delete this distribusi {distribusiname}") + selectorform = SelectorForm() + try: + user = User.query.filter_by(email=current_user.email).first() + distribusi = Distribusis.query.filter_by( + distribusiname=distribusiname + ).first() + if distribusi.userid is user.id: + db.session.delete(distribusi) + db.session.commit() + userfolder = os.path.join("stash", distribusi.distribusiname) + shutil.rmtree(userfolder) + except: + db.session.rollback() + selectorform.distribusis.errors.append("Unknown error occured!") + flash("An error occured !", "danger") + return selectorform def SelectorVisible(): diff --git a/verse/start.py b/verse/start.py index b0cb0fe..92368f8 100644 --- a/verse/start.py +++ b/verse/start.py @@ -8,15 +8,10 @@ from datetime import timedelta from flask import ( render_template, redirect, - flash, url_for, session, send_from_directory, ) -from sqlalchemy.exc import ( - IntegrityError, - InvalidRequestError, -) from flask_login import ( logout_user, login_required, @@ -42,9 +37,14 @@ from selector import SelectUpdateDistribusi from selector import DeleteDistribusi from selector import SelectorVisible +# Upload +from upload import UploadNewDistribusi +from upload import UploadUpdatedFiles -from statuspengguna.helper import AreFilesUploaded +# UserPengguna +from statuspengguna.helper import IsZipUploaded from statuspengguna.helper import CurrentDistribusi +from statuspengguna.helper import ResetUserState from statuspengguna.loginuser import LoginUser from statuspengguna.registeruser import RegisterUser @@ -63,6 +63,7 @@ def session_handler(): @APP.route("/") def index(): + ResetUserState() distribusis = Distribusis.query.filter( Distribusis.distribusiname.isnot(None) ).all() @@ -82,10 +83,9 @@ def distribusi(): selectorform = SelectorForm() selectorform.distribusis.choices = distribusisfields() - files_uploaded = AreFilesUploaded() selectorvisible = SelectorVisible() current_distribusi = CurrentDistribusi() - + files_uploaded = IsZipUploaded(current_distribusi) user = User.query.filter_by(email=current_user.email).first() distribusi = Distribusis.query.filter_by( distribusiname=current_distribusi @@ -130,53 +130,20 @@ def distribusi(): @APP.route("/upload", methods=["POST"]) @login_required def upload(): - uploadform = UploadForm() distribusiform = DistribusiForm() themeform = ThemeForm() selectorform = SelectorForm() selectorform.distribusis.choices = distribusisfields() - files_uploaded = AreFilesUploaded() selectorvisible = SelectorVisible() - if uploadform.validate_on_submit(): - user = User.query.filter_by(email=current_user.email).first() + current_distribusi = CurrentDistribusi() + if current_distribusi == "new": + uploadform = UploadNewDistribusi(APP.config["UPLOAD_FOLDER"]) + else: + uploadform = UploadUpdatedFiles(APP.config["UPLOAD_FOLDER"]) + files_uploaded = IsZipUploaded(uploadform.sitename.data) - try: - newdistribusi = Distribusis( - distribusiname=uploadform.sitename.data, - userid=user.id, - term=uploadform.term.data, - course=uploadform.course.data, - year=uploadform.academicyear.data, - tags=uploadform.tags.data, - ) - user.currentdistribusi = uploadform.sitename.data - db.session.add(newdistribusi) - db.session.commit() - except InvalidRequestError: - db.session.rollback() - uploadform.sitename.errors.append("Something went wrong!") - flash("Something went wrong!", "danger") - except IntegrityError: - db.session.rollback() - uploadform.sitename.errors.append( - "distribusi name already exists!" - ) - flash("distribusi name already exists!", "warning") - - zipfilename = "{}.zip".format(newdistribusi.distribusiname) - zipfile = uploadform.zipfile.data - zipfile.save(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename)) - - newuserfolder = os.path.join("stash", newdistribusi.distribusiname) - if not os.path.exists(newuserfolder): - os.mkdir(newuserfolder) - - copyzipfile = os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename) - shutil.copy(copyzipfile, newuserfolder) - os.remove(os.path.join(APP.config["UPLOAD_FOLDER"], zipfilename)) - files_uploaded = AreFilesUploaded() template = render_template( "distribusi.html", uploadform=uploadform, @@ -198,9 +165,9 @@ def theme(): selectorform = SelectorForm() selectorform.distribusis.choices = distribusisfields() - files_uploaded = AreFilesUploaded() selectorvisible = SelectorVisible() current_distribusi = CurrentDistribusi() + files_uploaded = IsZipUploaded(current_distribusi) if themeform.validate_on_submit(): newuserfolder = os.path.join("stash", current_distribusi) @@ -226,9 +193,8 @@ def theme(): @login_required def editor(): editorform = EditorForm() - files_uploaded = AreFilesUploaded() current_distribusi = CurrentDistribusi() - + files_uploaded = IsZipUploaded(current_distribusi) if editorform.validate_on_submit(): userfolder = os.path.join("stash", current_distribusi) cssfilename = "{}.css".format(editorform.cssname.data) @@ -253,17 +219,31 @@ def selector(): themeform = ThemeForm() selectorform = SelectorForm() selectorform.distribusis.choices = distribusisfields() - - files_uploaded = AreFilesUploaded() - selectorvisible = SelectorVisible() - + current_distribusi = CurrentDistribusi() if selectorform.validate_on_submit(): if selectorform.new.data: - SelectNewDistribusi() + selectorform = SelectNewDistribusi() if selectorform.delete.data: - DeleteDistribusi(selectorform.distribusis.data) + selectorform = DeleteDistribusi(selectorform.distribusis.data) + selectorform.distribusis.choices = distribusisfields() if selectorform.update.data: - SelectUpdateDistribusi(selectorform.distribusis.data) + selectorform = SelectUpdateDistribusi( + selectorform.distribusis.data + ) + current_distribusi = CurrentDistribusi() + distribusi = Distribusis.query.filter_by( + distribusiname=current_distribusi + ).first() + uploadform.sitename.data = distribusi.distribusiname + uploadform.sitename.render_kw = {"readonly": True} + uploadform.term.data = distribusi.term + uploadform.course.data = distribusi.course + uploadform.academicyear.data = distribusi.year + uploadform.tags.data = distribusi.tags + + files_uploaded = IsZipUploaded(current_distribusi) + selectorvisible = SelectorVisible() + template = render_template( "distribusi.html", uploadform=uploadform, diff --git a/verse/statuspengguna/helper.py b/verse/statuspengguna/helper.py index 0c2782b..43ea19e 100644 --- a/verse/statuspengguna/helper.py +++ b/verse/statuspengguna/helper.py @@ -1,19 +1,20 @@ import os from flask_login import current_user +from flask import flash from usermodel import User from distribusimodel import Distribusis +from app import db -def AreFilesUploaded(): - user = User.query.filter_by(email=current_user.email).first() - distribusi = Distribusis.query.filter_by(userid=user.id).first() - if distribusi is None: - print("distribusi folder is empty") - return False - userfolder = os.path.join("stash", distribusi.distribusiname) +def IsZipUploaded(distribusiname): + userfolder = os.path.join("stash", distribusiname) if os.path.exists(userfolder): - print("folder found, files are uploaded") - return True + zipfilename = "{}.zip".format(distribusiname) + if os.path.exists(os.path.join(userfolder, zipfilename)): + print("folder with zipfile found, file uploaded") + return True + print("distribusi folder is empty") + return False def HasDistribusi(): @@ -32,7 +33,7 @@ def CurrentDistribusi(): user = User.query.filter_by(email=current_user.email).first() if user.currentdistribusi is None: return "None" - if user.currentdistribusi is "new": + if user.currentdistribusi == "new": return "new" return user.currentdistribusi @@ -43,6 +44,19 @@ def DistribusiSelected(): return False return True + +def ResetUserState(): + """reset user state upon visiting index, so that distribusi workflow can + be done correctly""" + try: + user = User.query.filter_by(email=current_user.email).first() + user.currentdistribusi = None + db.session.commit() + except: + db.session.rollback() + flash("An error occured !", "danger") + + # def IsThemeSelected # def IsCustomThemePresent # def IsDistribusiLive diff --git a/verse/templates/distribusiworkflow/selector.html b/verse/templates/distribusiworkflow/selector.html index 715a7b0..358a049 100644 --- a/verse/templates/distribusiworkflow/selector.html +++ b/verse/templates/distribusiworkflow/selector.html @@ -1,5 +1,5 @@
-

Welcome back to your Distribusi

+

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 }} @@ -13,13 +13,15 @@
- {{ selectorform.new }} -
-
{{ selectorform.update }}
{{ selectorform.delete }}
+ +

Alternatively you can make a new site

+
+ {{ selectorform.new }} +
diff --git a/verse/upload.py b/verse/upload.py new file mode 100644 index 0000000..04ef6ae --- /dev/null +++ b/verse/upload.py @@ -0,0 +1,88 @@ +import os +import shutil +from flask import flash +from sqlalchemy.exc import ( + IntegrityError, + InvalidRequestError, +) +from usermodel import User +from distribusimodel import Distribusis +from flask_login import current_user +from statuspengguna.helper import CurrentDistribusi +from forms.uploadform import UploadForm +from app import db + + +def UploadNewDistribusi(uploadfolder): + uploadform = UploadForm() + if uploadform.validate_on_submit(): + user = User.query.filter_by(email=current_user.email).first() + try: + newdistribusi = Distribusis( + distribusiname=uploadform.sitename.data, + userid=user.id, + term=uploadform.term.data, + course=uploadform.course.data, + year=uploadform.academicyear.data, + tags=uploadform.tags.data, + ) + user.currentdistribusi = uploadform.sitename.data + db.session.add(newdistribusi) + db.session.commit() + except InvalidRequestError: + db.session.rollback() + uploadform.sitename.errors.append("Something went wrong!") + flash("Something went wrong!", "danger") + except IntegrityError: + db.session.rollback() + uploadform.sitename.errors.append( + "distribusi name already exists!" + ) + flash("distribusi name already exists!", "warning") + + zipfilename = "{}.zip".format(newdistribusi.distribusiname) + zipfile = uploadform.zipfile.data + zipfile.save(os.path.join(uploadfolder, zipfilename)) + + newuserfolder = os.path.join("stash", newdistribusi.distribusiname) + if not os.path.exists(newuserfolder): + os.mkdir(newuserfolder) + + copyzipfile = os.path.join(uploadfolder, zipfilename) + shutil.copy(copyzipfile, newuserfolder) + os.remove(os.path.join(uploadfolder, zipfilename)) + + return uploadform + + +def UploadUpdatedFiles(uploadfolder): + uploadform = UploadForm() + if uploadform.validate_on_submit(): + try: + current_distribusi = CurrentDistribusi() + distribusi = Distribusis.query.filter_by( + distribusiname=current_distribusi + ).first() + distribusi.term = uploadform.term.data + distribusi.course = uploadform.course.data + distribusi.year = uploadform.academicyear.data + distribusi.tags = uploadform.tags.data + db.session.commit() + except: + db.session.rollback() + uploadform.sitename.errors.append("Something went wrong!") + flash("Something went wrong!", "danger") + + zipfilename = "{}.zip".format(distribusi.distribusiname) + zipfile = uploadform.zipfile.data + zipfile.save(os.path.join(uploadfolder, zipfilename)) + + newuserfolder = os.path.join("stash", distribusi.distribusiname) + shutil.rmtree(newuserfolder) + os.mkdir(newuserfolder) + + copyzipfile = os.path.join(uploadfolder, zipfilename) + shutil.copy(copyzipfile, newuserfolder) + os.remove(os.path.join(uploadfolder, zipfilename)) + + return uploadform