source: MILK/trunk/milk_server/milk_server/controllers/atom_editor/listatom.py @ 4984

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/controllers/atom_editor/listatom.py@4984
Revision 4984, 7.6 KB checked in by cbyrom, 12 years ago (diff)

Add new trackback controller and implement methods to allow
authenticated users to post comments on metadata docs. These comments
are then emailed to the metadata maintainer and added to the
corrections feed. Control then returns to the metadata document - in
non edit form - and, in the case of moles atom docs, a pop up informs
the user that the operation has completed successfully. Also add the
required genshi templates and template methods to enable this new
functionality.

Line 
1'''
2 Class representing pylons controller for the listing and searching of atom
3 data
4 
5 @author: C Byrom, Tessella Sep 2008
6'''
7import logging, xmlrpclib
8from ndg.common.src.models.Atom import Atom
9from ndg.common.src.clients.xmldb.eXist.searchclient import SearchClient
10from ndg.common.src.models.vocabtermdata import VocabTermData as VTD, VocabTermItem as VTI
11from milk_server.lib.base import *
12from editorconstants import *
13import ndg.common.src.lib.htmlUtilities as utils
14from atomeditorcontroller import AtomEditorController
15   
16class ListatomController(AtomEditorController):
17    '''
18    Provides the pylons controller for listing/searching NDG Atom documents.
19    '''
20    def atomHome(self):
21        '''
22        Render a simple home page for the atom editor
23        '''
24        logging.info("Rendering atom home page")
25        c.title = ATOM_HOME_TITLE
26        return self.savePageAndRender("atom_editor/atom_home")
27
28
29    def showExampleGranulite(self):
30        '''
31        Show an example granulite file
32        '''
33        logging.info("Rendering example granulite file page")
34        c.title = EXAMPLE_GRANULITE_TITLE
35        return self.savePageAndRender("atom_editor/granulite_example")
36
37
38    def showAtomHelp(self):
39        '''
40        Show the help page
41        '''
42        logging.info("Rendering help page")
43        c.title = HELP_PAGE_TITLE
44        return self.savePageAndRender("atom_editor/atom_help")
45   
46   
47    def list(self, searchData=None, associatedAtomID=None, \
48             associatedAtomType=None, associationType=None):
49        '''
50        Provide a simple search interface to the atoms data
51        - display a filter + display any results + if an atomID is
52        provided, allow atoms to be linked to this
53        @keyword searchData: if '1' do a search, if '0' just display search filter
54        @keyword associatedAtomID: the ID of an atom to which other atoms
55        should be linked - via deployments
56        @keyword associatedAtomType: type of atom that the data is being
57        associated with
58        @keyword associationType: type of association to be done - NB, use
59        the constants in htmlUtilities for these
60        '''
61        try:
62            c.searchData = None
63            self._setup()
64            if searchData and int(searchData) > 0:
65                logging.info("Preparing atom search")
66                c.searchData = searchData
67                atomTypeID = self.inputs.get('atomTypeID')
68                providerID = self.inputs.get('providerID')
69                # NB, avoid page being reloaded in incorrect state - e.g. after
70                # browser reload
71                if not (atomTypeID and providerID):
72                    return self.list()
73                   
74                atomTypeID = atomTypeID.split('--')[1]
75                providerID = providerID.split('--')[1]
76               
77                # if we're dealing with activity deployments, substitute the typeID
78                # for the subType Deployments term
79                if atomTypeID == VTD.ACTIVITY_DEPLOYMENT_TERM:
80                    atomTypeID = VTD.DEPLOYMENT_TERM
81                   
82                c.results = self.searchAtoms(providerID, atomTypeID, self.inputs.get('title'))
83               
84            logging.info("Rendering atom listings page")
85            c.errors = {}
86            c.title = LIST_ATOM_TITLE
87            c.assocAtomType = associatedAtomType
88            c.associateLink = ""
89            if associatedAtomID:
90                c.associateLink = h.url_for('save', uri = associatedAtomID, \
91                                            saveLevel = self.ADD_ASSOCIATIONS)
92            c.searchLink = h.url_for('list', searchData='1', 
93                                     associatedAtomID = associatedAtomID, \
94                                     associatedAtomType = associatedAtomType, \
95                                     associationType = associationType)
96           
97            # set up the drop down content
98            # NB, if we are here whilst doing an association, restrict the filter
99            # appropriately
100            listVals = []
101            defaultVal = None
102            # set up a default 'all' value
103            allVal = VTI(SearchClient.DEFAULT_ALL_VAL, SearchClient.DEFAULT_ALL_VAL)
104            if associationType:
105                val = int(associationType)
106                if val == utils.GRANULE_ASSOCIATION:
107                    c.title += GRANULE_ASSOCIATION_TITLE
108                    listVals = [g.vtd.TERM_DATA[g.vtd.GRANULE_TERM]]
109                elif val == utils.DEPLOYMENT_ASSOCIATION:
110                    c.title += DEPLOYMENTS_ASSOCIATION_TITLE
111                    listVals = [g.vtd.TERM_DATA[g.vtd.DEPLOYMENT_TERM]]
112                elif val == utils.ENTITY_ASSOCIATION:
113                    c.title += DEPLOYMENTS_DATA_ASSOCIATION_TITLE
114                    listVals = g.vtd.getValidTypes(g.vtd.DEPLOYABLE_ATOM_CATEGORY)
115                    defaultVal = allVal
116            else:
117                listVals = g.vtd.getValidTypes(g.vtd.ATOM_CATEGORY)
118                defaultVal = allVal
119           
120            c.atomTypes = utils.getVocabTermDataDropdown(listVals, \
121                                                   defaultVal = defaultVal, \
122                                                   selected = self.inputs.get('atomTypeID'))
123            c.providerIDs = utils.getVocabTermDataDropdown(g.vtd.getValidTypes(g.vtd.PROVIDER_CATEGORY), 
124                                                     defaultVal = allVal, \
125                                                     selected = self.inputs.get('providerID'))
126
127            return self.savePageAndRender('atom_editor/atom_list', **self.inputs)
128
129        except Exception, e:
130            c.xml='Unexpected error loading page [%s]' %str(e)
131            c.doc=''
132            logging.error(c.xml)
133       
134        response.status_code = 400
135        return render("genshi", 'atom_editor/error')
136   
137   
138    def searchAtoms(self, providerID, atomTypeID, term):
139        '''
140        Search for atoms with specified filter info
141        @param providerID: provider ID of atom or 'all' for all
142        @param atomTypeID: type of atom or 'all' for all
143        @param term: text to search for in atom
144        @return array of dicts with info for each doc found in search
145        '''
146        logging.info("Search for atoms with the following filter: \
147            \nProviderID: '%s'\nAtom Type: '%s'\nText in title: '%s'" \
148            %(providerID, atomTypeID, term))
149       
150        searchClient = SearchClient(dbHostName = self.cf.get('NDG_EXIST','local'),
151                                    configFileName = self.cf.get('NDG_EXIST','passwordFile'))
152       
153        # NB, for some reason, the xmlrpc call to eXist sometimes results in a spurious
154        # 'java.lang.IndexOutOfBoundsException' - retry a couple of times, if so
155        attempt = 0
156        results = None
157        while results is None:
158            try:
159                results = searchClient.search(
160                            term,
161                            start=1,
162                            howmany=10000, # NB, we want to get back everything!
163                            target = SearchClient.ATOM_TARGET,
164                            providerID = providerID, 
165                            atomTypeID = atomTypeID,
166                            useChunked = True)
167            except xmlrpclib.Fault, e:
168                logging.info("Error caught whilst calling eXist: %s" %e.faultString)
169                attempt += 1
170                if attempt < 3:
171                    logging.info("- retrying eXist call")
172                else:
173                    raise e
174
175        logging.info("Search completed")
176        return results
177
178           
179       
Note: See TracBrowser for help on using the repository browser.