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

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

Add delete functionality to front tier of oai editor. Include
message to confirm the data has been completed, as a pop up, when
the operation is done. Update tests and simplify model + dal layer.

  • 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 createChangeRequest(self, vocabListURL):
19        '''
20        Create change request for a specified list and change type.
21        @param vocabListURL: url of the list which holds the term
22        '''
23        logging.info("Creating new change request")
24        self._setUpController(vocabListURL)
25        self.__getInputs()
26        logging.info("- of type, %s" %self.changeType)
27
28        # if we're creating a change request for an update, get the original
29        # record to include in this request, too
30        if self.changeType.typeFlag & ct.UPDATE_TERM_FLAG:
31            record = c.vocabList.recordType().getRecordByID(self.recordID, c.vocabList)
32            self.records.insert(0, record)
33
34        cr = createChangeRequestWithData(self.records, vocabListURL, 
35                                         self.proposedDate, self.proposedBy, 
36                                         self.changeType, self.comment, 
37                                         self.mailThreadTitle, self.mailThreadUrl,
38                                         self.status, oldRecord = self.oldRecord)
39       
40        g.dao.createChangeRequest(cr)
41        logging.info("- change request created")
42        h.redirect_to('viewCurrentStatus', vocabListURL = vocabListURL)
43
44
45    def updateChangeRequest(self, vocabListURL, id):
46        '''
47        Update an existing change request for a specified list and term. 
48        @param vocabListURL: url of the list which the change request relates to
49        @param id: ID of change request to update
50        '''
51        logging.info("Updating change request")
52        self._setUpController(vocabListURL)
53        self.__getInputs()
54
55        cr = createChangeRequestWithData(self.records, self.vocabListURL, 
56                                         self.proposedDate, self.proposedBy, 
57                                         self.changeType, self.comment, 
58                                         self.mailThreadTitle, self.mailThreadUrl,
59                                         self.status, dbID = id,
60                                         version = self.version,
61                                         createdDate = self.createdDate,
62                                         oldRecord = self.oldRecord)
63       
64        # update to ensure that the records are returned
65        cr = g.dao.updateChangeRequest(cr)
66
67        # check if any workflow needs to be instigated due to status changes
68        if self.status == str(st.SUBMITTED):
69            resultSummary = self.submitChangeRequest(cr)
70
71            # save the summary, so it can be displayed in a pop up on the web page
72            session[self.SUBMITTED_MESSAGE_SESSION_KEY] = resultSummary
73            session.save()
74       
75        logging.info("- change request updated")
76        h.redirect_to('viewCurrentStatus', vocabListURL = vocabListURL)
77   
78
79    def __getInputs(self):
80        '''
81        Retrieve common inputs when dealing with provider info and set these
82        on the current object
83        '''
84        logging.debug("Processing inputs")
85
86        self.providerName = self.inputs.get(PI.NAME_ATT_NAME)
87       
88        i = 0
89        self.repositoryInfos = []
90        while self.inputs.has_key(str(i) + RI_DELIMITER + NAME_ELEMENT_NAME):
91            self.repositoryInfos.append(self.__getInputResositoryInfo(str(i) + RI_DELIMITER))
92            i += 1
93           
94        logging.debug("- all inputs processed")
95
96
97    def __getInputResositoryInfo(self, prefix):
98        '''
99        Retrieve inputs relating to repository info records - and use these create
100        local versions
101        @keyword prefix: prefix to prepend to the input keys to look for - used when a
102        specific data set is required - e.g. each provider can have several repository
103        info data sets - and these usually have a numerical index to distinguish between
104        them
105        @raise ValueError: if prefix doesn't have any associated data
106        @return ProviderInfo with data relating to the input prefix
107        '''
108        logging.debug("Getting provider info data (index = %s)" %prefix)
109       
110        inputs = {}
111        for att in RI.__slots__:
112            val = self.inputs.get(prefix + att)
113            if val:
114                inputs[att] = val
115
116        if not inputs:
117            raise ValueError("- could not find any input data for specified prefix")
118
119        logging.debug(" - input data retrieved - now using this to set up RepositoryInfo object")
120        return RI(**inputs)
121
122       
123    def editProviderInfo(self, providerName):
124        '''
125        Edit the provider info for the specified provider name
126        @param providerName: name of the provider whose data is to be edited
127        '''
128        logging.info("Setting up oai info editor edit page")
129        self._setUpController(providerName = providerName)
130
131        c.saveLink = h.url_for('saveProviderInfo', providerName = providerName)
132        c.deleteLink = h.url_for('deleteProviderInfo', providerName = providerName)
133       
134        c.title = const.EDIT_PAGE_TITLE %providerName
135        return self._renderTemplate('genshi', 'edit_provider_infos')
136
137       
138    def deleteProviderInfo(self, providerName):
139        '''
140        Delete the provider info for the specified provider name
141        @param providerName: name of the provider whose data is to be deleted
142        '''
143        logging.debug("Deleting provider info for provider, '%s'" %providerName)
144        self._setUpController(providerName = providerName)
145
146        pi = g.dao.deleteProviderInfo(providerName)
147
148        session[const.POP_UP_MESSAGE_SESSION_KEY] = "Data for provider with name, %s, successfully deleted" %providerName
149        session.save()
150
151        # now, return to the view page
152        h.redirect_to('home')
153
154       
155    def saveProviderInfo(self, providerName):
156        '''
157        Save the provider info for the specified provider name
158        @param providerName: name of the provider whose data is to be saved
159        '''
160        logging.debug("Saving provider info for provider, '%s'" %providerName)
161        self._setUpController(providerName = providerName)
162        self.__getInputs()
163
164        pi = PI.createProviderInfoWithData(self.providerName, self.repositoryInfos)
165        pi.oldName = providerName
166           
167        # update to ensure that the records are returned
168        pi = g.dao.updateProviderInfo(pi)
169
170        # now, return to the view page
171        h.redirect_to('viewProviderInfo', providerName = pi.name)
Note: See TracBrowser for help on using the repository browser.