This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
executable file
·160 lines (147 loc) · 6.47 KB
/
main.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
#
# The MIT License
#
# Copyright (c) 2009 William T. Katz
# Website/Contact: http://www.billkatz.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""A super simple Google App Engine text posting app.
Logged in visitors can add some test and search for keywords across all
added pages. It demos a simple full text search module.
"""
__author__ = 'William T. Katz'
import cgi
import logging
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
# The following are necessary for full-text search demo
import search
INDEXING_URL = '/tasks/searchindexing'
class Page(search.Searchable, db.Model):
user = db.UserProperty()
title = db.StringProperty()
content = db.TextProperty()
created = db.DateTimeProperty(auto_now=True)
INDEX_TITLE_FROM_PROP = 'title'
# INDEX_USES_MULTI_ENTITIES = False
class SimplePage(webapp.RequestHandler):
def render(self, html):
user = users.get_current_user()
page = '<html><body><div style="display:inline"><a href="/">Add Page</a> | '
if user:
page += 'Logged in as %s ' % (user.nickname())
logout_url = users.create_logout_url(self.request.uri)
page += '| <a href="%s">Logout</a>' % (logout_url)
else:
login_url = users.create_login_url(self.request.uri)
page += '<a href="%s">Google login</a>' % (login_url)
page += """</div>
<hr>
<h3>Full Text Search Test</h3>
<p>This app tests a full text search module for Google App Engine.
Once you are logged in, you can add text pages that will be indexed via
Task Queue API tasks. The search indices are efficiently stored using
"Relation Index" entities as described in
<a href="http://code.google.com/events/io/sessions/BuildingScalableComplexApps.html">
this Google I/O talk.</a></p>
<p>My blog has an
<a href="http://www.billkatz.com/2009/6/Simple-Full-Text-Search-for-App-Engine">
article on this appengine-search module</a>. You can download the code from the
<a href="http://github.com/DocSavage/appengine-search">appengine-search
github repository</a> under a liberal open source (MIT) license.</p>
<form action="/search" method="get">
Search for phrase (e.g., 'lorem ipsum'):
"""
page += '<input name="phrase"'
phrase = self.request.get('phrase')
if phrase:
page += ' value="%s">' % (phrase)
page += '<input type="submit" name="submitbtn" value="Return Pages">'
page += '<input type="submit" name="submitbtn" value="Return Keys Only">'
page += """
<p><strong>Return Pages</strong> retrieves the entire Page entities.<br />
<strong>Return Keys Only</strong> retrieves just the keys but uses
intelligent key naming to transmit "Title" data via the key names.</p>
"""
page += '</form>'
page += html
page += '</body></html>'
self.response.out.write(page)
class MainPage(SimplePage):
def get(self):
user = users.get_current_user()
if not user:
html = '<h4>Please login to add a page.</h4>'
else:
import time
time_string = time.strftime('Page submitted %X on %x')
html = """
<h4>Add a text page below:</h4>
<form action="/" method="post">
<div>Title: <input type="text" size="40" name="title"
"""
html += 'value="' + time_string + '" />'
html += """
<em>This data will be encoded in the key names of index entities.</em></div>
<div><textarea name="content" rows="10" cols="60"></textarea></div>
<div><input type="submit" value="Add Page" /></div>
</form>
"""
self.render(html)
def post(self):
user = users.get_current_user()
content = self.request.get('content')
title = self.request.get('title')
if not user:
self.redirect('/?msg=You+must+be+logged+in')
elif not content:
self.redirect('/')
else:
page = Page(content=content, title=title, user=user)
page.put()
page.enqueue_indexing(url=INDEXING_URL)
html = "<div>Thanks for entering the following text:</div>"
html += "<pre>%s</pre>" % (cgi.escape(content))
self.render(html)
class SearchPage(SimplePage):
def get(self):
submitbtn = self.request.get('submitbtn')
phrase = self.request.get('phrase')
html = "<h4>'" + phrase + "' was found on these pages:</h4>"
if submitbtn == 'Return Keys Only':
key_list = Page.search(phrase, keys_only=True)
for key_and_title in key_list:
html += "<div><p>Title: %s</p></div>" % key_and_title[1]
else:
pages = Page.search(phrase)
for page in pages:
html += "<div><p>Title: %s</p><p>User: %s, Created: %s</p><pre>%s</pre></div>" \
% (page.title, str(page.user), str(page.created), cgi.escape(page.content))
self.render(html)
application = webapp.WSGIApplication([
('/', MainPage),
('/search', SearchPage),
(INDEXING_URL, search.SearchIndexing)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()