source: mauRepo/HPFos/trunk/src/HPFos/molesSessionMiddleware.py @ 8348

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/HPFos/trunk/src/HPFos/molesSessionMiddleware.py@8353
Revision 8348, 1.9 KB checked in by mnagni, 7 years ago (diff)

Refactored the project structure

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
6from libs.commons_db import DbManager
7from moles3epb import Moles3EPBFactory
8from settings import MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT
9from django.core.context_processors import csrf
10from django.shortcuts import render_to_response
11from sqlalchemy.exc import SQLAlchemyError
12
13class MolesSessionMiddleware(object):
14   
15    #This attribute should be not here.
16    #unfortunately I cannot find any start/stop signals from Django
17    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
18    _moles3EPBFactory = None
19    _epbInitialized = False
20   
21    def _doInitialization(self):
22        """
23            Represents the access to the Moles database.
24            Creates from the existing db connections pools a new session
25            to be used by all the DB operations involved in the actual HTTPRequest
26        """                   
27        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
28        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
29        MolesSessionMiddleware._epbInitialized = True
30   
31    def process_request(self, request):
32        try: 
33            if not MolesSessionMiddleware._epbInitialized:
34                self._doInitialization()
35            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()
36        except Exception as e:           
37            return self.process_exception(request, e)
38
39    def process_response(self, request, response):
40        if hasattr(request, 'moles3EPB'):
41            request.moles3EPB.close()
42        return response
43
44
45    def process_exception(self, request, exception):
46        if isinstance(exception, SQLAlchemyError):
47            MolesSessionMiddleware._epbInitialized = False
48        c = {}
49        c['exception'] = exception
50        c.update(csrf(request))
51        return render_to_response('exceptionResponseTemplate', c)
Note: See TracBrowser for help on using the repository browser.