add demonstration of geosearch
[gae-samples.git] / guestbook_namespaces / appengine_config.py
blobcf9b75a8de3816fc0b856bb32d4611a0c85e3ce1
1 #!/usr/bin/python2.4
3 # Copyright 2010 Google Inc. All Rights Reserved.
5 """
6 Manages the namespace for the application.
8 This file presents ways an ISV (Independent Software Vendor) might use
9 namespaces to distribute the guestbook application to different corporate
10 clients. The original guestbook.py is left unchanged. Our namespace choosing
11 hook is run when datastore or memcache attempt to resolve the namespace.
12 When defined in appengine_config.py the lib_config mechanism substitutes this
13 function for the default definition which returns None. This hopefully shows how
14 easy it can be to make an existing app namespace aware.
16 Setting _NAMESPACE_PICKER has the following effects:
18 If _USE_SERVER_NAME, we read the server name
19 foo.guestbook-isv.appspot.com and set the namespace.
21 If _USE_GOOGLE_APPS_DOMAIN, we allow the namespace manager to infer the
22 namespace from the request.
24 If _USE_COOKIE, then the ISV might have a gateway page that sets a cookie called
25 'namespace' for example, and we read this cookie and set the namespace to its
26 value. Note this is not a secure use of cookies.
28 Other possibilities not implemented here include using a mapping from user to
29 namespace and possibly setting a namespace cookie from this mapping. If the
30 mapping is stored in datastore, we would probably not wish to look it up on
31 every query.
32 """
34 __author__ = 'nverne@google.com (Nicholas Verne)'
36 import Cookie
37 import os
39 from google.appengine.api import namespace_manager
41 _USE_SERVER_NAME = 0
42 _USE_GOOGLE_APPS_DOMAIN = 1
43 _USE_COOKIE = 2
45 _NAMESPACE_PICKER = _USE_SERVER_NAME
47 def namespace_manager_default_namespace_for_request():
48 """Determine which namespace is to be used for a request.
50 The value of _NAMESPACE_PICKER has the following effects:
52 If _USE_SERVER_NAME, we read server name
53 foo.guestbook-isv.appspot.com and set the namespace.
55 If _USE_GOOGLE_APPS_DOMAIN, we allow the namespace manager to infer
56 the namespace from the request.
58 If _USE_COOKIE, then the ISV might have a gateway page that sets a
59 cookie called 'namespace', and we set the namespace to the cookie's value
60 """
61 name = None
63 if _NAMESPACE_PICKER == _USE_SERVER_NAME:
64 name = os.environ['SERVER_NAME']
65 elif _NAMESPACE_PICKER == _USE_GOOGLE_APPS_DOMAIN:
66 name = namespace_manager.google_apps_namespace()
67 elif _NAMESPACE_PICKER == _USE_COOKIE:
68 cookies = os.environ.get('HTTP_COOKIE', None)
69 if cookies:
70 name = Cookie.BaseCookie(cookies).get('namespace')
72 return name