source: mauRepo/HPFos/trunk/hpfos/HPFos/molesSessionMiddleware.py @ 8515

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

Incomplete - # 22534: Add versiojn number to the gui page
 http://team.ceda.ac.uk/trac/ceda/ticket/22534
Corrects import mistakes

  • Property svn:mime-type set to text/plain
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 hpfos.libs.commons_db import DbManager
34from hpfos.HPFos.settings import MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT
35from django.core.context_processors import csrf
36from django.shortcuts import render_to_response
37from sqlalchemy.exc import SQLAlchemyError
38from hpfos.HPFos.moles3epb import Moles3EPBFactory
39
40class MolesSessionMiddleware(object):
41   
42    #This attribute should be not here.
43    #unfortunately I cannot find any start/stop signals from Django
44    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
45    _moles3EPBFactory = None
46    _epbInitialized = False
47   
48    def _doInitialization(self):
49        """
50            Represents the access to the Moles database.
51            Creates from the existing db connections pools a new session
52            to be used by all the DB operations involved in the actual HTTPRequest
53        """                   
54        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
55        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
56        MolesSessionMiddleware._epbInitialized = True
57   
58    def process_request(self, request):
59        try: 
60            if not MolesSessionMiddleware._epbInitialized:
61                self._doInitialization()
62            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()
63        except Exception as e:           
64            return self.process_exception(request, e)
65
66    def process_response(self, request, response):
67        if hasattr(request, 'moles3EPB'):
68            request.moles3EPB.close()
69        return response
70
71
72    def process_exception(self, request, exception):
73        if isinstance(exception, SQLAlchemyError):
74            MolesSessionMiddleware._epbInitialized = False
75        c = {}
76        c['exception'] = exception
77        c.update(csrf(request))
78        return render_to_response('exceptionResponseTemplate', c)
Note: See TracBrowser for help on using the repository browser.