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