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

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

Strip out code not relevant to MILK - mainly WCS and WMS stuff - also including the CSML server code + trackback code
Also tidy up structure of 'public' dir - setting up new 'style' dir and
centralising icons in icons dir + remove all unused icons, javascript and stylesheets.
Also strip out testcase code and populate new test directory structure.

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