~nchohan/appscale/zk3.3.4

« back to all changes in this revision

Viewing changes to AppServer/google/appengine/ext/webapp/util.py

  • Committer: Navraj Chohan
  • Date: 2009-03-28 01:14:04 UTC
  • Revision ID: nchohan@cs.ucsb.edu-20090328011404-42m1w6yt60m6yfg3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2007 Google Inc.
 
4
#
 
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
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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.
 
16
#
 
17
 
 
18
"""Convience functions for the Webapp framework."""
 
19
 
 
20
 
 
21
 
 
22
 
 
23
 
 
24
__all__ = ["login_required", "run_wsgi_app"]
 
25
 
 
26
import os
 
27
import sys
 
28
import wsgiref.util
 
29
 
 
30
from google.appengine.api import users
 
31
from google.appengine.ext import webapp
 
32
 
 
33
 
 
34
def login_required(handler_method):
 
35
  """A decorator to require that a user be logged in to access a handler.
 
36
 
 
37
  To use it, decorate your get() method like this:
 
38
 
 
39
    @login_required
 
40
    def get(self):
 
41
      user = users.get_current_user(self)
 
42
      self.response.out.write('Hello, ' + user.nickname())
 
43
 
 
44
  We will redirect to a login page if the user is not logged in. We always
 
45
  redirect to the request URI, and Google Accounts only redirects back as a GET
 
46
  request, so this should not be used for POSTs.
 
47
  """
 
48
  def check_login(self, *args):
 
49
    if self.request.method != 'GET':
 
50
      raise webapp.Error('The check_login decorator can only be used for GET '
 
51
                         'requests')
 
52
    user = users.get_current_user()
 
53
    if not user:
 
54
      self.redirect(users.create_login_url(self.request.uri))
 
55
      return
 
56
    else:
 
57
      handler_method(self, *args)
 
58
  return check_login
 
59
 
 
60
 
 
61
def run_wsgi_app(application):
 
62
  """Runs your WSGI-compliant application object in a CGI environment.
 
63
 
 
64
  Compared to wsgiref.handlers.CGIHandler().run(application), this
 
65
  function takes some shortcuts.  Those are possible because the
 
66
  app server makes stronger promises than the CGI standard.
 
67
  """
 
68
  env = dict(os.environ)
 
69
  env["wsgi.input"] = sys.stdin
 
70
  env["wsgi.errors"] = sys.stderr
 
71
  env["wsgi.version"] = (1, 0)
 
72
  env["wsgi.run_once"] = True
 
73
  env["wsgi.url_scheme"] = wsgiref.util.guess_scheme(env)
 
74
  env["wsgi.multithread"] = False
 
75
  env["wsgi.multiprocess"] = False
 
76
  result = application(env, _start_response)
 
77
  if result is not None:
 
78
    for data in result:
 
79
      sys.stdout.write(data)
 
80
 
 
81
 
 
82
def _start_response(status, headers, exc_info=None):
 
83
  """A start_response() callable as specified by PEP 333"""
 
84
  if exc_info is not None:
 
85
    raise exc_info[0], exc_info[1], exc_info[2]
 
86
  print "Status: %s" % status
 
87
  for name, val in headers:
 
88
    print "%s: %s" % (name, val)
 
89
  print
 
90
  return sys.stdout.write