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

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

New model classes. Cleaned many redundant attributes. Better handling of the lists
Incomplete - # 22428: CEDA Project - observationCollection
 http://team.ceda.ac.uk/trac/ceda/ticket/22428

Line 
1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
6from MolesManager.moles3epb import Moles3EPB
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.MigrationEPB import MigrationEPB
13from libs.migration.InfodbEPB import InfodbEPB
14import logging
15
16
17
18class MolesSessionMiddleware(object):
19   
20    #This attribute should be not here.
21    #unfortunately I cannot find any start/stop signals from Django
22    _migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
23    _epbInitialized = False
24   
25
26
27
28    def _getNewMolesSession(self):
29        session = Moles3EPB.getNewMolesSession() 
30        return session
31   
32    def _doInitialization(self):
33        infoDB = DbManager(INFO_DB_CONNECTION)
34        InfodbEPB.overrrideDBManager(infoDB)
35           
36        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
37        MigrationEPB.overrrideDBManager(migrationDB)       
38
39        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
40        Moles3EPB.overrrideDBManager(molesDB)
41        MolesSessionMiddleware._epbInitialized = True           
42   
43    def _getMolesSession(self):
44        if not MolesSessionMiddleware._epbInitialized:
45            self._doInitialization()
46
47        return self._getNewMolesSession()
48   
49    def _migration(self, runMigration = RUN_MIGRATION):
50        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
51            #t.setDaemon(False)
52            MolesSessionMiddleware._migrationThread.start()
53           
54    """
55        Represents the access to the Moles database.
56        Creates from the existing db connections pools a new session
57        to be used by all the DB operations involved in the actual HTTPRequest
58    """ 
59   
60    def process_request(self, request):       
61        self._migration() #see the note on  MolesSessionMiddleware._migration
62       
63        request.moles_session = self._getMolesSession()
64
65
66    def process_response(self, request, response):
67        if hasattr(request, 'moles_session'):
68            request.moles_session.close()
69
70        return response
71
72
73    def process_exception(self, request, exception):
74        try:
75            session = request.moles_session           
76        except AttributeError:
77            return
78        session.rollback()
79        session.close()       
Note: See TracBrowser for help on using the repository browser.