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

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

Add new field to provider info - email - to allow mailing of results
of document ingest results - since this needs to be done asynchronously

  • since it can take a considerable amount of time. Adjust controllers

and templates for this new field and add new tests.

  • 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
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        c.harvestLink = ''
30       
31        c.title = const.NEW_PAGE_TITLE
32        return self._renderTemplate('genshi', 'edit_provider_infos')
33   
34
35    def __getInputs(self):
36        '''
37        Retrieve common inputs when dealing with provider info and set these
38        on the current object
39        '''
40        logging.debug("Processing inputs")
41
42        self.providerName = urllib.unquote(self.inputs.get(PI.NAME_ATT_NAME))
43        self.providerEmail = urllib.unquote(self.inputs.get(PI.EMAIL_ADDRESS_NAME))
44        # NB, we always need the provider name specified - this is effectively the
45        # primary key for the data
46        c.errors = {}
47        if not self.providerName:
48            c.errors[PI.NAME_ATT_NAME] = 'Please enter a value'
49       
50        i = 0
51        self.repositoryInfos = []
52        while self.inputs.has_key(str(i) + RI_DELIMITER + NAME_ELEMENT_NAME):
53            ri = self.__getInputResositoryInfo(str(i) + RI_DELIMITER)
54            if not ri.isEmpty():
55                self.repositoryInfos.append(ri)
56            else:
57                # remove any errors associated with empty records
58                for key in c.errors.keys():
59                    if key.startswith(str(i)):
60                        del c.errors[key]
61            i += 1
62           
63        logging.debug("- all inputs processed")
64
65
66    def __getInputResositoryInfo(self, prefix):
67        '''
68        Retrieve inputs relating to repository info records - and use these create
69        local versions
70        @keyword prefix: prefix to prepend to the input keys to look for - used when a
71        specific data set is required - e.g. each provider can have several repository
72        info data sets - and these usually have a numerical index to distinguish between
73        them
74        @raise ValueError: if prefix doesn't have any associated data
75        @return ProviderInfo with data relating to the input prefix
76        '''
77        logging.debug("Getting provider info data (index = %s)" %prefix)
78       
79        inputs = {}
80        for att in RI.__slots__:
81            val = self.inputs.get(prefix + att)
82            if val:
83                inputs[att] = val
84            elif att == NAME_ELEMENT_NAME or att == URL_ELEMENT_NAME:
85                c.errors[prefix + att] = 'Please enter a value'
86
87        if not inputs:
88            raise ValueError("- could not find any input data for specified prefix")
89
90        logging.debug(" - input data retrieved - now using this to set up RepositoryInfo object")
91        return RI(**inputs)
92
93       
94    def editProviderInfo(self, providerName):
95        '''
96        Edit the provider info for the specified provider name
97        @param providerName: name of the provider whose data is to be edited
98        '''
99        logging.debug("Setting up oai info editor edit page")
100        unquotedName = urllib.unquote(providerName)
101        self._setUpController(providerName = unquotedName)
102
103        c.saveLink = h.url_for('saveProviderInfo', providerName = urllib.quote(providerName))
104        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName)
105       
106        c.title = const.EDIT_PAGE_TITLE %unquotedName
107        return self._renderTemplate('genshi', 'edit_provider_infos', **self.inputs)
108
109       
110    def deleteProviderInfo(self, providerName):
111        '''
112        Delete the provider info for the specified provider name
113        @param providerName: name of the provider whose data is to be deleted
114        '''
115        logging.debug("Deleting provider info for provider, '%s'" %providerName)
116        providerName = urllib.unquote(providerName)
117        self._setUpController(providerName = providerName)
118
119        pi = g.dao.deleteProviderInfo(providerName)
120
121        # prepare pop up message to display result
122        session[const.POP_UP_MESSAGE_SESSION_KEY] = "Data for provider with name, %s, successfully deleted" %providerName
123        session.save()
124
125        # now, return to the view page
126        h.redirect_to('home')
127
128       
129    def saveProviderInfo(self, providerName):
130        '''
131        Save the provider info for the specified provider name
132        @param providerName: name of the provider whose data is to be saved
133        '''
134        logging.debug("Saving provider info for provider, '%s'" %providerName)
135        providerName = urllib.unquote(providerName)
136        self._setUpController(providerName = providerName)
137        self.__getInputs()
138       
139        if c.errors:
140            return self.editProviderInfo(providerName)
141
142        pi = PI.createProviderInfoWithData(self.providerName, self.repositoryInfos, 
143                                           providerEmail = self.providerEmail)
144        pi.oldName = providerName
145
146        # now create or update the data
147        try:
148            if providerName == const.NEW_PROVIDER_INFO_NAME:
149                pi, fileName = g.dao.createProviderInfo(pi)
150            else:
151                # update to ensure that the records are returned
152                pi = g.dao.updateProviderInfo(pi)
153        except Exception, e:
154            c.errors = {'System error': e}
155            return render('genshi', 'error')
156
157        # now, return to the view page
158        h.redirect_to('viewProviderInfo', providerName = urllib.quote(pi.name))
Note: See TracBrowser for help on using the repository browser.