comparison eventsRestService.py @ 1:283badd62593

new version for embedded e4D
author dwinter
date Fri, 12 Oct 2012 08:36:57 +0200
parents 26c06d568e1d
children f198c86dd1f6
comparison
equal deleted inserted replaced
0:26c06d568e1d 1:283badd62593
1 import web 1 import web
2 import eventsDB2kml 2 import eventsDB2kml
3 import harvestDataFromEvents 3 import harvestDataFromEvents
4 import urllib 4 import urllib
5 import urlparse
6
5 urls = ( 7 urls = (
6 '/xpath/(.*)', 'xpath', 8 '/xpath/(.*)', 'xpath',
9 '/xpathSearch', 'xpathSearch',
10 '/sqlSearch', 'sqlSearch',
11 '/sql', 'sql',
7 '/species', 'species', 12 '/species', 'species',
8 '/applicants', 'applicants', 13 '/applicants', 'applicants',
9 '/locations','locations' 14 '/locations','locations',
15 '/restricted', 'restricted'
10 ) 16 )
11 app = web.application(urls, globals()) 17 app = web.application(urls, globals())
12 18
13 E4D_URL="http://localhost:8180/e4D/" 19 E4D_URL="http://localhost/mmpa/"
14 BASE_URL="http://localhost:8080/" 20 BASE_URL="http://localhost/m/"
21
22 APPLICANT_FILE_NAME="http://localhost/outApplicants.xml"
23 RESEARCH_FILE_NAME="http://localhost/outResearch.xml"
15 class locations: 24 class locations:
16 25
17 def GET(self): 26 def GET(self):
18 27
19 ret="" 28 ret=""
20 29
21 url1=urllib.quote_plus("http://localhost/outApplicants.xml") 30 url1=urllib.quote_plus(APPLICANT_FILE_NAME)
22 ret+=""" 31 ret+="""
23 32
24 <a href=" 33 <a href="
25 %s?kml1=%s&source1=1" 34 %s?kml1=%s&source1=1"
26 target="e4D">applicants</a><br/> 35 >applicants</a><br/>
27 """%(E4D_URL,url1) 36 """%(E4D_URL,url1)
28 37
29 38
30 url2=urllib.quote_plus("http://localhost/outResearch.xml") 39 url2=urllib.quote_plus(RESEARCH_FILE_NAME)
31 ret+=""" 40 ret+="""
32 41
33 <a href=" 42 <a href="
34 %s?kml1=%s&source1=1" 43 %s?kml1=%s&source1=1"
35 target="e4D">research</a><br/> 44 >research</a><br/>
36 """%(E4D_URL,url2) 45 """%(E4D_URL,url2)
37 46
38 ret+= """ 47 ret+= """
39 <a href=" 48 <a href="
40 %s?kml1=%s&source1=1&kml2=%s&source2=1" 49 %s?kml1=%s&source1=1&kml2=%s&source2=1"
41 target="e4D">both</a><br/> 50 >both</a><br/>
42 """%(E4D_URL,url1,url2) 51 """%(E4D_URL,url1,url2)
43 52
44 return ret 53 return ret
45 54
46 55 class restricted:
47 56 def GET(self):
57 x=web.input()
58 pathes=x.get("path")
59 print pathes
60 select=x.get("select")
61 newRestriction=x.get("newRestriction")
62
63 if select is None:
64 select="data"
65
66
67
68 #path kann meherer abfragen enthalten durch | getrennt
69
70 splittedPath = pathes.split("|")
71 print splittedPath
72 queriesArray=[]
73 for xlinkPath in splittedPath:
74 url = urllib.unquote_plus(xlinkPath);
75
76
77 qs=urlparse.urlparse(url).query
78
79 queries=urlparse.parse_qsl(qs)
80 q=""
81 for query in queries:
82 if query[0]=='q':
83 q=query[1]
84
85
86
87
88
89 url= urlparse.urlparse(url).path
90
91 pathSplitted=url.split("xpath/")
92 if len(pathSplitted)>1:
93 path=pathSplitted[1]
94 queriesArray.append((path,q))
95 print queriesArray
96
97
98
99 db = eventsDB2kml.DBQuery()
100
101 if newRestriction is None: # keine weiteren Einschaenkungen dann erzeugen das kml file
102 res= "<events>"+ db.searchXPaths(queriesArray)+"</events>"
103
104
105
106 hv=harvestDataFromEvents.EventKMLTransformer()
107
108
109 #x,cnt=hv.readString(res, ".//research_location/place_information", 0)
110 x,cnt=hv.readString(res, ".//place_information", 0)
111
112
113
114 s = hv.toKML(x)
115
116 return s
117
118
119 #sonst suche nach restriction suche muss eine liste von werten ergeben die sich mit der REstriction versteht, dh. restriction =wert wird dann in die Links eingebaut
120 # e.g. man such nach name d.h
121 # select=%20distinct%20cast%28xpath%28%27//applicant/name/text%28%29%27,%20data%29%20as%20text[]%29%20AS%20name
122 # dann muss die Restriction darauf passen i.a. wird die Restriction dem xpath im Suchstring entsprechen,
123 # newRestriction=http%3A%2F%2Flocalhost%2Fm%2Fxpath%2F%2F%2Fapplicant%2Fnam%2Ftext%28%29%
124
125 newRestriction=urllib.unquote_plus(newRestriction)
126
127 vals = db.searchXPaths(queriesArray,select,True)
128
129
130 ret=""
131
132
133 #berechen den neuer link = alter path ergaenzt um neue restriction
134 for val in vals:
135 val=val[0] # result ist immer ein tuple
136 # mehr als ein Treffer pro eintrag
137 for v in val:
138 newQuery=""
139 splittedPath.append(newRestriction+"?q="+v)
140 splittedPathQuoted=[urllib.quote_plus(x) for x in splittedPath]
141
142 pathNew="|".join(splittedPathQuoted)
143 urlNew=urllib.quote_plus("""%srestricted?path=%s"""%(BASE_URL,pathNew))
144 ret+="""<a href="%s?kml1=%s&source1=1">%s</a><br/>"""%(E4D_URL,urlNew,v)
145 return ret
146
147 class sqlSearch:
148 def GET(self):
149 x=web.input()
150 sql=x.get("sql")
151 p=x.get("xpathPlace")
152 output=x.get("output")
153
154
155
156 url="%ssql?q=%s&p=%s"%(BASE_URL,urllib.quote_plus(sql),p)
157
158 if output=="sql":
159 raise web.seeother(url)
160
161 url=urllib.quote_plus(url)
162
163
164
165 url="""%s?kml1=%s&source1=1"""%(E4D_URL,url)
166 raise web.seeother(url)
167
168
169 class xpathSearch:
170 def GET(self):
171 x=web.input()
172 path=x.get("path")
173 value=x.get("value")
174
175 url="%sxpath/%s?q=%s"%(BASE_URL,path,value)
176
177 url=urllib.quote_plus(url)
178
179
180
181 url="""%s?kml1=%s&source1=1"""%(E4D_URL,url)
182 raise web.seeother(url)
183
184 class sql:
185 def GET(self):
186 x = web.input()
187 q= x.get("q")
188 p= x.get("p")
189 db = eventsDB2kml.DBQuery()
190 res= "<events>"+db.sql(q)+"</events>"
191
192
193
194 hv=harvestDataFromEvents.EventKMLTransformer()
195
196
197 #x,cnt=hv.readString(res, ".//research_location/place_information", 0)
198 x,cnt=hv.readString(res, p, 0)
199
200
201
202 s = hv.toKML(x)
203
204 return s
205
206
48 class xpath: 207 class xpath:
49 208
50 def GET(self, path): 209 def GET(self, path):
51 210
52 if not path: 211 if not path:
74 233
75 class applicants: 234 class applicants:
76 235
77 def GET(self): 236 def GET(self):
78 db = eventsDB2kml.DBQuery() 237 db = eventsDB2kml.DBQuery()
238
239
240 x = web.input()
241 where= x.get("where")
242
243 whereClause=""
244
245 #Beispiel:
246 #select distinct
247 #
248 #cast (xpath('//name/text()',data) as text[]) AS name
249 #
250 #from locations
251 # where
252 # 'Astrophytum asterius' = ANY (cast (xpath('//species/text()', data) as text[])) ;
253
254 if where!="" and where is not None:
255 whereClause="where %s"%where
256
79 q=""" 257 q="""
80 select 258 select
81 cast(xpath('//applicant/name/text()', data) as text[]) AS name 259 cast(xpath('//applicant/name/text()', data) as text[]) AS name
82 from locations 260 from locations
83 """ 261 %s
262 """%whereClause
84 263
85 res=db.query(q) 264 res=db.query(q)
86 #hole alle species 265 #hole alle species
87 spec=set() 266 spec=set()
88 267
95 ret="" 274 ret=""
96 spList=[x for x in spec] 275 spList=[x for x in spec]
97 276
98 spList.sort() 277 spList.sort()
99 for x in spList: 278 for x in spList:
100 print x 279
101 ret+="""<a href="/xpath///applicant/name/text()?q=%s">%s</a>"""%(x,x) 280 ret+="""<a href="%sxpath///applicant/name/text()?q=%s">%s</a>"""%(BASE_URL,x,x)
102 281
103 url=urllib.quote_plus("%sxpath///applicant/name/text()?q=%s"%(BASE_URL,x)) 282 url=urllib.quote_plus("%sxpath///applicant/name/text()?q=%s"%(BASE_URL,x))
104 ret+=""" 283 ret+="""
105 284
106 <a href=" 285 <a href="
107 %s?kml1=%s&source1=1" 286 %s?kml1=%s&source1=1"
108 target="e4D">e4D</a><br/> 287 >e4D</a><br/>
109 """%(E4D_URL,url) 288 """%(E4D_URL,url)
110 289
111 return ret 290 return ret
112 class species: 291 class species:
113 292
114 def GET(self): 293 def GET(self):
115 db = eventsDB2kml.DBQuery() 294 db = eventsDB2kml.DBQuery()
295
296 x = web.input()
297 where= x.get("where")
298
299 whereClause=""
300
301 # Beispiel:
302 #select
303 #
304 #cast (xpath('//species/text()',data) as text[]) AS name
305 #
306 #from locations
307 # where
308 # 'Anna George' = ANY (cast (xpath('//name/text()', data) as text[])) ;
309 #
310 if where!="" and where is not None:
311 whereClause="where %s"%where
312
116 q=""" 313 q="""
117 select 314 select distinct
118 cast(xpath('//species/text()', data) as text[]) AS name 315 cast(xpath('//species/text()', data) as text[]) AS name
119 from locations 316 from locations
120 """ 317 %s
121 318 """%whereClause
319
320
122 res=db.query(q) 321 res=db.query(q)
123 #hole alle species 322 #hole alle species
124 spec=set() 323 spec=set()
125 324
126 for r in res: 325 for r in res:
132 ret="" 331 ret=""
133 spList=[x for x in spec] 332 spList=[x for x in spec]
134 333
135 spList.sort() 334 spList.sort()
136 for x in spList: 335 for x in spList:
137 print x 336
138 ret+="""<a href="/xpath///species/text()?q=%s">%s</a>"""%(x,x) 337 ret+="""<a href="%sxpath///species/text()?q=%s">%s</a>"""%(BASE_URL,x,x)
139 338
140 url=urllib.quote_plus("%sxpath///species/text()?q=%s"%(BASE_URL,x)) 339 url=urllib.quote_plus("%sxpath///species/text()?q=%s"%(BASE_URL,x))
141 ret+=""" 340 ret+="""
142 341
143 <a href=" 342 <a href="
144 %s?kml1=%s&source1=1" 343 %s?kml1=%s&source1=1"
145 target="e4D">e4D</a><br/> 344 >e4D</a><br/>
146 """%(E4D_URL,url) 345 """%(E4D_URL,url)
147 346
148 return ret 347 return ret
149 348
150 if __name__ == "__main__": 349 if __name__ == "__main__":