source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/DiscoveryGUI.py @ 1615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/DiscoveryGUI.py@1615
Revision 1615, 6.3 KB checked in by lawrence, 13 years ago (diff)

Modifications to discovery (and browse) to support ndgDoc as an interim
measure ...

Line 
1import DiscoveryTemplate
2import ndgSearch as NS
3from DIF import DIF
4from renderDiscoverySet import renderDiscoverySet
5from DiscoveryState import DiscoveryState
6
7class DiscoveryGUI:
8    ''' This class handles the NDG gui search interface, and is intimately related to the
9    template.py code which provides the gui interface and the variables which need to be populated '''
10   
11    #The key options are either there is content in the inputdictionary, in which case we have a
12    #search to do, and then some results to present, or there is none, in which case we present
13    #an appropriate interface back to the user (remembering that we only produce part of a page,
14    #this can all be embedded in anything else.
15   
16    def __init__(self,request,config=None):
17        ''' Takes a dictionary of variables which have been populated by the cgi (or whatever)
18        handler, and tests them for sanity before handing queries off to the NDG backend xqquery
19        web service '''
20       
21        self.request=request
22        self.inputs=request.variables
23        self.message=''
24        self.html=''
25        self.config=config
26        self.serviceFile=self.config.get('services','serviceFile')
27       
28        #is this simply a blank request for an interface?
29        if self.inputs.keys()==[]:
30            self.standardInterface()
31            return
32        elif self.inputs.keys()==['advanced']:
33            self.advancedInterface()
34            return
35        elif self.inputs.keys()==['help']:
36            self.helpInterface(0)
37            return
38        elif self.inputs.keys()==['help','advanced']:
39            self.helpInterface(1)
40            return
41       
42        #check simplest text issues common to both simple and advanced search
43        expected=['advanced','textTarget','searchString']
44        self.__checkform(expected)
45        if self.message!='': return
46       
47        #the following may also be present
48        continuations={'start':1,'howmany':20}
49        for i in continuations:
50            if i not in self.inputs: self.inputs[i]=continuations[i]
51           
52        advanced=int(self.inputs['advanced'])
53       
54        if not advanced:
55            self.doText(self.inputs['searchString'],self.inputs['textTarget'],
56                        self.inputs['start'],self.inputs['howmany'])
57        else:
58            #check content
59            expected=['timeLimit','startDateDay','startDateMon','startDateYear',
60                        'endDateDay','endDateMon','endDateYear','spaceLimit',
61                        'bboxN','bboxE','bboxS','bboxW',
62                        'sourceLimit']
63            self.__checkform(expected)
64            if self.message!='': return
65           
66            if self.inputs['timeLimit']=="0" and self.inputs['spaceLimit']=="0": 
67                if int(self.inputs['sourceLimit']):
68                    scope=[]
69                    if 'sourceMDIP' in self.inputs:
70                        if self.inputs['sourceMDIP']: scope.append('MDIP')
71                    if 'sourceNERC' in self.inputs:
72                        if self.inputs['sourceNERC']: scope.append('NERC')
73                else:
74                    scope=None
75                    #it's a simple text search from the advanced box
76                self.doText(self.inputs['searchString'],self.inputs['textTarget'],
77                            self.inputs['start'],self.inputs['howmany'],scope)
78            else :
79                #it's more complicated
80                self.doGeoSpatial()
81        return
82           
83    def __checkform(self,expected):
84        ''' Simply checks the inputs to make sure the elements in expected are present '''
85        message="An incomplete NDG search form was received: "
86        for i in expected:
87            if i not in self.inputs: 
88                self.message=message+i
89                self.html='<p>%s</p>'%self.message
90               
91    def __setState(self,id,hits,offset,stride):
92        ''' Sets the discovery state to be used by external routines '''
93        return DiscoveryState(id,self.request,hits,offset,stride)
94       
95    def doText(self,searchString,textTarget,start,howmany,scope=None):
96        ''' Carry out a text search for <searchString> without geospatial elements,
97        in the <textTarget> where the accepted text target values are controlled
98        by the DiscoveryTemplate GUI, and are: All, Authors, Parameters '''
99        # although at the moment we only support ALL
100       
101        start,howmany=int(start),int(howmany)  # url arguments need conversion ...
102       
103        ws=NS.ndgSearch()
104        documents=ws.search(searchString,start=start,howmany=howmany,scope=scope)
105       
106        hits=ws.hits
107        id=ws.serverSessionID
108        state=self.__setState(id,hits,start,howmany)
109        if hits==0: 
110            self.html='<p> No records found </p>'
111        else:
112            results=ws.getAllDocsAsElements()
113            difs=[]
114            for result in results: difs.append(DIF(result,serviceFile=self.serviceFile,et=1))
115            self.html=renderDiscoverySet(difs,state,
116                               summary=1,spatial=1,temporal=1,services=1)
117               
118        return
119   
120    def doGeoSpatial(self):
121        self.html='Geospatial and Time limiting not yet implemented'
122        return
123   
124    def standardInterface(self):
125       
126        try:
127            helpAddress=self.config.get('SEARCH','helpURL')
128            discoveryURL=self.config.get('SEARCH','discoveryURL')
129            advancedURL=self.config.get('SEARCH','advancedURL')
130        except:
131            self.message='Error, invalid configuration for search interface'
132            self.html=''
133            return
134       
135        self.html=DiscoveryTemplate.main%locals()
136        return
137   
138    def advancedInterface(self):
139       
140        try:
141            helpAddress=self.config.get('SEARCH','helpURL')
142            discoveryURL=self.config.get('SEARCH','discoveryURL')
143            advancedURL=self.config.get('SEARCH','advancedURL')
144        except:
145            self.message='Error, invalid configuration for search interface'
146            self.html=''
147            return
148        self.html=DiscoveryTemplate.advanced%locals()
149        return
150   
151    def helpInterface(self,advanced):
152        if advanced:
153            self.advancedInterface()
154        else:
155            self.standardInterface()
156        self.html=DiscoveryTemplate.help+self.html
157        return
158       
159       
Note: See TracBrowser for help on using the repository browser.