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

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

Add functionality to allow admin users to add new provider info +
restrict deletion of info to admin users + add basic input checking
to ensure provider name is specified as a minimum.

  • 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
12
13class MasterController(BaseController):
14
15           
16    def _setUpController(self, providerName = None):
17        '''
18        Set up anything that may be of use to various controllers
19        @keyword providerName: name of provider related to currently viewed data - used
20        to refined the providerInfos data available to the templates
21        '''
22        self.cf = request.environ['appConfig']
23       
24        # TODO: determine the ID of the current user
25        userID = 'calum'
26       
27        if not userID in g.users:
28            raise ValueError("Unrecognised user, '%s' - access to editor denied" %userID)
29       
30        c.user = g.users[userID]
31        c.providerInfos = g.dao.getProviderInfoForUser(c.user)
32        if providerName:
33            if providerName == constants.NEW_PROVIDER_INFO_NAME:
34                c.providerInfos = [PI.ProviderInfo()]
35            elif providerName != constants.ALL_PROVIDER_IDS:
36                pis = []
37                logging.debug("- refining provider info list to include only data from provider, '%s'"
38                              %providerName)
39                for pi in c.providerInfos:
40                    if pi.name == providerName:
41                        pis.append(pi)
42                c.providerInfos = pis
43           
44        # get any inputs POSTED from forms
45        self.inputs = dict(request.params)
46
47
48    def _renderTemplate(self, type, template, **inputs):
49        '''
50        Render templates in a consistent manner - to catch errors in the same way
51        - allowing these to be displayed better
52        @param type: type of template to use - e.g. 'kid' or 'genshi'
53        @param template: template to render
54        @param inputs: dict of inputs with keynames matching input names in template
55        form 
56        '''
57        logging.debug("Rendering '%s' style template, '%s'" %(type, template))
58        try:
59            html = render(type, template)
60            logging.debug("- rendering completed successfully - returning data")
61
62            # NB, need html in unicode for for htmlfill.render
63            html = unicode(html, 'utf-8', 'xmlcharrefreplace')
64
65            # NB, htmlfill has some peculiarities - e.g. it will override checkbox
66            # settings unless these are specified in inputs - so only use if really
67            # needed
68            if inputs or c.errors:
69                import pdb
70                pdb.set_trace()
71                for key, val in inputs.items():
72                    inputs[key] = str(val)
73       
74                return htmlfill.render(html, inputs, errors = c.errors)
75            else:
76                return html
77       
78        except Exception, e:
79            logging.error("Problem occured whilst rendering template: %s" %e)
80            c.errors = {'Rendering error': e}
81            return render('genshi', 'error')
Note: See TracBrowser for help on using the repository browser.