Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Perform a session rollback in CRC if needed

At the moment, tgext.crud or sprox don't perform a rollback, but it's
needed or all session-bound objects won't be accessible in the
error_handler page.

More or less fixes #86.
  • Loading branch information...
commit a7a2867cac8a255cbc6e804e33f922be3736b589 1 parent dcff939
@moschlar authored
Showing with 15 additions and 3 deletions.
  1. +14 −2 sauce/controllers/crc.py
  2. +1 −1  sauce/model/__init__.py
View
16 sauce/controllers/crc.py
@@ -7,12 +7,14 @@
@author: moschlar
'''
+import sys
import logging
from itertools import groupby
from tg import expose, tmpl_context as c, request, flash, lurl, abort
-from tg.decorators import before_validate, cached_property, before_render, override_template
+from tg.decorators import before_validate, before_call, before_render,\
+ cached_property, override_template
from tgext.crud import CrudRestController, EasyCrudRestController
#from tw2.forms import TextField, SingleSelectField, Label, TextArea, CheckBox
@@ -33,7 +35,9 @@
Assignment, Test, NewsItem)
from sauce.widgets.datagrid import JSSortableDataGrid
from webhelpers.html.builder import literal
-from sqlalchemy.orm.properties import RelationshipProperty
+
+from sqlalchemy.exc import IntegrityError, DatabaseError, ProgrammingError
+errors = (IntegrityError, DatabaseError, ProgrammingError)
__all__ = ['TeamsCrudController', 'StudentsCrudController', 'TutorsCrudController',
'TeachersCrudController', 'EventsCrudController', 'LessonsCrudController',
@@ -335,6 +339,12 @@ def injector(cls, remainder, params):
for i in s.inject:
params[i] = s.inject[i]
+ @staticmethod
+ def rollback(remainder, params):
+ '''Perform a session rollback when tgext.crud and/or sprox don't do'''
+ if sys.exc_info()[0] in errors:
+ DBSession.rollback()
+
# Register injection hook for POST requests
before_validate(FilteredCrudRestController.injector)(FilteredCrudRestController.post)
@@ -342,8 +352,10 @@ def injector(cls, remainder, params):
before_render(FilteredCrudRestController.before_get_all)(FilteredCrudRestController.get_all)
# Register hook for new
before_render(FilteredCrudRestController.before_new)(FilteredCrudRestController.new)
+before_call(FilteredCrudRestController.rollback)(FilteredCrudRestController.new)
# Register hook for edit
before_render(FilteredCrudRestController.before_edit)(FilteredCrudRestController.edit)
+before_call(FilteredCrudRestController.rollback)(FilteredCrudRestController.edit)
#--------------------------------------------------------------------------------
View
2  sauce/model/__init__.py
@@ -13,7 +13,7 @@
# Global session manager: DBSession() returns the Thread-local
# session object appropriate for the current web request.
-maker = sessionmaker(autoflush=True, autocommit=False, expire_on_commit=False,
+maker = sessionmaker(autoflush=True, autocommit=False, expire_on_commit=False,
extension=ZopeTransactionExtension())
DBSession = scoped_session(maker)
Please sign in to comment.
Something went wrong with that request. Please try again.