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

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

Add Harvester class to run the jOAI Harvester API and run repository
harvests. Add code throughout the app to allow harvests to be
ran from the UI. Add new test data and tests to exercise the new
functionality and adjust the config file to allow input of required
harvest data. Add confirmation pop up when running deletes.

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