add demonstration of geosearch
[gae-samples.git] / search / product_search_python / base_handler.py
blob12078b7d628f583000757c9115f117c1a33530bf
1 #!/usr/bin/env python
3 # Copyright 2012 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """ The base request handler class.
18 """
21 import webapp2
22 from webapp2_extras import jinja2
23 import json
25 from google.appengine.api import users
28 class BaseHandler(webapp2.RequestHandler):
29 """The other handlers inherit from this class. Provides some helper methods
30 for rendering a template and generating template links."""
32 @classmethod
33 def logged_in(cls, handler_method):
34 """
35 This decorator requires a logged-in user, and returns 403 otherwise.
36 """
37 def auth_required(self, *args, **kwargs):
38 if (users.get_current_user() or
39 self.request.headers.get('X-AppEngine-Cron')):
40 handler_method(self, *args, **kwargs)
41 else:
42 self.error(403)
43 return auth_required
45 @webapp2.cached_property
46 def jinja2(self):
47 return jinja2.get_jinja2(app=self.app)
49 def render_template(self, filename, template_args):
50 template_args.update(self.generateSidebarLinksDict())
51 self.response.write(self.jinja2.render_template(filename, **template_args))
53 def render_json(self, response):
54 self.response.write("%s(%s);" % (self.request.GET['callback'],
55 json.dumps(response)))
57 def getLoginLink(self):
58 """Generate login or logout link and text, depending upon the logged-in
59 status of the client."""
60 if users.get_current_user():
61 url = users.create_logout_url(self.request.uri)
62 url_linktext = 'Logout'
63 else:
64 url = users.create_login_url(self.request.uri)
65 url_linktext = 'Login'
66 return (url, url_linktext)
68 def getAdminManageLink(self):
69 """Build link to the admin management page, if the user is logged in."""
70 if users.get_current_user():
71 admin_url = '/admin/manage'
72 return (admin_url, 'Admin/Add sample data')
73 else:
74 return (None, None)
76 def createProductAdminLink(self):
77 if users.get_current_user():
78 admin_create_url = '/admin/create_product'
79 return (admin_create_url, 'Create new product (admin)')
80 else:
81 return (None, None)
83 def generateSidebarLinksDict(self):
84 """Build a dict containing login/logout and admin links, which will be
85 included in the sidebar for all app pages."""
87 url, url_linktext = self.getLoginLink()
88 admin_create_url, admin_create_text = self.createProductAdminLink()
89 admin_url, admin_text = self.getAdminManageLink()
90 return {
91 'admin_create_url': admin_create_url,
92 'admin_create_text': admin_create_text,
93 'admin_url': admin_url,
94 'admin_text': admin_text,
95 'url': url,
96 'url_linktext': url_linktext