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

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

Complete - # 22489: CEDA Observation Collection - phenomenonTime
 http://team.ceda.ac.uk/trac/ceda/ticket/22489
Incomplete - # 22531: CedaMoles3 MO_Party
 http://team.ceda.ac.uk/trac/ceda/ticket/22531

Many more unitests
Better structure for database operation (see MolesManager?.db.after_flux and moles3epb)
Documented many method but not updated docs folder

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 9 Jan 2012
30
31@author: Maurizio Nagni
32'''
33from MolesManager.moles3epb import Moles3EPBFactory
34from libs.migration.client import MigrationThread
35from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
36    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
37    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
38from libs.commons_db import DbManager
39from libs.migration.InfodbEPB import InfodbEPBFactory
40from libs.migration.MigrationEPB import MigrationEPBFactory
41from sqlalchemy import exc
42from django.core.context_processors import csrf
43from django.shortcuts import render_to_response
44
45
46
47class MolesSessionMiddleware(object):
48   
49    #This attribute should be not here.
50    #unfortunately I cannot find any start/stop signals from Django
51    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
52    _epbInitialized = False
53    _infodbEPBFactory = None   
54    _moles3EPBFactory = None
55    _migrationEPBFactory = None
56   
57    def _doInitialization(self):
58
59        infoDB = DbManager(INFO_DB_CONNECTION)
60        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
61           
62        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
63        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
64
65        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
66        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
67       
68        MolesSessionMiddleware._migrationThread = MigrationThread( \
69            MolesSessionMiddleware._moles3EPBFactory.createEPB(), \
70            MolesSessionMiddleware._migrationEPBFactory.createEPB(), \
71            MolesSessionMiddleware._infodbEPBFactory.createEPB(), \
72            interval = MIGRATION_INTERVAL )
73        MolesSessionMiddleware._epbInitialized = True     
74           
75        _migration()
76   
77    def process_request(self, request): 
78        try:
79            if not MolesSessionMiddleware._epbInitialized:
80                self._doInitialization()
81                 
82            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()                   
83        except exc.DBAPIError, e:
84            # an exception is raised, Connection is invalidated.
85            #if e.connection_invalidated:
86            #    print "Connection was invalidated!"
87            return self.process_exception(request, e)
88
89    def process_response(self, request, response):
90        if hasattr(request, "moles3EPB"):           
91            request.moles3EPB.close()
92           
93        return response
94
95    def process_exception(self, request, exception):
96        c = {}
97        c['exception'] = exception
98        c.update(csrf(request))
99        return render_to_response('errorTemplate.html', c)
100   
101def _migration(runMigration = RUN_MIGRATION):
102    if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
103        #t.setDaemon(False)
104        MolesSessionMiddleware._migrationThread.start()       
Note: See TracBrowser for help on using the repository browser.