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

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

Discovery Gui now supports author and parameter searches properly.

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.logger=self.config.getLog()
29        self.serviceFile=configDir+self.config.get('services','serviceFile')
30       
31        #is this simply a blank request for an interface?
32        if self.inputs.keys()==[]:
33            self.html+='<div id="superPad">'
34            self.standardInterface()
35            self.html+='</div>'
36            return
37        elif self.inputs.keys()==['advanced']:
38            self.advancedInterface()
39            return
40        elif self.inputs.keys()==['help']:
41            self.helpInterface(0)
42            return
43        elif self.inputs.keys()==['help','advanced']:
44            self.helpInterface(1)
45            return
46       
47        #check simplest text issues common to both simple and advanced search
48        expected=['advanced','textTarget','searchString']
49        self.__checkform(expected)
50        if self.message!='': return
51       
52        #the following may also be present
53        continuations={'start':1,'howmany':10}
54        for i in continuations:
55            if i not in self.inputs: self.inputs[i]=continuations[i]
56           
57        advanced=int(self.inputs['advanced'])
58       
59        self.html=''
60        if not advanced:
61            self.doText(self.inputs['searchString'],self.inputs['textTarget'],
62                        self.inputs['start'],self.inputs['howmany'])
63        else:
64            #check content
65            expected=['timeLimit','startDateDay','startDateMon','startDateYear',
66                        'endDateDay','endDateMon','endDateYear','spaceLimit',
67                        'bboxN','bboxE','bboxS','bboxW',
68                        'sourceLimit']
69            self.__checkform(expected)
70            if self.message!='': return
71             
72            if int(self.inputs['sourceLimit']):
73                scope=[]
74                if 'sourceMDIP' in self.inputs:
75                    if self.inputs['sourceMDIP']: scope.append('MDIP')
76                if 'sourceNERC' in self.inputs:
77                    if self.inputs['sourceNERC']: scope.append('NERC')
78            else:
79                scope=None
80               
81            if int(self.inputs['spaceLimit']):
82                bbox=[self.inputs['bboxN'],self.inputs['bboxS'],self.inputs['bboxW'],self.inputs['bboxE']]
83            else:
84                bbox=None
85               
86            if int(self.inputs['timeLimit']):
87                dateRange=[(self.inputs['startDateDay'],self.inputs['startDateMon'],self.inputs['startDateYear']),
88                           (self.inputs['endDateDay'],self.inputs['endDateMon'],self.inputs['endDateYear'])]
89            else:
90                dateRange=None
91               
92            self.doText(self.inputs['searchString'],self.inputs['textTarget'],
93                        self.inputs['start'],self.inputs['howmany'],scope=scope,dateRange=dateRange,bbox=bbox)
94           
95        return
96           
97    def __checkform(self,expected):
98        ''' Simply checks the inputs to make sure the elements in expected are present '''
99        message="An incomplete NDG search form was received: "
100        for i in expected:
101            if i not in self.inputs: 
102                self.message=message+i
103                self.html='<p>%s</p>'%self.message
104               
105    def __setState(self,id,hits,offset,stride):
106        ''' Sets the discovery state to be used by external routines '''
107        return DiscoveryState(id,self.request,hits,offset,stride)
108       
109    def doText(self,searchString,textTarget,start,howmany,scope=None,dateRange=None,bbox=None):
110        ''' Carry out a text search for <searchString>
111        in the <textTarget> where the accepted text target values are controlled
112        by the DiscoveryTemplate GUI, and are: All, Authors, Parameters '''
113       
114        start,howmany=int(start),int(howmany)  # url arguments need conversion ...
115       
116        ws=NS.ndgSearch(logger=self.logger)
117        documents=ws.search(searchString,start=start,howmany=howmany,target=textTarget,
118                            scope=scope,dateRange=dateRange,bbox=bbox)
119        if ws.error !=None:
120            for i in ws.error:
121                self.html+='<p>%s</p>'%i
122            return
123       
124        hits=ws.hits
125        id=ws.serverSessionID
126        state=self.__setState(id,hits,start,howmany)
127        if hits==0: 
128            self.html+='<p> No records found </p>'
129        else:
130            results=ws.getAllDocsAsElements()
131            difs=[]
132            for result in results: difs.append(DIF(result,serviceFile=self.serviceFile,et=1))
133            self.html+=renderDiscoverySet(difs,state,
134                               summary=1,spatial=1,temporal=1,services=1)
135                               
136        self.standardInterface()
137        return
138
139    def standardInterface(self):
140       
141        try:
142            helpAddress=self.config.get('SEARCH','helpURL')
143            discoveryURL=self.config.get('SEARCH','discoveryURL')
144            advancedURL=self.config.get('SEARCH','advancedURL')
145        except:
146            self.message='Error, invalid configuration for search interface'
147            self.html+=''
148            return
149       
150        self.html+=DiscoveryTemplate.main%locals()
151        return
152   
153    def advancedInterface(self):
154       
155        try:
156            helpAddress=self.config.get('SEARCH','helpURL')
157            discoveryURL=self.config.get('SEARCH','discoveryURL')
158            advancedURL=self.config.get('SEARCH','advancedURL')
159        except:
160            self.message='Error, invalid configuration for search interface'
161            self.html=''
162            return
163        advancedSubmit=DiscoveryTemplate.advancedSubmit%locals()
164        searchText=DiscoveryTemplate.searchText
165        searchTime=DiscoveryTemplate.searchTime
166        searchArea=DiscoveryTemplate.searchArea
167        searchSource=DiscoveryTemplate.searchSource
168        self.html+=DiscoveryTemplate.advanced%locals()
169        return
170   
171    def helpInterface(self,advanced):
172        if advanced:
173            self.advancedInterface()
174        else:
175            self.standardInterface()
176        helpFile=self.config.get('HELP','helpFile')
177        f=file(helpFile,'r')
178        self.html+=f.read()
179        return
180       
181       
Note: See TracBrowser for help on using the repository browser.