Skip to content

Commit 9c86b39

Browse files
committed
working version with search
1 parent 6366b3c commit 9c86b39

9 files changed

+178
-8
lines changed

app.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ handlers:
1616
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
1717
login: admin
1818

19+
- url: /api/index
20+
script: index.py
21+
1922
- url: /api/.*
2023
script: api/api.py
2124

25+
- url: /search
26+
script: search.py
27+
2228
- url: /.*
2329
script: main.py

course.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN"
2+
"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+
<title>WDA Courses Catalog - Mobile</title>
7+
<link href="/static/css/default.css" type="text/css" rel="stylesheet" />
8+
</head>
9+
<body>
10+
<center>
11+
<img src="/static/img/wda-training.png"/>
12+
<hr style="border:1px dashed;color:58595b" width="100%"/>
13+
14+
<form action="/search" method="get">
15+
<input name="query" type="text" value=""><input type="submit" value="Search">
16+
</form>
17+
<br/>
18+
<br/>
19+
Categories: <a href="">Technology</a> <a href="">Management</a> <a href="">MS Office</a>
20+
21+
<p>
22+
{% for r in results %}
23+
<b>
24+
<a href="/course?id={{r.id}}">{{ r.name }}</a>
25+
</b>
26+
<br/>
27+
{% endfor %}
28+
</p>
29+
30+
<p>
31+
<a href="http://wda.dc.gov">Home</a>
32+
</p>
33+
34+
</center>
35+
36+
</body>
37+
</html>

index.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import cgi
3+
4+
from google.appengine.api import users
5+
from google.appengine.ext import webapp
6+
from google.appengine.ext.webapp.util import run_wsgi_app
7+
8+
from google.appengine.ext import db
9+
import models
10+
11+
from whoosh import store
12+
from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
13+
from whoosh.index import getdatastoreindex
14+
from whoosh.index import create_in
15+
from whoosh.qparser import QueryParser, MultifieldParser
16+
17+
#SEARCHSCHEMA = Schema(title=TEXT(stored=True))
18+
SEARCHSCHEMA = Schema(title=TEXT(stored=True), content=TEXT,
19+
path=ID(stored=True), tags=KEYWORD, icon=STORED)
20+
21+
22+
class IndexAPI(webapp.RequestHandler):
23+
def get(self):
24+
#ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
25+
ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
26+
courses=models.Course().all()
27+
for course in courses:
28+
writer = ix.writer()
29+
writer.add_document(title=course.name, content=course.description, path=course.course_id)
30+
writer.commit()
31+
self.response.out.write("indexed %s <br/>" % (course.name))
32+
33+
application = webapp.WSGIApplication(
34+
[('/api/index', IndexAPI)],
35+
debug=True)
36+
37+
def main():
38+
run_wsgi_app(application)
39+
40+
if __name__ == "__main__":
41+
main()

main.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<img src="/static/img/wda-training.png"/>
1212
<hr style="border:1px dashed;color:58595b" width="100%"/>
1313

14-
<form>
15-
<input/> <button name="go" />
14+
<form action="/search" method="get">
15+
<input name="query" type="text" value=""><input type="submit" value="Search">
1616
</form>
1717
<br/>
1818
<br/>

main.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ def get(self):
1313
path = os.path.join(os.path.dirname(__file__), 'main.html')
1414
self.response.out.write(template.render(path, template_values))
1515

16-
class RoutesHandler(webapp.RequestHandler):
16+
class CourseHandler(webapp.RequestHandler):
1717
def get(self):
1818

19-
template_values = {'route': "route_tag"}
20-
path = os.path.join(os.path.dirname(__file__), 'directions.html')
19+
template_values = {'name': "course_name",
20+
'id':'course_id'}
21+
path = os.path.join(os.path.dirname(__file__), 'course.html')
2122
self.response.out.write(template.render(path, template_values))
2223

2324

2425
def main():
2526
application = webapp.WSGIApplication(
2627
[('/', MainHandler),
27-
('/whereismybus/route/.*', RoutesHandler),
28+
('/course', CourseHandler),
2829
],
2930
debug=True)
3031
wsgiref.handlers.CGIHandler().run(application)
-6.31 KB
Binary file not shown.

search.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN"
2+
"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+
<title>WDA Courses Catalog - Mobile</title>
7+
<link href="/static/css/default.css" type="text/css" rel="stylesheet" />
8+
</head>
9+
<body>
10+
<center>
11+
<img src="/static/img/wda-training.png"/>
12+
<hr style="border:1px dashed;color:58595b" width="100%"/>
13+
14+
<form action="/search" method="get">
15+
<input name="query" type="text" value=""><input type="submit" value="Search">
16+
</form>
17+
<br/>
18+
<br/>
19+
Categories: <a href="">Technology</a> <a href="">Management</a> <a href="">MS Office</a>
20+
21+
<p>
22+
{% for r in results %}
23+
<b>
24+
<a href="/course?id={{r.id}}">{{ r.name }}</a>
25+
</b>
26+
<br/>
27+
{% endfor %}
28+
</p>
29+
30+
<p>
31+
<a href="http://wda.dc.gov">Home</a>
32+
</p>
33+
34+
</center>
35+
36+
</body>
37+
</html>

search.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import cgi
2+
import os
3+
4+
from google.appengine.ext import webapp
5+
from google.appengine.ext.webapp.util import run_wsgi_app
6+
from google.appengine.ext.webapp import template
7+
8+
9+
from whoosh import store
10+
from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
11+
from whoosh.index import getdatastoreindex
12+
from whoosh.qparser import QueryParser, MultifieldParser
13+
import logging
14+
15+
SEARCHSCHEMA = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(stored=True))
16+
17+
18+
class SearchPage(webapp.RequestHandler):
19+
def get(self):
20+
21+
ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
22+
parser = QueryParser("content", schema = ix.schema)
23+
q = parser.parse(self.request.get('query'))
24+
results = ix.searcher().search(q)
25+
26+
r=[]
27+
for result in results:
28+
rr={"name":result['title'], "id":result['path']}
29+
r.append(rr)
30+
31+
template_values = {"results":r}
32+
path = os.path.join(os.path.dirname(__file__), 'search.html')
33+
self.response.out.write(template.render(path, template_values))
34+
35+
36+
37+
application = webapp.WSGIApplication(
38+
[
39+
('/search', SearchPage)],
40+
debug=True)
41+
42+
def main():
43+
run_wsgi_app(application)
44+
45+
if __name__ == "__main__":
46+
main()

tools/upload.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import csv
22
import httplib, urllib
33

4+
location="localhost:8097"
5+
#location="wda-mobile.appspot.com"
46

57
courses={}
68

@@ -48,7 +50,7 @@
4850
print data
4951

5052
params = urllib.urlencode(data)
51-
conn = httplib.HTTPConnection("localhost:8097")
53+
conn = httplib.HTTPConnection(location)
5254
conn.request("POST", "/api/course", params)
5355
response = conn.getresponse()
5456
print response.status, response.reason
@@ -66,7 +68,7 @@
6668
"prerequisite":row[1]}
6769

6870
params = urllib.urlencode(data)
69-
conn = httplib.HTTPConnection("localhost:8097")
71+
conn = httplib.HTTPConnection(location)
7072
conn.request("POST", "/api/prerequisite", params)
7173
response = conn.getresponse()
7274
print response.status, response.reason

0 commit comments

Comments
 (0)