source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/listatom.py @ 4348

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/listatom.py@4348
Revision 4348, 6.7 KB checked in by cbyrom, 11 years ago (diff)

Move vocabdata object into global variable to allow efficient re-use +
simplify some of the edit control logic + remove unused routings.

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'''
7from ows_server.lib.base import *
8from ows_server.models import Utilities
9from ndgUtils import ndgObject
10from paste.request import parse_querystring
11from ows_server.lib.ndgInterface import interface
12from xml.parsers.expat import ExpatError
13import logging
14from ndgUtils.models.Atom import Atom
15from formencode import Schema, validators, Invalid
16from genshi.filters import HTMLFormFiller
17from genshi import HTML
18from ndgUtils import DocumentRetrieve
19from ndgUtils.eXistInterface import ndg_eXist
20import ndgUtils.models.existdbclient as edc
21from ndgUtils.models.MolesEntity import MolesEntity as ME
22from ndgUtils.vocabtermdata import VocabTermData as VTD, VocabTermItem as VTI
23import ows_server.templates.htmlUtilities as utils
24from editatom import EditatomController as ec
25
26
27class ListatomController(BaseController):
28    '''
29    Provides the pylons controller for listing/searching NDG Atom documents.
30    '''
31    def __setup(self,uri=None):
32        ''' Common setup stuff for all the actions on this controller '''
33        logging.info("Setting up EditatomController")
34        self.cf=request.environ['ndgConfig']
35       
36        if uri:
37            try:
38                self.ndgObject = ndgObject(uri, config=self.cf.config)
39            except ValueError,e:
40                return e
41
42        self.inputs=dict(parse_querystring(request.environ))
43
44        logging.info("EditatomController set up")
45        return 0
46
47
48    def atomHome(self):
49        '''
50        Render a simple home page for the atom editor
51        '''
52        logging.info("Rendering atom home page")
53        c.title = "Atom Editor Home Page"
54        return render("genshi", "atom_home")
55
56   
57    def list(self, searchData=None, associatedAtomID=None, \
58             associatedAtomType=None, associationType=None):
59        '''
60        Provide a simple search interface to the atoms data
61        - display a filter + display any results + if an atomID is
62        provided, allow atoms to be linked to this
63        @keyword associatedAtomID: the ID of an atom to which other atoms
64        should be linked - via deployments
65        @keyword associatedAtomType: type of atom that the data is being
66        associated with
67        @keyword associationType: type of association to be done - NB, use
68        the constants in htmlUtilities for these
69        '''
70        try:
71            inputs = request.params
72            c.searchData = None
73            if searchData and int(searchData) > 0:
74                logging.info("Preparing atom search")
75                c.searchData = searchData
76                self.__setup()
77                atomTypeID = inputs.get('atomTypeID').split('--')[1]
78                providerID = inputs.get('providerID').split('--')[1]
79                c.results = self.searchAtoms(providerID, atomTypeID, inputs.get('title'))
80               
81            logging.info("Rendering atom listings page")
82            c.errors = {}
83            c.title='Browse atoms'
84            c.assocAtomType = associatedAtomType
85            c.associateLink = ""
86            if associatedAtomID:
87                c.associateLink = h.url_for(controller='editatom', action='saveAtom', \
88                    uri = associatedAtomID, saveLevel = ec.ADD_ASSOCIATIONS)
89            c.searchLink = h.url_for(controller = 'listatom', action='list', \
90                                     searchData='1', associatedAtomID = associatedAtomID)
91           
92            c.searchTerm = inputs.get('title')
93           
94            # set up the drop down content
95            # NB, if we are here whilst doing an association, restrict the filter
96            # appropriately
97            listVals = []
98            defaultVal = None
99            # set up a default 'all' value
100            allVal = VTI(ndg_eXist.DEFAULT_ALL_VAL, ndg_eXist.DEFAULT_ALL_VAL)
101            if associationType:
102                val = int(associationType)
103                if val == utils.GRANULE_ASSOCIATION:
104                    c.title += ' - to create associations with granule data'
105                    listVals = [g.vtd.TERM_DATA[g.vtd.GRANULE_TERM]]
106                elif val == utils.DEPLOYMENT_ASSOCIATION:
107                    c.title += ' - to create associations with deployment data'
108                    listVals = [g.vtd.TERM_DATA[g.vtd.ACTIVITY_TERM]]
109                elif val == utils.ENTITY_ASSOCIATION:
110                    c.title += ' - to create associations with activity/data production tool/observation station data'
111                    listVals = g.vtd.getValidTypes(g.vtd.DEPLOYABLE_ATOM_CATEGORY)
112                    defaultVal = allVal
113            else:
114                listVals = g.vtd.getValidTypes(g.vtd.ATOM_CATEGORY)
115                defaultVal = allVal
116           
117            c.atomTypes = utils.getVocabTermDataDropdown(listVals, \
118                                                   defaultVal = defaultVal, \
119                                                   selected = inputs.get('atomTypeID'))
120            c.providerIDs = utils.getVocabTermDataDropdown(g.vtd.getValidTypes(g.vtd.PROVIDER_CATEGORY), 
121                                                     defaultVal = allVal, \
122                                                     selected = inputs.get('providerID'))
123
124            return render("genshi", 'atom_list')
125
126        except Exception, e:
127            #we may be showing an xml document ... but it could go wrong if
128            #we have crap content ...
129            c.xml='Unexpected error loading page [%s]' %str(e)
130            c.doc=''
131            logging.error(c.xml)
132       
133        response.status_code = 400
134        return render('error')
135   
136   
137    def searchAtoms(self, providerID, atomTypeID, term):
138        '''
139        Search for atoms with specified filter info
140        @param providerID: provider ID of atom or 'all' for all
141        @param atomTypeID: type of atom or 'all' for all
142        @param term: text to search for in atom
143        @return array of dicts with info for each doc found in search
144        '''
145        logging.info("Search for atoms with the following filter: \
146            \nProviderID: '%s'\nAtom Type: '%s'\nText in title: '%s'" \
147            %(providerID, atomTypeID, term))
148       
149        dr = DocumentRetrieve(self.cf.get('NDG_EXIST','local'),
150                              self.cf.get('NDG_EXIST','passwordFile'))
151   
152        results = dr.search(term,
153                            start=1,
154                            howmany=10000, # NB, we want to get back everything!
155                            target=ndg_eXist.ATOM_TARGET,
156                            providerID = providerID, 
157                            atomTypeID = atomTypeID)
158
159        logging.info("Search completed")
160        return results
161
162           
163       
Note: See TracBrowser for help on using the repository browser.