source: mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py @ 8351

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

remove the SQL echo

Line 
1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
6from MolesManager.moles3epb import Moles3EPBFactory
7from libs.migration.client import MigrationThread
8from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
9    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
10    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
11from libs.commons_db import DbManager
12from libs.migration.InfodbEPB import InfodbEPBFactory
13from libs.migration.MigrationEPB import MigrationEPBFactory
14from sqlalchemy.exc import SQLAlchemyError
15from django.core.context_processors import csrf
16from django.shortcuts import render_to_response
17
18
19
20class MolesSessionMiddleware(object):
21   
22    #This attribute should be not here.
23    #unfortunately I cannot find any start/stop signals from Django
24    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
25    _epbInitialized = False
26    _infodbEPBFactory = None   
27    _moles3EPBFactory = None
28    _migrationEPBFactory = None
29   
30    def _doInitialization(self):
31
32        infoDB = DbManager(INFO_DB_CONNECTION)
33        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
34        #InfodbEPB.overrrideDBManager(infoDB)
35           
36        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
37        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
38
39        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
40        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
41       
42        MolesSessionMiddleware._migrationThread = MigrationThread( \
43            MolesSessionMiddleware._moles3EPBFactory.createEPB(), \
44            MolesSessionMiddleware._migrationEPBFactory.createEPB(), \
45            MolesSessionMiddleware._infodbEPBFactory.createEPB(), \
46            interval = MIGRATION_INTERVAL )
47        MolesSessionMiddleware._epbInitialized = True     
48           
49        self._migration() #see the note on  MolesSessionMiddleware._migration     
50   
51    """
52    def _getMolesSession(self):
53        '''
54            @deprecated: db session is going to be removed from all GUI-related instances
55        '''
56        if not MolesSessionMiddleware._epbInitialized:
57            self._doInitialization()
58
59        return self._getNewMolesSession()
60    """
61   
62    def _migration(self, runMigration = RUN_MIGRATION):
63        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
64            #t.setDaemon(False)
65            MolesSessionMiddleware._migrationThread.start()
66           
67    """
68        Represents the access to the Moles database.
69        Creates from the existing db connections pools a new session
70        to be used by all the DB operations involved in the actual HTTPRequest
71    """ 
72   
73    def process_request(self, request): 
74        try:
75            if not MolesSessionMiddleware._epbInitialized:
76                self._doInitialization()
77                 
78            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()                   
79        except SQLAlchemyError as ex:
80            MolesSessionMiddleware._epbInitialized = False
81            return self.process_exception(request, ex)
82
83    def process_response(self, request, response):
84        if hasattr(request, "moles3EPB"):           
85            request.moles3EPB.close()
86           
87        return response
88
89    def process_exception(self, request, exception):
90        try:
91            c = {}
92            c['exception'] = exception
93            c.update(csrf(request))
94            return render_to_response('errorTemplate.html', c)
95        except SQLAlchemyError as ex:
96            MolesSessionMiddleware._epbInitialized = False           
97       
Note: See TracBrowser for help on using the repository browser.