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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py@8206
Revision 8206, 3.3 KB checked in by mnagni, 8 years ago (diff)
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            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")
32
33            # This sets up the trigger that keeps the tsvector column up to date.
34            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \
35                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")                       
36        except Exception as e:
37            pass
38
39
40
41   
42    def _getMolesSession(self):
43        if not MolesSessionMiddleware._epbInitialized:
44            migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
45            MigrationEPB.overrrideDBManager(migrationDB)       
46
47            molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
48            Moles3EPB.overrrideDBManager(molesDB)
49            MolesSessionMiddleware._epbInitialized = True
50           
51            self._initSearchIndexes(molesDB)
52           
53            return Moles3EPB.getNewMolesSession()
54        else:
55            return Moles3EPB.getNewMolesSession()
56   
57    def _migration(self, runMigration = RUN_MIGRATION):
58        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
59            #t.setDaemon(False)
60            MolesSessionMiddleware._migrationThread.start()
61           
62    """
63        Represents the access to the Moles database.
64        Creates from the existing db connections pools a new session
65        to be used by all the DB operations involved in the actual HTTPRequest
66    """ 
67   
68    def process_request(self, request):       
69        self._migration() #see the note on  MolesSessionMiddleware._migration
70       
71        request.moles_session = self._getMolesSession()
72
73
74    def process_response(self, request, response):
75        if hasattr(request, 'moles_session'):
76            request.moles_session.close()
77
78        return response
79
80
81    def process_exception(self, request, exception):
82        try:
83            session = request.moles_session           
84        except AttributeError:
85            return
86        session.rollback()
87        session.close()       
Note: See TracBrowser for help on using the repository browser.