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

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

Better handling of request URLs for both discovery and browse

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       
36        #check simplest text issues common to both simple and advanced search
37        expected=['advanced','textTarget','searchString']
38        self.__checkform(expected)
39        if self.message!='': return
40       
41        #the following may also be present
42        continuations={'start':1,'howmany':20}
43        for i in continuations:
44            if i not in self.inputs: self.inputs[i]=continuations[i]
45           
46        advanced=int(self.inputs['advanced'])
47       
48        if not advanced:
49            self.doText(self.inputs['searchString'],self.inputs['textTarget'],
50                        self.inputs['start'],self.inputs['howmany'])
51        else:
52            #check content
53            expected=['timeLimit','startDateDay','startDateMon','startDateYear',
54                        'endDateDay','endDateMon','endDateYear','spaceLimit',
55                        'bboxN','bboxE','bboxS','bboxW','sourceLimit']
56            self.__checkform(expected)
57            if self.message!='': return
58           
59            if self.inputs['timeLimit']=="None" and self.inputs['sourceLimit']=="None" and \
60                self.inputs['spaceLimit']=="None":
61                #it's a simple text search from the advanced box
62                self.doText(self.inputs['searchString'],self.inputs['textTarget'],
63                            self.inputs['start'],self.inputs['howmany'])
64            else:
65                #it's more complicated
66                self.doGeoSpatial()
67        return
68           
69    def __checkform(self,expected):
70        ''' Simply checks the inputs to make sure the elements in expected are present '''
71        message="An incomplete NDG search form was received: "
72        for i in expected:
73            if i not in self.inputs: 
74                self.message=message+i
75                self.html='<p>%s</p>'%self.message
76               
77    def __setState(self,id,hits,offset,stride):
78        ''' Sets the discovery state to be used by external routines '''
79        return DiscoveryState(id,self.request,hits,offset,stride)
80       
81    def doText(self,searchString,textTarget,start,howmany):
82        ''' Carry out a text search for <searchString> without geospatial elements,
83        in the <textTarget> where the accepted text target values are controlled
84        by the DiscoveryTemplate GUI, and are: All, Authors, Parameters '''
85        # although at the moment we only support ALL
86       
87        start,howmany=int(start),int(howmany)  # url arguments need conversion ...
88       
89        ws=NS.ndgSearch()
90        documents=ws.search(searchString,start=start,howmany=howmany)
91        hits=ws.hits
92        id=ws.serverSessionID
93        state=self.__setState(id,hits,start,howmany)
94        if hits==0: 
95            self.html='<p> No records found </p>'
96        else:
97            results=ws.getAllDocsAsElements()
98            difs=[]
99            for result in results: difs.append(DIF(result,serviceFile=self.serviceFile,et=1))
100            self.html=renderDiscoverySet(difs,state,
101                               summary=1,spatial=1,temporal=1,services=1)
102               
103        return
104   
105    def doGeoSpatial(self):
106        pass
107   
108    def standardInterface(self):
109       
110        try:
111            helpAddress=self.config.get('SEARCH','helpURL')
112            discoveryURL=self.config.get('SEARCH','discoveryURL')
113            advancedURL=self.config.get('SEARCH','advancedURL')
114        except:
115            self.message='Error, invalid configuration for search interface'
116            self.html=''
117            return
118       
119        self.html=DiscoveryTemplate.main%locals()
120        return
121   
122    def advancedInterface(self):
123       
124        try:
125            helpAddress=self.config.get('SEARCH','helpURL')
126            discoveryURL=self.config.get('SEARCH','discoveryURL')
127            advancedURL=self.config.get('SEARCH','advancedURL')
128        except:
129            self.message='Error, invalid configuration for search interface'
130            self.html=''
131            return
132        self.html=DiscoveryTemplate.advanced%locals()
133        return
134       
135       
Note: See TracBrowser for help on using the repository browser.