| 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. |
| (...skipping 11 matching lines...) Show 10 above Show 10 below | |
| 25 Limitations: | 23 Limitations: |
| 26 - all user manipulation methods are not available | 24 - all user manipulation methods are not available |
| 27 - all user permissions methods are not available | 25 - all user permissions methods are not available |
| 28 - user.get_profile method not implemented | 26 - user.get_profile method not implemented |
| 29 - db.UserProperty returns user.User instead of DjangoUser | 27 - db.UserProperty returns user.User instead of DjangoUser |
| 30 """ | 28 """ |
| 31 | 29 |
| 32 from datetime import datetime | 30 from datetime import datetime |
| 33 import urllib | 31 import urllib |
| 34 | 32 |
| 35 from django.core import mail | |
| 36 from django.db.models.manager import EmptyManager | 33 from django.db.models.manager import EmptyManager |
| 37 from django.http import HttpResponseRedirect | 34 from django.http import HttpResponseRedirect |
| 38 from django.template import Node | 35 from django.template import Node |
| 39 from django.utils.encoding import smart_str | 36 from django.utils.encoding import smart_str |
| 40 | 37 |
| 41 from google.appengine.api import datastore_types | 38 from google.appengine.api import datastore_types |
| 39 from google.appengine.api import mail | |
| 42 from google.appengine.api import users | 40 from google.appengine.api import users |
| 43 from google.appengine.ext.webapp import template | 41 from google.appengine.ext.webapp import template |
| 44 | 42 |
| 45 from appengine_django import models | 43 from appengine_django import models |
| 46 | 44 |
| 47 register = template.create_template_register() | 45 register = template.create_template_register() |
| 48 template.register_template_library("appengine_django.auth") | 46 template.register_template_library("appengine_django.auth") |
| 49 | 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
| |
| 50 | |
| 51 class CallableString(str): | 48 class CallableString(str): |
| 52 """String subclass that returns the string if it's called as a function. | 49 """String subclass that returns the string if it's called as a function. |
| 53 This class is required for the user's email attribute. | 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
| |
| 54 """ | |
| 55 def __call__(self): | 51 def __call__(self): |
| 56 return str(self) | 52 return str(self) |
| 57 | 53 |
| 58 | 54 |
| 59 class DjangoUser(users.User): | 55 class DjangoUser(users.User): |
| 60 """App Engine User subclass that mimics the behavior of an Django 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
| |
| 61 | 57 |
| 62 This class is added to datastore_types when importing this module | 58 This class is added to datastore_types when importing this module |
| 63 to make it usable for db.UserProperty(). | 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
| |
| 64 """ | 60 """ |
| 65 id = None | 61 id = None |
| 66 is_active = True | 62 is_active = True |
| 67 first_name = None | 63 first_name = None |
| 68 last_name = None | 64 last_name = None |
| 69 password = None | 65 password = None |
| 70 _groups = EmptyManager() | 66 _groups = EmptyManager() |
| 71 _user_permissions = EmptyManager() | 67 _user_permissions = EmptyManager() |
| 72 | 68 |
| 73 def __init__(self, *args, **kw): | 69 def __init__(self, *args, **kw): |
| (...skipping 71 matching lines...) Show 10 above Show 10 below | |
| 145 def is_authenticated(self): | 141 def is_authenticated(self): |
| 146 """Always return True""" | 142 """Always return True""" |
| 147 return True | 143 return True |
| 148 | 144 |
| 149 def get_absolute_url(self): | 145 def get_absolute_url(self): |
| 150 return "/users/%s/" % urllib.quote(smart_str(self.username)) | 146 return "/users/%s/" % urllib.quote(smart_str(self.username)) |
| 151 | 147 |
| 152 def get_full_name(self): | 148 def get_full_name(self): |
| 153 return "" | 149 return "" |
| 154 | 150 |
| 155 def email_user(self, subject, message, from_email): | 151 def email_user(self, subject, message, from_email=None): |
| 156 """Sends an email to this user. | 152 """Sends an email to this user""" |
| 157 | 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
| |
| 158 According to the App Engine email API the from_email must be the | 154 to=self.email, |
| 159 email address of a registered administrator for the application. | 155 subject=subject, |
| 160 """ | 156 message=message) |
| 161 mail.send_mail(subject, | |
| 162 message, | |
| 163 from_email, | |
| 164 [self.email]) | |
| 165 | 157 |
| 166 def get_profile(self): | 158 def get_profile(self): |
| 167 raise NotImplementedError | 159 raise NotImplementedError |
| 168 datastore_types._PROPERTY_TYPES.append(DjangoUser) | 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
| |
| 169 | 161 |
| 170 | 162 |
| 171 def login_required(function): | 163 def login_required(function): |
| 172 """Implementation of Django's login_required decorator. | 164 """Implementation of Django's login_required decorator. |
| 173 | 165 |
| 174 The login redirect URL is always set to request.path | 166 The login redirect URL is always set to request.path |
| 175 """ | 167 """ |
| 176 def login_required_wrapper(request, *args, **kw): | 168 def login_required_wrapper(request, *args, **kw): |
| 177 if request.user.is_authenticated(): | 169 if request.user.is_authenticated(): |
| 178 return function(request, *args, **kw) | 170 return function(request, *args, **kw) |
| 179 return HttpResponseRedirect(users.create_login_url(request.path)) | 171 return HttpResponseRedirect(users.create_login_url(request.path)) |
| 180 return login_required_wrapper | 172 return login_required_wrapper |
| 181 | 173 |
| 182 | 174 |
| 183 class AuthLoginUrlsNode(Node): | 175 class AuthLoginUrlsNode(Node): |
| 184 """Template node that creates an appengine login or logout URL. | 176 """Template node that creates an appengine login or logout URL. |
| 185 | 177 |
| 186 If create_login_url is True the appengine's login URL is rendered into | 178 If create_login_url is True the appengine's login URL is rendered into |
| 187 the template, otherwise the logout URL. | 179 the template, otherwise the logout URL. |
| 188 """ | 180 """ |
| 189 def __init__(self, create_login_url, redirect): | 181 def __init__(self, create_login_url, redirect): |
| 190 self.redirect = redirect | 182 self.redirect = redirect |
| 191 self.create_login_url = create_login_url | 183 self.create_login_url = create_login_url |
| 192 | 184 |
| 193 def render(self, context): | 185 def render(self, context): |
| 194 if self.create_login_url: | 186 if self.create_login_url: |
| 195 return users.create_login_url(self.redirect) | 187 return users.create_login_url(self.redirect) |
| 196 else: | 188 else: |
| 197 return users.create_logout_url(self.redirect) | 189 return users.create_logout_url(self.redirect) |
| 198 | |
| 199 | 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
| |
| 200 def auth_login_urls(parser, token): | 191 def auth_login_urls(parser, token): |
| 201 """Template tag registered as 'auth_login_url' and 'auth_logout_url' | 192 """Template tag registered as 'auth_login_url' and 'auth_logout_url' |
| 202 when the module is imported. | 193 when the module is imported. |
| 203 | 194 |
| 204 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 |
| 205 defaults to '/'. | 196 defaults to '/'. |
| 206 """ | 197 """ |
| 207 bits = list(token.split_contents()) | 198 bits = list(token.split_contents()) |
| 208 if len(bits) == 2: | 199 if len(bits) == 2: |
| 209 redirect = bits[1] | 200 redirect = bits[1] |
| 210 else: | 201 else: |
| 211 redirect = "/" | 202 redirect = "/" |
| 212 login = bits[0] == "auth_login_url" | 203 login = bits[0] == "auth_login_url" |
| 213 return AuthLoginUrlsNode(login, redirect) | 204 return AuthLoginUrlsNode(login, redirect) |
| 214 register.tag("auth_login_url", auth_login_urls) | 205 register.tag("auth_login_url", auth_login_urls) |
| 215 register.tag("auth_logout_url", auth_login_urls) | 206 register.tag("auth_logout_url", auth_login_urls) |
| LEFT | RIGHT |