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

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

New NDG discovery interface, including CSS, still a few
bits to do, but functional as WSGI now.

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 self.inputs['timeLimit']=="0" and self.inputs['spaceLimit']=="0": 
73                if int(self.inputs['sourceLimit']):
74                    scope=[]
75                    if 'sourceMDIP' in self.inputs:
76                        if self.inputs['sourceMDIP']: scope.append('MDIP')
77                    if 'sourceNERC' in self.inputs:
78                        if self.inputs['sourceNERC']: scope.append('NERC')
79                else:
80                    scope=None
81                    #it's a simple text search from the advanced box
82                self.doText(self.inputs['searchString'],self.inputs['textTarget'],
83                            self.inputs['start'],self.inputs['howmany'],scope)
84            else :
85                #it's more complicated
86                self.doGeoSpatial()
87        return
88           
89    def __checkform(self,expected):
90        ''' Simply checks the inputs to make sure the elements in expected are present '''
91        message="An incomplete NDG search form was received: "
92        for i in expected:
93            if i not in self.inputs: 
94                self.message=message+i
95                self.html='<p>%s</p>'%self.message
96               
97    def __setState(self,id,hits,offset,stride):
98        ''' Sets the discovery state to be used by external routines '''
99        return DiscoveryState(id,self.request,hits,offset,stride)
100       
101    def doText(self,searchString,textTarget,start,howmany,scope=None):
102        ''' Carry out a text search for <searchString> without geospatial elements,
103        in the <textTarget> where the accepted text target values are controlled
104        by the DiscoveryTemplate GUI, and are: All, Authors, Parameters '''
105        # although at the moment we only support ALL
106       
107        start,howmany=int(start),int(howmany)  # url arguments need conversion ...
108       
109        ws=NS.ndgSearch(logger=self.logger)
110        documents=ws.search(searchString,start=start,howmany=howmany,scope=scope)
111       
112        hits=ws.hits
113        id=ws.serverSessionID
114        state=self.__setState(id,hits,start,howmany)
115        if hits==0: 
116            self.html+='<p> No records found </p>'
117        else:
118            results=ws.getAllDocsAsElements()
119            difs=[]
120            for result in results: difs.append(DIF(result,serviceFile=self.serviceFile,et=1))
121            self.html+=renderDiscoverySet(difs,state,
122                               summary=1,spatial=1,temporal=1,services=1)
123                               
124        self.standardInterface()
125        return
126   
127    def doGeoSpatial(self):
128        self.html+='Geospatial and Time limiting not yet implemented'
129        return
130   
131    def standardInterface(self):
132       
133        try:
134            helpAddress=self.config.get('SEARCH','helpURL')
135            discoveryURL=self.config.get('SEARCH','discoveryURL')
136            advancedURL=self.config.get('SEARCH','advancedURL')
137        except:
138            self.message='Error, invalid configuration for search interface'
139            self.html+=''
140            return
141       
142        self.html+=DiscoveryTemplate.main%locals()
143        return
144   
145    def advancedInterface(self):
146       
147        try:
148            helpAddress=self.config.get('SEARCH','helpURL')
149            discoveryURL=self.config.get('SEARCH','discoveryURL')
150            advancedURL=self.config.get('SEARCH','advancedURL')
151        except:
152            self.message='Error, invalid configuration for search interface'
153            self.html=''
154            return
155        advancedSubmit=DiscoveryTemplate.advancedSubmit%locals()
156        searchText=DiscoveryTemplate.searchText
157        searchTime=DiscoveryTemplate.searchTime
158        searchArea=DiscoveryTemplate.searchArea
159        searchSource=DiscoveryTemplate.searchSource
160        self.html+=DiscoveryTemplate.advanced%locals()
161        return
162   
163    def helpInterface(self,advanced):
164        if advanced:
165            self.advancedInterface()
166        else:
167            self.standardInterface()
168        self.html+=DiscoveryTemplate.help+self.html
169        return
170       
171       
Note: See TracBrowser for help on using the repository browser.