1 | import DiscoveryTemplate |
---|
2 | from paste.request import parse_querystring |
---|
3 | import ndgSearch as NS |
---|
4 | from DIF import DIF |
---|
5 | from renderDiscoverySet import renderDiscoverySet |
---|
6 | from DiscoveryState import DiscoveryState |
---|
7 | from Utilities import myConfig,mailError |
---|
8 | from ndgObject import ndgObject |
---|
9 | debug=0 |
---|
10 | class DiscoveryGUI: |
---|
11 | ''' This class handles the NDG gui search interface, and is intimately related to the |
---|
12 | template.py code which provides the gui interface and the variables which need to be populated ''' |
---|
13 | |
---|
14 | #The key options are either there is content in the inputdictionary, in which case we have a |
---|
15 | #search to do, and then some results to present, or there is none, in which case we present |
---|
16 | #an appropriate interface back to the user (remembering that we only produce part of a page, |
---|
17 | #this can all be embedded in anything else. |
---|
18 | |
---|
19 | def __init__(self,environ,config,logger): |
---|
20 | ''' Takes a dictionary of variables which have been populated by the cgi (or whatever) |
---|
21 | handler, and tests them for sanity before handing queries off to the NDG backend xqquery |
---|
22 | web service ''' |
---|
23 | |
---|
24 | inputs=dict(parse_querystring(environ)) |
---|
25 | self.inputs=inputs |
---|
26 | self.environ=environ |
---|
27 | self.config=config |
---|
28 | self.logger=environ['wsgilog.logger'] |
---|
29 | self.mailServer=self.config.get('DEFAULT','mailServer','outbox.rl.ac.uk') |
---|
30 | self.message='' |
---|
31 | self.html='' |
---|
32 | |
---|
33 | #is this simply a blank request for an interface? |
---|
34 | if self.inputs.keys()==[]: |
---|
35 | self.html+='<div id="superPad">' |
---|
36 | self.standardInterface() |
---|
37 | self.html+='</div>' |
---|
38 | return |
---|
39 | elif self.inputs.keys()==['advanced']: |
---|
40 | self.advancedInterface() |
---|
41 | return |
---|
42 | elif self.inputs.keys()==['help']: |
---|
43 | self.helpInterface(0) |
---|
44 | return |
---|
45 | elif self.inputs.keys()==['help','advanced']: |
---|
46 | self.helpInterface(1) |
---|
47 | return |
---|
48 | |
---|
49 | #check simplest text issues common to both simple and advanced search |
---|
50 | expected=['advanced','textTarget','searchString'] |
---|
51 | self.__checkform(expected) |
---|
52 | if self.message!='': return |
---|
53 | |
---|
54 | #the following may also be present |
---|
55 | continuations={'start':1,'howmany':10} |
---|
56 | for i in continuations: |
---|
57 | if i not in self.inputs: self.inputs[i]=continuations[i] |
---|
58 | |
---|
59 | advanced=int(self.inputs['advanced']) |
---|
60 | |
---|
61 | self.html='' |
---|
62 | if not advanced: |
---|
63 | self.doText(self.inputs['searchString'],self.inputs['textTarget'], |
---|
64 | self.inputs['start'],self.inputs['howmany']) |
---|
65 | else: |
---|
66 | #check content |
---|
67 | expected=['timeLimit','startDateDay','startDateMon','startDateYear', |
---|
68 | 'endDateDay','endDateMon','endDateYear','spaceLimit', |
---|
69 | 'bboxN','bboxE','bboxS','bboxW', |
---|
70 | 'sourceLimit'] |
---|
71 | self.__checkform(expected) |
---|
72 | if self.message!='': return |
---|
73 | |
---|
74 | if int(self.inputs['sourceLimit']): |
---|
75 | scope=[] |
---|
76 | if 'sourceMDIP' in self.inputs: |
---|
77 | if self.inputs['sourceMDIP']: scope.append('MDIP') |
---|
78 | if 'sourceNERC' in self.inputs: |
---|
79 | if self.inputs['sourceNERC']: scope.append('NERC_DDC') |
---|
80 | else: |
---|
81 | scope=None |
---|
82 | |
---|
83 | if int(self.inputs['spaceLimit']): |
---|
84 | bbox=[self.inputs['bboxN'],self.inputs['bboxS'],self.inputs['bboxW'],self.inputs['bboxE']] |
---|
85 | else: |
---|
86 | bbox=None |
---|
87 | |
---|
88 | if int(self.inputs['timeLimit']): |
---|
89 | dateRange=[(self.inputs['startDateDay'],self.inputs['startDateMon'],self.inputs['startDateYear']), |
---|
90 | (self.inputs['endDateDay'],self.inputs['endDateMon'],self.inputs['endDateYear'])] |
---|
91 | else: |
---|
92 | dateRange=None |
---|
93 | |
---|
94 | self.doText(self.inputs['searchString'],self.inputs['textTarget'], |
---|
95 | self.inputs['start'],self.inputs['howmany'],scope=scope,dateRange=dateRange,bbox=bbox) |
---|
96 | |
---|
97 | return |
---|
98 | |
---|
99 | def __checkform(self,expected): |
---|
100 | ''' Simply checks the inputs to make sure the elements in expected are present ''' |
---|
101 | message="An incomplete NDG search form was received: " |
---|
102 | for i in expected: |
---|
103 | if i not in self.inputs: |
---|
104 | self.message=message+i |
---|
105 | self.html='<p>%s</p>'%self.message |
---|
106 | |
---|
107 | def __setState(self,id,searchString,hits,offset,stride): |
---|
108 | ''' Sets the discovery state to be used by external routines ''' |
---|
109 | if hits<stride:stride=hits |
---|
110 | return DiscoveryState(id,searchString,self.environ,hits,offset,stride) |
---|
111 | |
---|
112 | def doText(self,searchString,textTarget,start,howmany,scope=None,dateRange=None,bbox=None): |
---|
113 | ''' Carry out a text search for <searchString> |
---|
114 | in the <textTarget> where the accepted text target values are controlled |
---|
115 | by the DiscoveryTemplate GUI, and are: All, Authors, Parameters ''' |
---|
116 | |
---|
117 | start,howmany=int(start),int(howmany) # url arguments need conversion ... |
---|
118 | |
---|
119 | ws=NS.ndgSearch(logger=self.logger) |
---|
120 | documents=ws.search(searchString,start=start,howmany=howmany,target=textTarget, |
---|
121 | scope=scope,dateRange=dateRange,bbox=bbox) |
---|
122 | if ws.error !=None: |
---|
123 | for i in ws.error: |
---|
124 | self.html+='<p>%s</p>'%i |
---|
125 | return |
---|
126 | |
---|
127 | hits=ws.hits |
---|
128 | id=ws.serverSessionID |
---|
129 | state=self.__setState(id,searchString,hits,start,howmany) |
---|
130 | if hits==0: |
---|
131 | self.html+='<p> No records found </p>' |
---|
132 | else: |
---|
133 | try: |
---|
134 | results=ws.getLabelledDocs(format='DIF') |
---|
135 | difs=[] |
---|
136 | errors=[] |
---|
137 | for result in results: |
---|
138 | obj=ndgObject(result[0]) |
---|
139 | obj.setConfig(self.config) |
---|
140 | try: |
---|
141 | difs.append(DIF(result[1],ndgObj=obj)) |
---|
142 | except ValueError,e: |
---|
143 | errors.append((result[0],str(e))) |
---|
144 | if results==[]: |
---|
145 | self.html+='<p>No results for "%s"!</p>'%searchString |
---|
146 | elif difs==[]: |
---|
147 | self.html+='<p>No usable results for "%s"!'%searchString |
---|
148 | else: |
---|
149 | if errors<>[]: |
---|
150 | self.html+='<p>Search results for "%s"'%searchString |
---|
151 | dp=[] |
---|
152 | for e in errors: |
---|
153 | n=ndgObject(e[0]) |
---|
154 | if n.repository not in dp: dp.append(n.repository) |
---|
155 | if len(dp)<>1: |
---|
156 | dp='[Various Data Providers]' |
---|
157 | else: |
---|
158 | dp='[%s]'%dp[0] |
---|
159 | self.html+=' (unfortunately %s hits matched unformattable documents from %s, an internal error has been logged):</p>'%(len(errors),dp) |
---|
160 | mailError('b.n.lawrence@rl.ac.uk','DIF errors',str(errors),server=self.mailServer) |
---|
161 | self.html+=renderDiscoverySet(difs,state,config=self.config, |
---|
162 | summary=1,spatial=1,temporal=1,services=1) |
---|
163 | except ValueError,e: |
---|
164 | if debug: |
---|
165 | raise ValueError,str(e) |
---|
166 | else: |
---|
167 | self.html='<p> Error retrieving documents for %s hits is [%s]</p>'%(hits,e) |
---|
168 | |
---|
169 | self.standardInterface() |
---|
170 | return |
---|
171 | |
---|
172 | def standardInterface(self): |
---|
173 | |
---|
174 | try: |
---|
175 | helpAddress=self.config.get('SEARCH','helpURL') |
---|
176 | discoveryURL=self.config.get('SEARCH','discoveryURL') |
---|
177 | advancedURL=self.config.get('SEARCH','advancedURL') |
---|
178 | except: |
---|
179 | self.message='Error, invalid configuration for search interface' |
---|
180 | self.html+='' |
---|
181 | return |
---|
182 | |
---|
183 | self.html+=DiscoveryTemplate.main%locals() |
---|
184 | return |
---|
185 | |
---|
186 | def advancedInterface(self): |
---|
187 | |
---|
188 | try: |
---|
189 | helpAddress=self.config.get('SEARCH','helpURL') |
---|
190 | discoveryURL=self.config.get('SEARCH','discoveryURL') |
---|
191 | advancedURL=self.config.get('SEARCH','advancedURL') |
---|
192 | except: |
---|
193 | self.message='Error, invalid configuration for search interface' |
---|
194 | self.html='' |
---|
195 | return |
---|
196 | advancedSubmit=DiscoveryTemplate.advancedSubmit%locals() |
---|
197 | searchText=DiscoveryTemplate.searchText |
---|
198 | searchTime=DiscoveryTemplate.searchTime |
---|
199 | searchArea=DiscoveryTemplate.searchArea |
---|
200 | searchSource=DiscoveryTemplate.searchSource |
---|
201 | self.html+=DiscoveryTemplate.advanced%locals() |
---|
202 | return |
---|
203 | |
---|
204 | def helpInterface(self,advanced): |
---|
205 | if advanced: |
---|
206 | self.advancedInterface() |
---|
207 | else: |
---|
208 | self.standardInterface() |
---|
209 | helpFile=self.config.get('HELP','helpFile') |
---|
210 | f=file(helpFile,'r') |
---|
211 | self.html+=f.read() |
---|
212 | return |
---|
213 | |
---|
214 | |
---|