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

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

Add functionality to add new repository info data to provider info
records + properly escape/unescape provider name when passing via url.

  • Property svn:executable set to *
Line 
1'''
2 Class representing pylons controller for the input and edit of work requests to
3 update/create new Vocab Terms
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
14import oai_info_editor.model.providerinfo as PI
15 
16class EditController(MasterController):
17
18    def createProviderInfo(self):
19        '''
20        Create provider info data.
21        '''
22        logging.debug("Setting up oai info editor create page")
23       
24        providerName = const.NEW_PROVIDER_INFO_NAME
25        self._setUpController(providerName = providerName)
26
27        c.saveLink = h.url_for('saveProviderInfo', providerName = providerName)
28        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName)
29       
30        c.title = const.NEW_PAGE_TITLE
31        return self._renderTemplate('genshi', 'edit_provider_infos')
32   
33
34    def __getInputs(self):
35        '''
36        Retrieve common inputs when dealing with provider info and set these
37        on the current object
38        '''
39        logging.debug("Processing inputs")
40
41        self.providerName = urllib.unquote(self.inputs.get(PI.NAME_ATT_NAME))
42       
43        # NB, we always need the provider name specified - this is effectively the
44        # primary key for the data
45        if not self.providerName:
46            c.errors = {'name': 'Please enter a value'}
47       
48        i = 0
49        self.repositoryInfos = []
50        while self.inputs.has_key(str(i) + RI_DELIMITER + NAME_ELEMENT_NAME):
51            self.repositoryInfos.append(self.__getInputResositoryInfo(str(i) + RI_DELIMITER))
52            i += 1
53           
54        logging.debug("- all inputs processed")
55
56
57    def __getInputResositoryInfo(self, prefix):
58        '''
59        Retrieve inputs relating to repository info records - and use these create
60        local versions
61        @keyword prefix: prefix to prepend to the input keys to look for - used when a
62        specific data set is required - e.g. each provider can have several repository
63        info data sets - and these usually have a numerical index to distinguish between
64        them
65        @raise ValueError: if prefix doesn't have any associated data
66        @return ProviderInfo with data relating to the input prefix
67        '''
68        logging.debug("Getting provider info data (index = %s)" %prefix)
69       
70        inputs = {}
71        for att in RI.__slots__:
72            val = self.inputs.get(prefix + att)
73            if val:
74                inputs[att] = val
75
76        if not inputs:
77            raise ValueError("- could not find any input data for specified prefix")
78
79        logging.debug(" - input data retrieved - now using this to set up RepositoryInfo object")
80        return RI(**inputs)
81
82       
83    def editProviderInfo(self, providerName):
84        '''
85        Edit the provider info for the specified provider name
86        @param providerName: name of the provider whose data is to be edited
87        '''
88        logging.debug("Setting up oai info editor edit page")
89        unquotedName = urllib.unquote(providerName)
90        self._setUpController(providerName = unquotedName)
91
92        c.saveLink = h.url_for('saveProviderInfo', providerName = urllib.quote(providerName))
93        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName)
94       
95        c.title = const.EDIT_PAGE_TITLE %unquotedName
96        return self._renderTemplate('genshi', 'edit_provider_infos', **self.inputs)
97
98       
99    def deleteProviderInfo(self, providerName):
100        '''
101        Delete the provider info for the specified provider name
102        @param providerName: name of the provider whose data is to be deleted
103        '''
104        logging.debug("Deleting provider info for provider, '%s'" %providerName)
105        providerName = urllib.unquote(providerName)
106        self._setUpController(providerName = providerName)
107
108        pi = g.dao.deleteProviderInfo(providerName)
109
110        session[const.POP_UP_MESSAGE_SESSION_KEY] = "Data for provider with name, %s, successfully deleted" %providerName
111        session.save()
112
113        # now, return to the view page
114        h.redirect_to('home')
115
116       
117    def saveProviderInfo(self, providerName):
118        '''
119        Save the provider info for the specified provider name
120        @param providerName: name of the provider whose data is to be saved
121        '''
122        logging.debug("Saving provider info for provider, '%s'" %providerName)
123        providerName = urllib.unquote(providerName)
124        self._setUpController(providerName = providerName)
125        self.__getInputs()
126       
127        if c.errors:
128            return self.editProviderInfo(providerName)
129
130        pi = PI.createProviderInfoWithData(self.providerName, self.repositoryInfos)
131        pi.oldName = providerName
132
133        # now create or update the data
134        if providerName == const.NEW_PROVIDER_INFO_NAME:
135            pi, fileName = g.dao.createProviderInfo(pi)
136        else:
137            # update to ensure that the records are returned
138            pi = g.dao.updateProviderInfo(pi)
139
140        # now, return to the view page
141        h.redirect_to('viewProviderInfo', providerName = urllib.quote(pi.name))
Note: See TracBrowser for help on using the repository browser.