From af275146a3e43baba68f7f79baf5aeaaf2350f4c Mon Sep 17 00:00:00 2001 From: "amyu@google.com" Date: Thu, 3 Jan 2013 05:17:15 +0000 Subject: [PATCH] update to use py27 and webapp2 git-svn-id: http://google-app-engine-samples.googlecode.com/svn/trunk@168 99225164-8649-0410-878b-2ba91e509939 --- search/python/app.yaml | 11 +++++---- search/python/appengine_config.py | 9 ------- search/python/search_demo.py | 40 ++++++++++++++++---------------- search/python/{ => templates}/index.html | 6 ++--- 4 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 search/python/appengine_config.py rename search/python/{ => templates}/index.html (89%) diff --git a/search/python/app.yaml b/search/python/app.yaml index b93a6a7..82ee529 100644 --- a/search/python/app.yaml +++ b/search/python/app.yaml @@ -1,13 +1,16 @@ application: search-py-demo version: 1 -runtime: python +runtime: python27 +threadsafe: true api_version: 1 handlers: - url: /static static_dir: static -- url: /_ah/admin.* - script: $PYTHON_LIB/google/appengine/ext/admin/ - url: .* - script: search_demo.py + script: search_demo.application + +libraries: +- name: jinja2 + version: "2.6" diff --git a/search/python/appengine_config.py b/search/python/appengine_config.py deleted file mode 100644 index 1b91a79..0000000 --- a/search/python/appengine_config.py +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/python2.4 -# -# Copyright 2011 Google Inc. All Rights Reserved. - -"""App Engine Config file for setting up Django.""" - -__author__ = 'ged@google.com (Ged Ellis)' - -webapp_django_version = '1.2' diff --git a/search/python/search_demo.py b/search/python/search_demo.py index 0edd8b9..c9d8e1c 100644 --- a/search/python/search_demo.py +++ b/search/python/search_demo.py @@ -12,19 +12,29 @@ 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 -from google.appengine.ext import webapp -from google.appengine.ext.webapp import template -from google.appengine.ext.webapp.util import run_wsgi_app - _INDEX_NAME = 'greeting' -_ENCODE_TRANS_TABLE = string.maketrans('-: .@', '_____') +# _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 MainPage(webapp.RequestHandler): +class MainPage(BaseHandler): """Handles search requests for comments.""" def get(self): @@ -39,7 +49,7 @@ class MainPage(webapp.RequestHandler): expr_list = [search.SortExpression( expression='author', default_value='', direction=search.SortExpression.DESCENDING)] - # construct the sort options + # construct the sort options sort_opts = search.SortOptions( expressions=expr_list) query_options = search.QueryOptions( @@ -47,7 +57,6 @@ class MainPage(webapp.RequestHandler): sort_options=sort_opts) query_obj = search.Query(query_string=query, options=query_options) results = search.Index(name=_INDEX_NAME).search(query=query_obj) - if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' @@ -61,9 +70,7 @@ class MainPage(webapp.RequestHandler): 'url': url, 'url_linktext': url_linktext, } - - path = os.path.join(os.path.dirname(__file__), 'index.html') - self.response.out.write(template.render(path, template_values)) + self.render_template('index.html', template_values) def CreateDocument(author, content): @@ -79,7 +86,7 @@ def CreateDocument(author, content): search.DateField(name='date', value=datetime.now().date())]) -class Comment(webapp.RequestHandler): +class Comment(BaseHandler): """Handles requests to index comments.""" def post(self): @@ -100,14 +107,7 @@ class Comment(webapp.RequestHandler): self.redirect('/') -application = webapp.WSGIApplication( +application = webapp2.WSGIApplication( [('/', MainPage), ('/sign', Comment)], debug=True) - - -def main(): - run_wsgi_app(application) - -if __name__ == '__main__': - main() diff --git a/search/python/index.html b/search/python/templates/index.html similarity index 89% rename from search/python/index.html rename to search/python/templates/index.html index 62df82d..99920f2 100644 --- a/search/python/index.html +++ b/search/python/templates/index.html @@ -5,7 +5,7 @@ Search Demonstration App - +
Search Demo
@@ -17,8 +17,8 @@ {{number_returned}} of {{results.number_found}} comments found

{% for scored_document in results %} - {% for field in scored_document.fields %} - {{field.value}}   + {% for f in scored_document.fields %} + {{f.value}}   {% endfor %}

{% endfor %} -- 2.11.4.GIT