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

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

Modifications for wsgi (and hence fastcgi) compliance in new discovery

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