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

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

Incomplete - # 22385: CEDA Observation - result
 http://team.ceda.ac.uk/trac/ceda/ticket/22385

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 ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier
14from libs.migration.InfodbEPB import InfodbEPB
15
16
17class MolesSessionMiddleware(object):
18   
19    #This attribute should be not here.
20    #unfortunately I cannot find any start/stop signals from Django
21    _migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
22    _epbInitialized = False
23   
24
25    def _initSearchIndexes(self, db_manager):
26        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS!
27        # We don't want sqlalchemy to know about this column so we add it externally.
28        try:
29            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                 
30
31            # This indexes the tsvector column
32
33            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")
34
35            # This sets up the trigger that keeps the tsvector column up to date.
36            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \
37                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")                       
38        except Exception as e:
39            pass
40
41    def _getNewMolesSession(self):
42        session = Moles3EPB.getNewMolesSession() 
43        EVENTS_DB(session)
44        return session
45   
46    def _getMolesSession(self):
47        if not MolesSessionMiddleware._epbInitialized:
48            infoDB = DbManager(INFO_DB_CONNECTION)
49            InfodbEPB.overrrideDBManager(infoDB)
50           
51            migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
52            MigrationEPB.overrrideDBManager(migrationDB)       
53
54            molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
55            Moles3EPB.overrrideDBManager(molesDB)
56            MolesSessionMiddleware._epbInitialized = True
57           
58            self._initSearchIndexes(molesDB)
59
60        return self._getNewMolesSession()
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        self._migration() #see the note on  MolesSessionMiddleware._migration
75       
76        request.moles_session = self._getMolesSession()
77
78
79    def process_response(self, request, response):
80        if hasattr(request, 'moles_session'):
81            request.moles_session.close()
82
83        return response
84
85
86    def process_exception(self, request, exception):
87        try:
88            session = request.moles_session           
89        except AttributeError:
90            return
91        session.rollback()
92        session.close()       
Note: See TracBrowser for help on using the repository browser.