source: TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/controllers/master.py @ 5631

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/controllers/master.py@5631
Revision 5631, 4.2 KB checked in by cbyrom, 11 years ago (diff)

Simplify and tidy up the configuration of the OAI Info editor +
remove some unecessary debugging code elsewhere in the controllers.

  • Property svn:executable set to *
Line 
1'''
2 Class representing the top level pylons controller with functionality shared
3 across the different controllers - which should inherit this one
4 
5 @author: C Byrom, Tessella Apr 2009
6'''
7import logging, urllib, datetime, re
8from formencode import htmlfill
9from oai_info_editor.lib.base import *
10import oai_info_editor.lib.constants as constants
11import oai_info_editor.model.providerinfo as PI
12from oai_info_editor.model.user import User
13
14ADMIN_FLAG = 'admin'
15ADMIN_PARAMETER = "?%s=1" %ADMIN_FLAG
16BASE_SERVER_URL = "http://%s/logout"
17class MasterController(BaseController):
18
19           
20    def _setUpController(self, providerName = None):
21        '''
22        Set up anything that may be of use to various controllers
23        @keyword providerName: name of provider related to currently viewed data - used
24        to refined the providerInfos data available to the templates
25        '''
26        self.cf = request.environ['appConfig']
27       
28        c.user = self.__getUserDetails()
29       
30        c.logoutLink = BASE_SERVER_URL %request.environ['HTTP_HOST'] 
31        c.homeLink = h.url_for('home')
32        c.helpLink = h.url_for('help')
33
34        if c.user.isAdmin:
35            c.homeLink += ADMIN_PARAMETER
36            c.helpLink += ADMIN_PARAMETER
37       
38        c.providerInfos = g.dao.getProviderInfoForUser(c.user)
39        if providerName:
40            if providerName == constants.NEW_PROVIDER_INFO_NAME:
41                c.providerInfos = [PI.ProviderInfo()]
42            elif providerName != constants.ALL_PROVIDER_IDS:
43                pis = []
44                logging.debug("- refining provider info list to include only data from provider, '%s'"
45                              %providerName)
46                for pi in c.providerInfos:
47                    if pi.name == providerName:
48                        pis.append(pi)
49                c.providerInfos = pis
50           
51        # get any inputs POSTED from forms
52        self.inputs = dict(request.params)
53
54
55    def _renderTemplate(self, type, template, **inputs):
56        '''
57        Render templates in a consistent manner - to catch errors in the same way
58        - allowing these to be displayed better
59        @param type: type of template to use - e.g. 'kid' or 'genshi'
60        @param template: template to render
61        @param inputs: dict of inputs with keynames matching input names in template
62        form 
63        '''
64        logging.debug("Rendering '%s' style template, '%s'" %(type, template))
65        try:
66            html = render(type, template)
67            logging.debug("- rendering completed successfully - returning data")
68
69            # NB, need html in unicode for for htmlfill.render
70            html = unicode(html, 'utf-8', 'xmlcharrefreplace')
71
72            # NB, htmlfill has some peculiarities - e.g. it will override checkbox
73            # settings unless these are specified in inputs - so only use if really
74            # needed
75            # NB, the admin=1 parameter may end up here; delete, if so
76            if inputs.has_key(ADMIN_FLAG):
77                del inputs[ADMIN_FLAG]
78               
79            if inputs or c.errors:
80                for key, val in inputs.items():
81                    inputs[key] = str(val)
82
83                return htmlfill.render(html, inputs, errors = c.errors)
84            else:
85                return html
86       
87        except Exception, e:
88            logging.error("Problem occured whilst rendering template: %s" %e)
89            c.errors = {'Rendering error': e}
90            return render('genshi', 'error')
91
92
93    def __getUserDetails(self):
94        '''
95        Create User object from available data - this stores the user ID and
96        whether the user has admin rights or not.
97        @return:
98        '''
99        # NB, not always possibly or necessary to know the user ID
100        userID = request.path_info.split('/')[-1]
101        if userID in ["home", "help", "createProviderInfo"]:
102            userID = None
103           
104        return User(userID, ADMIN_FLAG in request.params and request.params[ADMIN_FLAG] == '1')
105       
106       
107    def _redirectSafely(self, url):
108        '''
109        Redirect ensuring that the path has been properly corrected for base url path
110        '''
111        if url.startswith(g.baseUrl):
112                        url = url.replace(g.baseUrl, '')
113                       
114        h.redirect_to(g.server + url)
Note: See TracBrowser for help on using the repository browser.