| LEFT | RIGHT |
|---|---|
| 1 # Copyright 2008 Google Inc. | 1 # Copyright 2008 Google Inc. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 13 # limitations under the License. |
| 14 | 14 |
| 15 """ | 15 """ |
| 16 DjangoUser model for authentication framework | 16 DjangoUser model for authentication framework |
| 17 """ | 17 """ |
| 18 | 18 |
| 19 from django.contrib.auth.models import SiteProfileNotAvailable | 19 from django.contrib.auth.models import SiteProfileNotAvailable |
| 20 from django.core import mail | 20 from django.core import mail |
| 21 from django.core.exceptions import ImproperlyConfigured | 21 from django.core.exceptions import ImproperlyConfigured |
| 22 from django.db import models | 22 from django.db import models |
| 23 from django.db.models.manager import EmptyManager | 23 from django.db.models.manager import EmptyManager |
| 24 | 24 |
| 25 from google.appengine.api import users | 25 from google.appengine.api import users |
| 26 from google.appengine.ext import db | 26 from google.appengine.ext import db |
| 27 | 27 |
| 28 from appengine_django.models import BaseModel | 28 from appengine_django.models import BaseModel |
| 29 | 29 |
| 30 | 30 |
| 31 class DjangoUser(BaseModel): | 31 class DjangoUser(BaseModel): |
| 32 """A model the same attributes and method as a Django user model. | 32 """A model the same attributes and method as a Django user model. |
| 33 | 33 |
| 34 The model has two additions. The first addition is a 'user' attribute | 34 The model has two additions. The first addition is a 'user' attribute |
| 35 which references a App Engine user. The second is the | 35 which references a App Engine user. The second is the |
| 36 'get_djangouser_for_user' classmethod that should be used to retrieve | 36 'get_djangouser_for_user' classmethod that should be used to retrieve |
| 37 a DjangoUser instance from a App Engine user object. | 37 a DjangoUser instance from a App Engine user object. |
| 38 """ | 38 """ |
| 39 user = db.UserProperty(required=True) | 39 user = db.UserProperty(required=True) |
| 40 username = db.StringProperty(required=True) | 40 username = db.StringProperty(required=True) |
| 41 first_name = db.StringProperty() | 41 first_name = db.StringProperty() |
| 42 last_name = db.StringProperty() | 42 last_name = db.StringProperty() |
| 43 email = db.EmailProperty() | 43 email = db.EmailProperty() |
| 44 password = db.StringProperty() | 44 password = db.StringProperty() |
| 45 is_staff = db.BooleanProperty(default=False, required=True) | 45 is_staff = db.BooleanProperty(default=False, required=True) |
| 46 is_active = db.BooleanProperty(default=True, required=True) | 46 is_active = db.BooleanProperty(default=True, required=True) |
| 47 is_superuser = db.BooleanProperty(default=False, required=True) | 47 is_superuser = db.BooleanProperty(default=False, required=True) |
| 48 is_staff = db.BooleanProperty(default=False, required=True) | 48 is_staff = db.BooleanProperty(default=False, required=True) |
| 49 last_login = db.DateTimeProperty(auto_now_add=True, required=True) | 49 last_login = db.DateTimeProperty(auto_now_add=True, required=True) |
| 50 date_joined = db.DateTimeProperty(auto_now_add=True, required=True) | 50 date_joined = db.DateTimeProperty(auto_now_add=True, required=True) |
| 51 groups = EmptyManager() | 51 groups = EmptyManager() |
| 52 user_permissions = EmptyManager() | 52 user_permissions = EmptyManager() |
| 53 | 53 |
| 54 def __unicode__(self): | 54 def __unicode__(self): |
| 55 return self.username | 55 return self.username |
| 56 | 56 |
| 57 def __str__(self): | 57 def __str__(self): |
| 58 return unicode(self).encode('utf-8') | 58 return unicode(self).encode('utf-8') |
| 59 | 59 |
| 60 @classmethod | 60 @classmethod |
| 61 def get_djangouser_for_user(cls, user): | 61 def get_djangouser_for_user(cls, user): |
| 62 email = user.email() | |
| 63 assert email | |
| 64 nickname = user.nickname() | |
| 65 if '@' in nickname: | |
| 66 nickname = nickname.split('@', 1)[0] | |
| 67 assert nickname | |
|
mattbrown.nz
2008/05/18 11:23:43
These statements are only used to create a user, s
aalbrecht
2008/05/18 19:17:47
On 2008/05/18 11:23:43, mattbrown.nz wrote:
> Thes
| |
| 62 query = cls.all().filter("user =", user) | 68 query = cls.all().filter("user =", user) |
| 63 if query.count() == 0: | 69 if query.count() == 0: |
| 64 django_user = cls(user=user, email=user.email(), username=user.nickname()) | 70 django_user = cls(user=user, email=email, username=nickname) |
| 65 django_user.save() | 71 django_user.save() |
| 66 else: | 72 else: |
| 67 django_user = query.get() | 73 django_user = query.get() |
| 68 return django_user | 74 return django_user |
| 69 | 75 |
| 70 def set_password(self, raw_password): | 76 def set_password(self, raw_password): |
| 71 raise NotImplementedError | 77 raise NotImplementedError |
| 72 | 78 |
| 73 def check_password(self, raw_password): | 79 def check_password(self, raw_password): |
| 74 raise NotImplementedError | 80 raise NotImplementedError |
| 75 | 81 |
| 76 def set_unusable_password(self): | 82 def set_unusable_password(self): |
| 77 raise NotImplementedError | 83 raise NotImplementedError |
| 78 | 84 |
| 79 def has_usable_password(self): | 85 def has_usable_password(self): |
| 80 raise NotImplementedError | 86 raise NotImplementedError |
| 81 | 87 |
| 82 def get_group_permissions(self): | 88 def get_group_permissions(self): |
| 83 return self.user_permissions | 89 return self.user_permissions |
| 84 | 90 |
| 85 def get_all_permissions(self): | 91 def get_all_permissions(self): |
| 86 return self.user_permissions | 92 return self.user_permissions |
| 87 | 93 |
| 88 def has_perm(self, perm): | 94 def has_perm(self, perm): |
| 89 return False | 95 return False |
| 90 | 96 |
| 91 def has_perms(self, perm_list): | 97 def has_perms(self, perm_list): |
| 92 return False | 98 return False |
| 93 | 99 |
| 94 def has_module_perms(self, module): | 100 def has_module_perms(self, module): |
| 95 return False | 101 return False |
| 96 | 102 |
| 97 def get_and_delete_messages(self): | 103 def get_and_delete_messages(self): |
| 98 return [] | 104 return [] |
| 99 | 105 |
| 100 def is_anonymous(self): | 106 def is_anonymous(self): |
| 101 """Always return False""" | 107 """Always return False""" |
| 102 return False | 108 return False |
| 103 | 109 |
| 104 def is_authenticated(self): | 110 def is_authenticated(self): |
| 105 """Always return True""" | 111 """Always return True""" |
| 106 return True | 112 return True |
| 107 | 113 |
| 108 def get_absolute_url(self): | 114 def get_absolute_url(self): |
| 109 return "/users/%s/" % urllib.quote(smart_str(self.username)) | 115 return "/users/%s/" % urllib.quote(smart_str(self.username)) |
| 110 | 116 |
| 111 def get_full_name(self): | 117 def get_full_name(self): |
| 112 full_name = u'%s %s' % (self.first_name, self.last_name) | 118 full_name = u'%s %s' % (self.first_name, self.last_name) |
| 113 return full_name.strip() | 119 return full_name.strip() |
| 114 | 120 |
| 115 def email_user(self, subject, message, from_email): | 121 def email_user(self, subject, message, from_email): |
| 116 """Sends an email to this user. | 122 """Sends an email to this user. |
| 117 | 123 |
| 118 According to the App Engine email API the from_email must be the | 124 According to the App Engine email API the from_email must be the |
| 119 email address of a registered administrator for the application. | 125 email address of a registered administrator for the application. |
| 120 """ | 126 """ |
| 121 mail.send_mail(subject, | 127 mail.send_mail(subject, |
| 122 message, | 128 message, |
| 123 from_email, | 129 from_email, |
| 124 [self.email]) | 130 [self.email]) |
| 125 | 131 |
| 126 def get_profile(self): | 132 def get_profile(self): |
| 127 """ | 133 """ |
| 128 Returns site-specific profile for this user. Raises | 134 Returns site-specific profile for this user. Raises |
| 129 SiteProfileNotAvailable if this site does not allow profiles. | 135 SiteProfileNotAvailable if this site does not allow profiles. |
| 130 | 136 |
| 131 When using the App Engine authentication framework, users are created | 137 When using the App Engine authentication framework, users are created |
| 132 automatically. | 138 automatically. To give projects the chance to create profiles for |
| 139 new users, this method checks for the presence of a classmethod in the | |
| 140 profile model called 'create_profile' if no profile for a user can be found. | |
| 141 If such a method exists, it is called with a DjangoUser as it's only | |
| 142 argument to intially create a profile for this user. The method must return | |
| 143 the created profile. | |
| 133 """ | 144 """ |
| 134 if not hasattr(self, '_profile_cache'): | 145 if not hasattr(self, '_profile_cache'): |
| 135 from django.conf import settings | 146 from django.conf import settings |
| 136 if not hasattr(settings, "AUTH_PROFILE_MODULE"): | 147 if not hasattr(settings, "AUTH_PROFILE_MODULE"): |
| 137 raise SiteProfileNotAvailable | 148 raise SiteProfileNotAvailable |
| 138 try: | 149 try: |
| 139 app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') | 150 app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') |
| 140 model = models.get_model(app_label, model_name) | 151 model = models.get_model(app_label, model_name) |
| 141 self._profile_cache = model.all().filter("user =", self).get() | 152 self._profile_cache = model.all().filter("user =", self).get() |
| 142 if not self._profile_cache: | 153 if not self._profile_cache and hasattr(model, "create_profile"): |
| 143 raise model.DoesNotExist | 154 self._profile_cache = model.create_profile(self) |
|
mattbrown.nz
2008/05/18 11:23:43
else:
raise DoesNotExist
To stay consistent with
aalbrecht
2008/05/18 19:17:47
On 2008/05/18 11:23:43, mattbrown.nz wrote:
> else
| |
| 144 except (ImportError, ImproperlyConfigured): | 155 except (ImportError, ImproperlyConfigured): |
| 145 raise SiteProfileNotAvailable | 156 raise SiteProfileNotAvailable |
| 146 return self._profile_cache | 157 return self._profile_cache |
| LEFT | RIGHT |