| LEFT | RIGHT |
|---|---|
| 1 # Copyright 2008 Google Inc. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 1 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
mattbrown.nz
2008/05/09 10:40:20
You should probably add an Author or Copyright lin
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> You
| |
| 4 # you may not use this file except in compliance with the License. | 2 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 3 # You may obtain a copy of the License at |
| 6 # | 4 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 5 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 6 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 7 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 8 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 10 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 11 # limitations under the License. |
| 14 | 12 |
| 15 """ | 13 """ |
| 16 Authentication module that mimics the behavior of Django's authentication | 14 Authentication module that mimics the behavior of Django's authentication |
| 17 implementation. | 15 implementation. |
| 18 | 16 |
| 17 To use this authentication module in an Appengine/Django project: | |
| 18 - add 'appengine_django.auth.middleware.AuthenticationMiddleware' | |
| 19 to MIDDLEWARE_CLASSES | |
| 20 - add 'appengine_django.auth.context_processors.auth' | |
| 21 to TEMPLATE_CONTEXT_PROCESSORS | |
| 22 | |
| 19 Limitations: | 23 Limitations: |
| 20 - all user permissions methods are not available (requires contenttypes) | 24 - all user manipulation methods are not available |
| 25 - all user permissions methods are not available | |
| 26 - user.get_profile method not implemented | |
| 27 - db.UserProperty returns user.User instead of DjangoUser | |
| 21 """ | 28 """ |
| 22 | 29 |
| 30 from datetime import datetime | |
| 31 import urllib | |
| 32 | |
| 33 from django.db.models.manager import EmptyManager | |
| 23 from django.http import HttpResponseRedirect | 34 from django.http import HttpResponseRedirect |
| 24 from django.template import Node | 35 from django.template import Node |
| 25 | 36 from django.utils.encoding import smart_str |
| 37 | |
| 38 from google.appengine.api import datastore_types | |
| 39 from google.appengine.api import mail | |
| 26 from google.appengine.api import users | 40 from google.appengine.api import users |
| 27 from google.appengine.ext.webapp import template | 41 from google.appengine.ext.webapp import template |
| 28 | 42 |
| 43 from appengine_django import models | |
| 44 | |
| 29 register = template.create_template_register() | 45 register = template.create_template_register() |
| 30 template.register_template_library("appengine_django.auth") | 46 template.register_template_library("appengine_django.auth") |
| 47 | |
|
mattbrown.nz
2008/05/09 10:40:20
add extra blank line
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> add
| |
| 48 class CallableString(str): | |
| 49 """String subclass that returns the string if it's called as a function. | |
| 50 This class is required for the user's email attribute.""" | |
|
mattbrown.nz
2008/05/09 10:40:20
closing """ on next line.
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> clos
| |
| 51 def __call__(self): | |
| 52 return str(self) | |
| 53 | |
| 54 | |
| 55 class DjangoUser(users.User): | |
| 56 """Appengine User subclass that mimics the behavior of an Django user. | |
|
mattbrown.nz
2008/05/09 10:40:20
App Engine (nitpick)
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> App
| |
| 57 | |
| 58 This class is added to datastore_types when importing this module | |
| 59 to make it usable for db.UserProperty(). | |
|
mattbrown.nz
2008/05/09 10:40:20
I don't believe this is possible. See further comm
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> I do
| |
| 60 """ | |
| 61 id = None | |
| 62 is_active = True | |
| 63 first_name = None | |
| 64 last_name = None | |
| 65 password = None | |
| 66 _groups = EmptyManager() | |
| 67 _user_permissions = EmptyManager() | |
| 68 | |
| 69 def __init__(self, *args, **kw): | |
| 70 super(DjangoUser, self).__init__(*args, **kw) | |
| 71 self.email = CallableString(self._User__email) | |
| 72 | |
| 73 def __unicode__(self): | |
| 74 return self.username | |
| 75 | |
| 76 def __str__(self): | |
| 77 return unicode(self).encode('utf-8') | |
| 78 | |
| 79 def _get_username(self): | |
| 80 return self.nickname() | |
| 81 username = property(fget=_get_username) | |
| 82 | |
| 83 def _is_superuser(self): | |
| 84 return users.is_current_user_admin() | |
| 85 is_superuser = property(fget=_is_superuser) | |
| 86 is_staff = property(fget=_is_superuser) | |
| 87 | |
| 88 def _default_now(self): | |
| 89 return datetime.now() | |
| 90 last_login = property(fget=_default_now) | |
| 91 date_joined = property(fget=_default_now) | |
| 92 | |
| 93 def save(self): | |
| 94 raise NotImplementedError | |
| 95 | |
| 96 def delete(self): | |
| 97 raise NotImplementedError | |
| 98 | |
| 99 def set_password(self, raw_password): | |
| 100 raise NotImplementedError | |
| 101 | |
| 102 def check_password(self, raw_password): | |
| 103 raise NotImplementedError | |
| 104 | |
| 105 def set_unusable_password(self): | |
| 106 raise NotImplementedError | |
| 107 | |
| 108 def has_usable_password(self): | |
| 109 raise NotImplementedError | |
| 110 | |
| 111 def _get_groups(self): | |
| 112 return self._groups | |
| 113 groups = property(_get_groups) | |
| 114 | |
| 115 def _get_user_permissions(self): | |
| 116 return self._user_permissions | |
| 117 user_permissions = property(_get_user_permissions) | |
| 118 | |
| 119 def get_group_permissions(self): | |
| 120 return self._user_permissions | |
| 121 | |
| 122 def get_all_permissions(self): | |
| 123 return self._user_permissions | |
| 124 | |
| 125 def has_perm(self, perm): | |
| 126 return False | |
| 127 | |
| 128 def has_perms(self, perm_list): | |
| 129 return False | |
| 130 | |
| 131 def has_module_perms(self, module): | |
| 132 return False | |
| 133 | |
| 134 def get_and_delete_messages(self): | |
| 135 return [] | |
| 136 | |
| 137 def is_anonymous(self): | |
| 138 """Always return False""" | |
| 139 return False | |
| 140 | |
| 141 def is_authenticated(self): | |
| 142 """Always return True""" | |
| 143 return True | |
| 144 | |
| 145 def get_absolute_url(self): | |
| 146 return "/users/%s/" % urllib.quote(smart_str(self.username)) | |
| 147 | |
| 148 def get_full_name(self): | |
| 149 return "" | |
| 150 | |
| 151 def email_user(self, subject, message, from_email=None): | |
| 152 """Sends an email to this user""" | |
| 153 mail.send_mail(sender=from_email, | |
|
mattbrown.nz
2008/05/09 10:40:20
The App Engine email API requires from_email to be
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> The
| |
| 154 to=self.email, | |
| 155 subject=subject, | |
| 156 message=message) | |
| 157 | |
| 158 def get_profile(self): | |
| 159 raise NotImplementedError | |
| 160 datastore_types._PROPERTY_TYPES.append(DjangoUser) | |
|
mattbrown.nz
2008/05/09 10:40:20
model properties need to be descended from db.Prop
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> mode
mattbrown.nz
2008/05/11 03:01:20
On 2008/05/09 20:23:23, aalbrecht wrote:
> 975, b
aalbrecht
2008/05/13 14:20:24
The DjangoUser class is now a model. I think this
| |
| 31 | 161 |
| 32 | 162 |
| 33 def login_required(function): | 163 def login_required(function): |
| 34 """Implementation of Django's login_required decorator. | 164 """Implementation of Django's login_required decorator. |
| 35 | 165 |
| 36 The login redirect URL is always set to request.path | 166 The login redirect URL is always set to request.path |
| 37 """ | 167 """ |
| 38 def login_required_wrapper(request, *args, **kw): | 168 def login_required_wrapper(request, *args, **kw): |
| 39 if request.user.is_authenticated(): | 169 if request.user.is_authenticated(): |
| 40 return function(request, *args, **kw) | 170 return function(request, *args, **kw) |
| 41 return HttpResponseRedirect(users.create_login_url(request.path)) | 171 return HttpResponseRedirect(users.create_login_url(request.path)) |
| 42 return login_required_wrapper | 172 return login_required_wrapper |
| 43 | 173 |
| 44 | 174 |
| 45 class AuthLoginUrlsNode(Node): | 175 class AuthLoginUrlsNode(Node): |
| 46 """Template node that creates an App Engine login or logout URL. | 176 """Template node that creates an appengine login or logout URL. |
| 47 | 177 |
| 48 If create_login_url is True the App Engine's login URL is rendered into | 178 If create_login_url is True the appengine's login URL is rendered into |
| 49 the template, otherwise the logout URL. | 179 the template, otherwise the logout URL. |
| 50 """ | 180 """ |
| 51 def __init__(self, create_login_url, redirect): | 181 def __init__(self, create_login_url, redirect): |
| 52 self.redirect = redirect | 182 self.redirect = redirect |
| 53 self.create_login_url = create_login_url | 183 self.create_login_url = create_login_url |
| 54 | 184 |
| 55 def render(self, context): | 185 def render(self, context): |
| 56 if self.create_login_url: | 186 if self.create_login_url: |
| 57 return users.create_login_url(self.redirect) | 187 return users.create_login_url(self.redirect) |
| 58 else: | 188 else: |
| 59 return users.create_logout_url(self.redirect) | 189 return users.create_logout_url(self.redirect) |
| 60 | |
| 61 | 190 |
|
mattbrown.nz
2008/05/09 10:40:20
add blank line
aalbrecht
2008/05/09 20:23:23
On 2008/05/09 10:40:20, mattbrown.nz wrote:
> add
| |
| 62 def auth_login_urls(parser, token): | 191 def auth_login_urls(parser, token): |
| 63 """Template tag registered as 'auth_login_url' and 'auth_logout_url' | 192 """Template tag registered as 'auth_login_url' and 'auth_logout_url' |
| 64 when the module is imported. | 193 when the module is imported. |
| 65 | 194 |
| 66 Both tags take an optional argument that specifies the redirect URL and | 195 Both tags take an optional argument that specifies the redirect URL and |
| 67 defaults to '/'. | 196 defaults to '/'. |
| 68 """ | 197 """ |
| 69 bits = list(token.split_contents()) | 198 bits = list(token.split_contents()) |
| 70 if len(bits) == 2: | 199 if len(bits) == 2: |
| 71 redirect = bits[1] | 200 redirect = bits[1] |
| 72 else: | 201 else: |
| 73 redirect = "/" | 202 redirect = "/" |
| 74 login = bits[0] == "auth_login_url" | 203 login = bits[0] == "auth_login_url" |
| 75 return AuthLoginUrlsNode(login, redirect) | 204 return AuthLoginUrlsNode(login, redirect) |
| 76 register.tag("auth_login_url", auth_login_urls) | 205 register.tag("auth_login_url", auth_login_urls) |
| 77 register.tag("auth_logout_url", auth_login_urls) | 206 register.tag("auth_logout_url", auth_login_urls) |
| LEFT | RIGHT |