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

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

Add new controller, feeds, to centralise all feed creation code. Add
new template, feed_creator, to allow input form to directly create
atom feed entry. Add general feed to discovery search page - tieing
this to the feed created by feed_creator. Add icons for feed and
include these in the config set up.

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