source: TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/controllers/edit.py @ 5466

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

Add to code to allow the OAI editor to be used with the new openid
based security system. Add the policy file and secured ini file +
adjust handling of users - enforcing page access via URL content

  • basically either by provider ID or via 'admin=1' parameter for

admin users. For the latter, provider security_redirector module to
allow admin users to use same point of entry as normal users.

  • Property svn:executable set to *
Line 
1'''
2 Class representing pylons controller for the input and edit of provider and
3 repository info
4 
5 @author: C Byrom, Tessella Feb 2009
6'''
7import logging, urllib, os
8from oai_info_editor.lib.base import *
9from oai_info_editor.controllers.master import MasterController
10import oai_info_editor.lib.constants as const
11import oai_info_editor.model.formattype as format
12from oai_info_editor.model.repositoryinfo import RepositoryInfo as RI, \
13    RI_DELIMITER, NAME_ELEMENT_NAME, SPLIT_BY_SET_NAME, URL_ELEMENT_NAME
14import oai_info_editor.model.providerinfo as PI
15from oai_info_editor.controllers.master import ADMIN_PARAMETER
16 
17class EditController(MasterController):
18
19    def createProviderInfo(self):
20        '''
21        Create provider info data.  NB, only admin users should be able to
22        access this method.
23        '''
24        logging.debug("Setting up oai info editor create page")
25       
26        providerName = const.NEW_PROVIDER_INFO_NAME
27        self._setUpController(providerName = providerName)
28
29        c.saveLink = h.url_for('saveProviderInfo', providerName = providerName) + ADMIN_PARAMETER
30        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName) + ADMIN_PARAMETER
31        c.harvestLink = ''
32       
33        c.title = const.NEW_PAGE_TITLE
34        return self._renderTemplate('genshi', 'edit_provider_infos')
35   
36
37    def __getInputs(self):
38        '''
39        Retrieve common inputs when dealing with provider info and set these
40        on the current object
41        '''
42        logging.debug("Processing inputs")
43
44        self.providerName = urllib.unquote(self.inputs.get(PI.NAME_ATT_NAME))
45        self.providerEmail = urllib.unquote(self.inputs.get(PI.EMAIL_ADDRESS_NAME))
46        # NB, we always need the provider name specified - this is effectively the
47        # primary key for the data
48        c.errors = {}
49        if not self.providerName:
50            c.errors[PI.NAME_ATT_NAME] = 'Please enter a value'
51       
52        i = 0
53        self.repositoryInfos = []
54        while self.inputs.has_key(str(i) + RI_DELIMITER + NAME_ELEMENT_NAME):
55            ri = self.__getInputResositoryInfo(str(i) + RI_DELIMITER)
56            if not ri.isEmpty():
57                self.repositoryInfos.append(ri)
58            else:
59                # remove any errors associated with empty records
60                for key in c.errors.keys():
61                    if key.startswith(str(i)):
62                        del c.errors[key]
63            i += 1
64           
65        logging.debug("- all inputs processed")
66
67
68    def __getInputResositoryInfo(self, prefix):
69        '''
70        Retrieve inputs relating to repository info records - and use these create
71        local versions
72        @keyword prefix: prefix to prepend to the input keys to look for - used when a
73        specific data set is required - e.g. each provider can have several repository
74        info data sets - and these usually have a numerical index to distinguish between
75        them
76        @raise ValueError: if prefix doesn't have any associated data
77        @return ProviderInfo with data relating to the input prefix
78        '''
79        logging.debug("Getting provider info data (index = %s)" %prefix)
80       
81        inputs = {}
82        for att in RI.__slots__:
83            val = self.inputs.get(prefix + att)
84            if val:
85                inputs[att] = val
86            elif att == NAME_ELEMENT_NAME or att == URL_ELEMENT_NAME:
87                c.errors[prefix + att] = 'Please enter a value'
88
89        if not inputs:
90            raise ValueError("- could not find any input data for specified prefix")
91
92        logging.debug(" - input data retrieved - now using this to set up RepositoryInfo object")
93        return RI(**inputs)
94
95       
96    def editProviderInfo(self, providerName):
97        '''
98        Edit the provider info for the specified provider name
99        @param providerName: name of the provider whose data is to be edited
100        '''
101        logging.debug("Setting up oai info editor edit page")
102        unquotedName = urllib.unquote(providerName)
103        self._setUpController(providerName = unquotedName)
104
105        c.saveLink = h.url_for('saveProviderInfo', providerName = urllib.quote(providerName))
106        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName)
107
108        if c.user.isAdmin:
109            c.saveLink += ADMIN_PARAMETER
110           
111        c.title = const.EDIT_PAGE_TITLE %unquotedName
112        return self._renderTemplate('genshi', 'edit_provider_infos', **self.inputs)
113
114       
115    def deleteProviderInfo(self, providerName):
116        '''
117        Delete the provider info for the specified provider name.  NB, only
118        admin users should be able to access this method.
119        @param providerName: name of the provider whose data is to be deleted
120        '''
121        logging.debug("Deleting provider info for provider, '%s'" %providerName)
122        providerName = urllib.unquote(providerName)
123        self._setUpController(providerName = providerName)
124
125        pi = g.dao.deleteProviderInfo(providerName)
126
127        # prepare pop up message to display result
128        session[const.POP_UP_MESSAGE_SESSION_KEY] = "Data for provider with name, %s, successfully deleted" %providerName
129        session.save()
130
131        # now, return to the view page
132        h.redirect_to('home') + ADMIN_PARAMETER
133
134       
135    def saveProviderInfo(self, providerName):
136        '''
137        Save the provider info for the specified provider name
138        @param providerName: name of the provider whose data is to be saved
139        '''
140        logging.debug("Saving provider info for provider, '%s'" %providerName)
141        providerName = urllib.unquote(providerName)
142        self._setUpController(providerName = providerName)
143        self.__getInputs()
144       
145        if c.errors:
146            return self.editProviderInfo(providerName)
147
148        pi = PI.createProviderInfoWithData(self.providerName, self.repositoryInfos, 
149                                           providerEmail = self.providerEmail)
150        pi.oldName = providerName
151
152        # now create or update the data
153        try:
154            if providerName == const.NEW_PROVIDER_INFO_NAME:
155                pi, fileName = g.dao.createProviderInfo(pi)
156            else:
157                # update to ensure that the records are returned
158                pi = g.dao.updateProviderInfo(pi)
159        except Exception, e:
160            c.errors = {'System error': e}
161            return render('genshi', 'error')
162
163        # now, return to the view page
164        viewLink = h.url_for('viewProviderInfo', providerName = urllib.quote(pi.name))
165        if c.user.isAdmin:
166            viewLink += ADMIN_PARAMETER
167        h.redirect_to(viewLink)
Note: See TracBrowser for help on using the repository browser.