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

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

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
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
14
15
16class MolesSessionMiddleware(object):
17   
18    #This attribute should be not here.
19    #unfortunately I cannot find any start/stop signals from Django
20    _migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
21    _epbInitialized = False
22   
23
24    def _initSearchIndexes(self, db_manager):
25        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS!
26        # We don't want sqlalchemy to know about this column so we add it externally.
27        try:
28            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                 
29
30            # This indexes the tsvector column
31
32            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")
33
34            # This sets up the trigger that keeps the tsvector column up to date.
35            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \
36                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")                       
37        except Exception as e:
38            pass
39
40    def _getNewMolesSession(self):
41        session = Moles3EPB.getNewMolesSession() 
42        MOLES3_DB_SCRIPT.attachEvents(session)
43        return session
44   
45    def _getMolesSession(self):
46        if not MolesSessionMiddleware._epbInitialized:
47            migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
48            MigrationEPB.overrrideDBManager(migrationDB)       
49
50            molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
51            Moles3EPB.overrrideDBManager(molesDB)
52            MolesSessionMiddleware._epbInitialized = True
53           
54            self._initSearchIndexes(molesDB)
55
56        return self._getNewMolesSession()
57   
58    def _migration(self, runMigration = RUN_MIGRATION):
59        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
60            #t.setDaemon(False)
61            MolesSessionMiddleware._migrationThread.start()
62           
63    """
64        Represents the access to the Moles database.
65        Creates from the existing db connections pools a new session
66        to be used by all the DB operations involved in the actual HTTPRequest
67    """ 
68   
69    def process_request(self, request):       
70        self._migration() #see the note on  MolesSessionMiddleware._migration
71       
72        request.moles_session = self._getMolesSession()
73
74
75    def process_response(self, request, response):
76        if hasattr(request, 'moles_session'):
77            request.moles_session.close()
78
79        return response
80
81
82    def process_exception(self, request, exception):
83        try:
84            session = request.moles_session           
85        except AttributeError:
86            return
87        session.rollback()
88        session.close()       
Note: See TracBrowser for help on using the repository browser.