PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/django/middleware/transaction.py

https://code.google.com/p/mango-py/
Python | 27 lines | 22 code | 1 blank | 4 comment | 0 complexity | c8c092700d23db72304e2725fa3184ae MD5 | raw file
 1from django.db import transaction
 2
 3class TransactionMiddleware(object):
 4    """
 5    Transaction middleware. If this is enabled, each view function will be run
 6    with commit_on_response activated - that way a save() doesn't do a direct
 7    commit, the commit is done when a successful response is created. If an
 8    exception happens, the database is rolled back.
 9    """
10    def process_request(self, request):
11        """Enters transaction management"""
12        transaction.enter_transaction_management()
13        transaction.managed(True)
14
15    def process_exception(self, request, exception):
16        """Rolls back the database and leaves transaction management"""
17        if transaction.is_dirty():
18            transaction.rollback()
19        transaction.leave_transaction_management()
20
21    def process_response(self, request, response):
22        """Commits and leaves transaction management."""
23        if transaction.is_managed():
24            if transaction.is_dirty():
25                transaction.commit()
26            transaction.leave_transaction_management()
27        return response