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
from flask import render_template
from sqlalchemy.exc import (
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from statuspengguna.helper import (
IsZipUploaded,
@ -14,7 +20,7 @@ from forms.editorform import EditorForm
from forms.selectorform import SelectorForm
def EditCss():
def Editor():
editorform = EditorForm()
current_distribusi = CurrentDistribusi()
if editorform.validate_on_submit():
@ -25,15 +31,68 @@ def EditCss():
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):
os.mkdir(newcssfolder)
cssfilename = "{}.css".format(editorform.cssname.data)
with open(os.path.join(newcssfolder, cssfilename), "w") as cssfile:
cssfile.write(editorform.css.data)
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):
uploadform = UploadForm()
distribusiform = DistribusiForm()

10
verse/selector.py

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

8
verse/start.py

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

13
verse/statuspengguna/helper.py

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

2
verse/templates/distribusiworkflow/selector.html

@ -17,6 +17,7 @@
<fieldset class="button required multiselect update">
{{ selectorform.update }}
</fieldset>
<hr>
<p>
This will delete your distribusi site.
<strong> This action cannot be undone! </strong>
@ -24,6 +25,7 @@
<fieldset class="button required multiselect delete">
{{ selectorform.delete }}
</fieldset>
<hr>
<p>Alternatively you can make a new site</p>
<fieldset class="button required multiselect">
{{ 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 (
IntegrityError,
InvalidRequestError,
DataError,
InterfaceError,
DatabaseError,
)
from app import db
@ -32,7 +35,7 @@ def UploadNewDistribusi(uploadfolder):
user.currentdistribusi = uploadform.sitename.data
db.session.add(newdistribusi)
db.session.commit()
except InvalidRequestError:
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
@ -71,7 +74,7 @@ def UploadUpdatedFiles(uploadfolder):
distribusi.year = uploadform.academicyear.data
distribusi.tags = uploadform.tags.data
db.session.commit()
except:
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
db.session.rollback()
uploadform.sitename.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")

Loading…
Cancel
Save