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

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

Complete - # 22416: CEDA MOLES3 - search function - by title keyword
 http://team.ceda.ac.uk/trac/ceda/ticket/22416

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        except Exception as e:
33            pass
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
39   
40    def _getMolesSession(self):
41        if not MolesSessionMiddleware._epbInitialized:
42            migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
43            MigrationEPB.overrrideDBManager(migrationDB)       
44
45            molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
46            Moles3EPB.overrrideDBManager(molesDB)
47            MolesSessionMiddleware._epbInitialized = True
48           
49            self._initSearchIndexes(molesDB)
50           
51            return Moles3EPB.getNewMolesSession()
52        else:
53            return Moles3EPB.getNewMolesSession()
54   
55    def _migration(self, runMigration = RUN_MIGRATION):
56        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
57            #t.setDaemon(False)
58            MolesSessionMiddleware._migrationThread.start()
59           
60    """
61        Represents the access to the Moles database.
62        Creates from the existing db connections pools a new session
63        to be used by all the DB operations involved in the actual HTTPRequest
64    """ 
65   
66    def process_request(self, request):       
67        self._migration() #see the note on  MolesSessionMiddleware._migration
68       
69        request.moles_session = self._getMolesSession()
70
71
72    def process_response(self, request, response):
73        if hasattr(request, 'moles_session'):
74            request.moles_session.close()
75
76        return response
77
78
79    def process_exception(self, request, exception):
80        try:
81            session = request.moles_session           
82        except AttributeError:
83            return
84        session.rollback()
85        session.close()       
Note: See TracBrowser for help on using the repository browser.