This repository was archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathsearch_demo.py
More file actions
120 lines (97 loc) · 3.58 KB
/
search_demo.py
File metadata and controls
120 lines (97 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
"""A simple guest book app that demonstrates the App Engine search API."""
from cgi import parse_qs
from datetime import datetime
import os
import string
import urllib
from urlparse import urlparse
import webapp2
from webapp2_extras import jinja2
from google.appengine.api import search
from google.appengine.api import users
_INDEX_NAME = 'greeting'
# _ENCODE_TRANS_TABLE = string.maketrans('-: .@', '_____')
class BaseHandler(webapp2.RequestHandler):
"""The other handlers inherit from this class. Provides some helper methods
for rendering a template."""
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(self, filename, template_args):
self.response.write(self.jinja2.render_template(filename, **template_args))
class FakeRequest():
"""Protects against annoying failures"""
results = ()
class MainPage(BaseHandler):
"""Handles search requests for comments."""
def get(self):
"""Handles a get request with a query."""
uri = urlparse(self.request.uri)
query = ''
if uri.query:
query = parse_qs(uri.query)
query = query['query'][0]
# sort results by author descending
expr_list = [search.SortExpression(
expression='author', default_value='',
direction=search.SortExpression.DESCENDING)]
# construct the sort options
sort_opts = search.SortOptions(
expressions=expr_list)
query_options = search.QueryOptions(
limit=3,
sort_options=sort_opts)
query_obj = search.Query(query_string=query, options=query_options)
results = FakeRequest()
try:
results = search.Index(name=_INDEX_NAME).search(query=query_obj)
except Exception, e:
pass
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'results': results,
'number_returned': len(results.results),
'url': url,
'url_linktext': url_linktext,
}
self.render_template('index.html', template_values)
def CreateDocument(author, content):
"""Creates a search.Document from content written by the author."""
if author:
nickname = author.nickname().split('@')[0]
else:
nickname = 'anonymous'
# Let the search service supply the document id.
return search.Document(
fields=[search.TextField(name='author', value=nickname),
search.TextField(name='comment', value=content),
search.DateField(name='date', value=datetime.now().date())])
class Comment(BaseHandler):
"""Handles requests to index comments."""
def post(self):
"""Handles a post request."""
author = None
if users.get_current_user():
author = users.get_current_user()
content = self.request.get('content')
query = self.request.get('search')
if content:
search.Index(name=_INDEX_NAME).put(CreateDocument(author, content))
if query:
self.redirect('/?' + urllib.urlencode(
#{'query': query}))
{'query': query.encode('utf-8')}))
else:
self.redirect('/')
application = webapp2.WSGIApplication(
[('/', MainPage),
('/sign', Comment)],
debug=True)