# Copyright 2012-2014 Brian May## This file is part of python-tldap.## python-tldap is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## python-tldap is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with python-tldap If not, see <http://www.gnu.org/licenses/>.""" Transaction middleware for Django. """fromdjango.utils.deprecationimportMiddlewareMixinimporttldap.transaction
[docs]classTransactionMiddleware(MiddlewareMixin):""" Transaction middleware. If this is enabled, each view function will be run with commit_on_response activated - that way a save() doesn't do a direct commit, the commit is done when a successful response is created. If an exception happens, the database is rolled back. """
[docs]defprocess_exception(self,request,exception):"""Rolls back the database and leaves transaction management"""tldap.transaction.rollback()
[docs]defprocess_response(self,request,response):"""Commits and leaves transaction management."""iftldap.transaction.is_managed():tldap.transaction.commit()tldap.transaction.leave_transaction_management()returnresponse