Introduce dynamic scope_path regexps
Instead of relying on scope_path's being "one slash deep", we should
instead allow for either:
1. scope_paths that have a pre-defined depth
2. scope_paths that can be arbitrarily deep
We achieve 1 by setting an entities scope_logic to another logic
module. We then recursively call getScopeDepth until we get to the
topmost entity (that is, an unscoped entity).
A little different is the solution to 2, since some entities can have
an arbitrarily deep scope (such as Documents), we need to have some
way of signaling this to getScopePattern. A clean solution is to
return None, rather than a number. If None is returned, the
SCOPE_PATH_ARG_PATTERN is returned as regexp instead, which will
match an arbitrarily deeply nested scope.
The solution for 2 requires that we return None somewhere in the
scope_logic chain, the most straight forward method to do so is to
override getScopeDepth anywhere such a scope is needed and make it
return None. A more elegant solution however, is to set the
scope_logic to that module in all entities that require it.
Patch by: Sverre Rabbelier
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Trivial implementation of the UserService."""
import os
import urllib
import urlparse
from google.appengine.api import user_service_pb
_DEFAULT_LOGIN_URL = 'https://www.google.com/accounts/Login?continue=%s'
_DEFAULT_LOGOUT_URL = 'https://www.google.com/accounts/Logout?continue=%s'
class UserServiceStub(object):
"""Trivial implementation of the UserService."""
def __init__(self,
login_url=_DEFAULT_LOGIN_URL,
logout_url=_DEFAULT_LOGOUT_URL):
"""Initializer.
Args:
login_url: String containing the URL to use for logging in.
logout_url: String containing the URL to use for logging out.
Note: Both the login_url and logout_url arguments must contain one format
parameter, which will be replaced with the continuation URL where the user
should be redirected after log-in or log-out has been completed.
"""
self.__num_requests = 0
self._login_url = login_url
self._logout_url = logout_url
os.environ['AUTH_DOMAIN'] = 'gmail.com'
def num_requests(self):
return self.__num_requests
def MakeSyncCall(self, service, call, request, response):
"""The apiproxy entry point.
Args:
service: must be 'user'
call: string representing which function to call
request: the URL to redirect to, a base.StringProto
response: the URL, a base.StringProto
Currently, CreateLoginURL and CreateLogoutURL are supported.
"""
assert service == 'user'
method = getattr(self, "_Dynamic_" + call)
method(request, response)
def _Dynamic_CreateLoginURL(self, request, response):
"""Trivial implementation of UserService.CreateLoginURL().
Args:
request: the URL to redirect to after login; a base.StringProto
response: the login URL; a base.StringProto
"""
self.__num_requests += 1
response.set_value(
self._login_url %
urllib.quote(self._AddHostToContinueURL(request.value())))
def _Dynamic_CreateLogoutURL(self, request, response):
"""Trivial implementation of UserService.CreateLogoutURL().
Args:
request: the URL to redirect to after logout; a base.StringProto
response: the logout URL; a base.StringProto
"""
self.__num_requests += 1
response.set_value(
self._logout_url %
urllib.quote(self._AddHostToContinueURL(request.value())))
def _AddHostToContinueURL(self, continue_url):
"""Adds the request host to the continue url if no host is specified.
Args:
continue_url: the URL which may or may not have a host specified
Returns:
string
"""
(protocol, host, path, parameters, query, fragment) = urlparse.urlparse(continue_url, 'http')
if host:
return continue_url
host = os.environ['SERVER_NAME']
if os.environ['SERVER_PORT'] != '80':
host = host + ":" + os.environ['SERVER_PORT']
if path == '':
path = '/'
return urlparse.urlunparse(
(protocol, host, path, parameters, query, fragment))