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

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

First cut of standalone discovery code ... integrated into one python
module with the browe stuff ... (next steps include split into two
parents and one child egg as well as making it actually work).

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