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

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

Add new template and controller code for adding and editing parameters +
fix data entities so they can properly associate granules and deployments with
them + add new form module to handle all the form validations and implement
basics for the create atom form.

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            if not c.vtd:
72                c.vtd = VTD()
73   
74            inputs = request.params
75            c.searchData = None
76            if searchData and int(searchData) > 0:
77                logging.info("Preparing atom search")
78                c.searchData = searchData
79                self.__setup()
80                atomTypeID = inputs.get('atomTypeID').split('--')[1]
81                providerID = inputs.get('providerID').split('--')[1]
82                c.results = self.searchAtoms(providerID, atomTypeID, inputs.get('title'))
83               
84            logging.info("Rendering atom listings page")
85            c.errors = {}
86            c.title='Browse atoms'
87            c.assocAtomType = associatedAtomType
88            c.associateLink = ""
89            if associatedAtomID:
90                c.associateLink = h.url_for(controller='editatom', action='saveAtom', \
91                    saveLevel = ec.ADD_ASSOCIATIONS, uri = associatedAtomID)
92            c.searchLink = h.url_for(controller = 'listatom', action='list', \
93                                     searchData='1', associatedAtomID = associatedAtomID)
94           
95            c.searchTerm = inputs.get('title')
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(ndg_eXist.DEFAULT_ALL_VAL, ndg_eXist.DEFAULT_ALL_VAL)
104            if associationType:
105                val = int(associationType)
106                if val == utils.GRANULE_ASSOCIATION:
107                    c.title += ' - to create associations with granule data'
108                    listVals = [c.vtd.TERM_DATA[c.vtd.GRANULE_TERM]]
109                elif val == utils.DEPLOYMENT_ASSOCIATION:
110                    c.title += ' - to create associations with deployment data'
111                    listVals = [c.vtd.TERM_DATA[c.vtd.ACTIVITY_TERM]]
112                elif val == utils.ENTITY_ASSOCIATION:
113                    c.title += ' - to create associations with activity/data production tool/observation station data'
114                    listVals = c.vtd.getValidTypes(c.vtd.DEPLOYABLE_ATOM_CATEGORY)
115                    defaultVal = allVal
116            else:
117                listVals = c.vtd.getValidTypes(c.vtd.ATOM_CATEGORY)
118                defaultVal = allVal
119           
120            c.atomTypes = utils.getVocabTermDataDropdown(listVals, \
121                                                   defaultVal = defaultVal, \
122                                                   selected = inputs.get('atomTypeID'))
123            c.providerIDs = utils.getVocabTermDataDropdown(c.vtd.getValidTypes(c.vtd.PROVIDER_CATEGORY), 
124                                                     defaultVal = allVal, \
125                                                     selected = inputs.get('providerID'))
126
127            return render("genshi", 'atom_list')
128
129        except Exception, e:
130            #we may be showing an xml document ... but it could go wrong if
131            #we have crap content ...
132            c.xml='Unexpected error loading page [%s]' %str(e)
133            c.doc=''
134            logging.error(c.xml)
135       
136        response.status_code = 400
137        return render('error')
138   
139   
140    def searchAtoms(self, providerID, atomTypeID, term):
141        '''
142        Search for atoms with specified filter info
143        @param providerID: provider ID of atom or 'all' for all
144        @param atomTypeID: type of atom or 'all' for all
145        @param term: text to search for in atom
146        @return array of dicts with info for each doc found in search
147        '''
148        logging.info("Search for atoms with the following filter: \
149            \nProviderID: '%s'\nAtom Type: '%s'\nText in title: '%s'" \
150            %(providerID, atomTypeID, term))
151       
152        dr = DocumentRetrieve(self.cf.get('NDG_EXIST','local'),
153                              self.cf.get('NDG_EXIST','passwordFile'))
154   
155        results = dr.search(term,
156                            start=1,
157                            howmany=10000, # NB, we want to get back everything!
158                            target=ndg_eXist.ATOM_TARGET,
159                            providerID = providerID, 
160                            atomTypeID = atomTypeID)
161
162        logging.info("Search completed")
163        return results
164
165           
166       
Note: See TracBrowser for help on using the repository browser.