PageRenderTime 3ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause
  1. from django.db import transaction
  2. class TransactionMiddleware(object):
  3. """
  4. Transaction middleware. If this is enabled, each view function will be run
  5. with commit_on_response activated - that way a save() doesn't do a direct
  6. commit, the commit is done when a successful response is created. If an
  7. exception happens, the database is rolled back.
  8. """
  9. def process_request(self, request):
  10. """Enters transaction management"""
  11. transaction.enter_transaction_management()
  12. transaction.managed(True)
  13. def process_exception(self, request, exception):
  14. """Rolls back the database and leaves transaction management"""
  15. if transaction.is_dirty():
  16. transaction.rollback()
  17. transaction.leave_transaction_management()
  18. def process_response(self, request, response):
  19. """Commits and leaves transaction management."""
  20. if transaction.is_managed():
  21. if transaction.is_dirty():
  22. transaction.commit()
  23. transaction.leave_transaction_management()
  24. return response