Browse Source

more thourough database exception handling

current_wdka_release
crunk 3 years ago
parent
commit
c2eb48590e
  1. 63
      verse/editor.py
  2. 10
      verse/selector.py
  3. 8
      verse/start.py
  4. 13
      verse/statuspengguna/helper.py
  5. 2
      verse/templates/distribusiworkflow/selector.html
  6. 0
      verse/themes/publicthemes/.gitignore
  7. 3
      verse/themes/userthemes/.gitignore
  8. 7
      verse/upload.py

63
verse/editor.py

@ -1,5 +1,11 @@
import os import os
from flask import render_template from flask import render_template
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from statuspengguna.helper import ( from statuspengguna.helper import (
IsZipUploaded, IsZipUploaded,
@ -14,7 +20,7 @@ from forms.editorform import EditorForm
from forms.selectorform import SelectorForm from forms.selectorform import SelectorForm
def EditCss(): def Editor():
editorform = EditorForm() editorform = EditorForm()
current_distribusi = CurrentDistribusi() current_distribusi = CurrentDistribusi()
if editorform.validate_on_submit(): if editorform.validate_on_submit():
@ -25,15 +31,68 @@ def EditCss():
def ValidateEditCssForm(editorform, current_distribusi): def ValidateEditCssForm(editorform, current_distribusi):
newcssfolder = os.path.join("themes/userstyles", current_distribusi) newcssfolder = os.path.join("themes/userthemes", current_distribusi)
if editorform.public.data:
MakePublicTheme(editorform, current_distribusi)
publicfolder = os.path.join("themes/publicthemes", current_distribusi)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, publicfolder)
else:
WriteCssToFile(editorform, publicfolder)
if editorform.cssfile.data:
SaveUploadCssFile(editorform, newcssfolder)
return
if editorform.cssname.data:
WriteCssToFile(editorform, newcssfolder)
def SaveUploadCssFile(editorform, newcssfolder):
if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder)
cssfile = editorform.cssfile.data
cssfile.save(os.path.join(newcssfolder, editorform.cssfile.data.filename))
def WriteCssToFile(editorform, newcssfolder):
if not os.path.exists(newcssfolder): if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder) os.mkdir(newcssfolder)
cssfilename = "{}.css".format(editorform.cssname.data) cssfilename = "{}.css".format(editorform.cssname.data)
with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile: with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile:
cssfile.write(editorform.css.data) cssfile.write(editorform.css.data)
cssfile.close cssfile.close
def MakePublicTheme(editorform, current_distribusi):
try:
distribusi = Distribusis.query.filter_by(
distribusiname=current_distribusi
).first()
distribusi.publictheme = editorform.public.data
db.session.commit()
except InvalidRequestError:
db.session.rollback()
editorform.public.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
except DataError:
db.session.rollback()
editorform.public.errors.append("Invalid Entry")
flash("Invalid Entry", "warning")
except InterfaceError:
db.session.rollback()
editorform.public.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
except DatabaseError:
db.session.rollback()
editorform.public.errors.append(
"Error connecting to the database"
)
flash("Error connecting to the database", "danger")
def RenderDistribusiTemplate(current_distribusi): def RenderDistribusiTemplate(current_distribusi):
uploadform = UploadForm() uploadform = UploadForm()
distribusiform = DistribusiForm() distribusiform = DistribusiForm()

10
verse/selector.py

@ -2,6 +2,12 @@ import os
import shutil import shutil
from flask import flash from flask import flash
from flask_login import current_user from flask_login import current_user
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from usermodel import User from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
@ -42,11 +48,11 @@ def DeleteDistribusi(distribusiname):
userfolder = os.path.join("stash", distribusi.distribusiname) userfolder = os.path.join("stash", distribusi.distribusiname)
shutil.rmtree(userfolder) shutil.rmtree(userfolder)
cssfolder = os.path.join( cssfolder = os.path.join(
"themes/userstyles", "themes/userthemes",
distribusi.distribusiname distribusi.distribusiname
) )
shutil.rmtree(cssfolder) shutil.rmtree(cssfolder)
except: except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
selectorform.distribusis.errors.append("Unknown error occured!") selectorform.distribusis.errors.append("Unknown error occured!")
flash("An error occured !", "danger") flash("An error occured !", "danger")

8
verse/start.py

@ -36,7 +36,7 @@ from selector import (
DeleteDistribusi, DeleteDistribusi,
SelectorVisible, SelectorVisible,
) )
from editor import EditCss from editor import Editor
# Upload # Upload
from upload import UploadNewDistribusi, UploadUpdatedFiles from upload import UploadNewDistribusi, UploadUpdatedFiles
@ -131,7 +131,7 @@ def distribusi():
# 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 = ""
cssfolder = os.path.join( cssfolder = os.path.join(
"themes/userstyles", distribusi.distribusiname "themes/userthemes", distribusi.distribusiname
) )
for filename in os.listdir(cssfolder): for filename in os.listdir(cssfolder):
if filename.endswith(".css"): if filename.endswith(".css"):
@ -206,7 +206,7 @@ def theme():
css_selected = IsCssSelected(current_distribusi) css_selected = IsCssSelected(current_distribusi)
if themeform.validate_on_submit(): if themeform.validate_on_submit():
newcssfolder = os.path.join("themes/userstyles", current_distribusi) newcssfolder = os.path.join("themes/userthemes", current_distribusi)
if not os.path.exists(newcssfolder): if not os.path.exists(newcssfolder):
os.mkdir(newcssfolder) os.mkdir(newcssfolder)
copycssfile = os.path.join( copycssfile = os.path.join(
@ -233,7 +233,7 @@ def theme():
@APP.route("/editor", methods=["GET", "POST"]) @APP.route("/editor", methods=["GET", "POST"])
@login_required @login_required
def editor(): def editor():
return EditCss() return Editor()
@APP.route("/selector", methods=["GET", "POST"]) @APP.route("/selector", methods=["GET", "POST"])

13
verse/statuspengguna/helper.py

@ -1,6 +1,13 @@
import os import os
from flask_login import current_user from flask_login import current_user
from flask import flash from flask import flash
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from usermodel import User from usermodel import User
from distribusimodel import Distribusis from distribusimodel import Distribusis
from app import db from app import db
@ -31,7 +38,7 @@ def IsDistribusiLive(distribusiname):
def IsCssSelected(distribusiname): def IsCssSelected(distribusiname):
userfolder = os.path.join("themes/userstyles", distribusiname) userfolder = os.path.join("themes/userthemes", 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"):
@ -70,7 +77,7 @@ def ResetUserState():
user = User.query.filter_by(email=current_user.email).first() user = User.query.filter_by(email=current_user.email).first()
user.currentdistribusi = None user.currentdistribusi = None
db.session.commit() db.session.commit()
except: except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
flash("An error occured !", "danger") flash("An error occured !", "danger")
@ -80,7 +87,7 @@ def SelectCurrentDistribusi(distribusiname):
try: try:
user.currentdistribusi = distribusiname user.currentdistribusi = distribusiname
db.session.commit() db.session.commit()
except: except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
flash("An error occured !", "danger") flash("An error occured !", "danger")

2
verse/templates/distribusiworkflow/selector.html

@ -17,6 +17,7 @@
<fieldset class="button required multiselect update"> <fieldset class="button required multiselect update">
{{ selectorform.update }} {{ selectorform.update }}
</fieldset> </fieldset>
<hr>
<p> <p>
This will delete your distribusi site. This will delete your distribusi site.
<strong> This action cannot be undone! </strong> <strong> This action cannot be undone! </strong>
@ -24,6 +25,7 @@
<fieldset class="button required multiselect delete"> <fieldset class="button required multiselect delete">
{{ selectorform.delete }} {{ selectorform.delete }}
</fieldset> </fieldset>
<hr>
<p>Alternatively you can make a new site</p> <p>Alternatively you can make a new site</p>
<fieldset class="button required multiselect"> <fieldset class="button required multiselect">
{{ selectorform.new }} {{ selectorform.new }}

0
verse/themes/userstyles/.gitignore → verse/themes/publicthemes/.gitignore

3
verse/themes/userthemes/.gitignore

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

7
verse/upload.py

@ -5,6 +5,9 @@ from flask_login import current_user
from sqlalchemy.exc import ( from sqlalchemy.exc import (
IntegrityError, IntegrityError,
InvalidRequestError, InvalidRequestError,
DataError,
InterfaceError,
DatabaseError,
) )
from app import db from app import db
@ -32,7 +35,7 @@ def UploadNewDistribusi(uploadfolder):
user.currentdistribusi = uploadform.sitename.data user.currentdistribusi = uploadform.sitename.data
db.session.add(newdistribusi) db.session.add(newdistribusi)
db.session.commit() db.session.commit()
except InvalidRequestError: except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!") uploadform.sitename.errors.append("Something went wrong!")
flash("Something went wrong!", "danger") flash("Something went wrong!", "danger")
@ -71,7 +74,7 @@ def UploadUpdatedFiles(uploadfolder):
distribusi.year = uploadform.academicyear.data distribusi.year = uploadform.academicyear.data
distribusi.tags = uploadform.tags.data distribusi.tags = uploadform.tags.data
db.session.commit() db.session.commit()
except: except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback() db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!") uploadform.sitename.errors.append("Something went wrong!")
flash("Something went wrong!", "danger") flash("Something went wrong!", "danger")

Loading…
Cancel
Save