summaryrefslogtreecommitdiffstats
path: root/appengine_django/auth/models.py
blob: d93e2404438f60269fe3385ca1a453e807b0d88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright 2008 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.

"""
App Engine compatible models for the Django authentication framework.
"""

from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.encoding import smart_str
import urllib

from django.db.models.manager import EmptyManager

from google.appengine.api import users
from google.appengine.ext import db

from appengine_django.models import BaseModel


class User(BaseModel):
  """A model with the same attributes and methods as a Django user model.

  The model has two additions. The first addition is a 'user' attribute which
  references a App Engine user. The second is the 'get_djangouser_for_user'
  classmethod that should be used to retrieve a DjangoUser instance from a App
  Engine user object.
  """
  user = db.UserProperty(required=True)
  username = db.StringProperty(required=True)
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  email = db.EmailProperty()
  password = db.StringProperty()
  is_staff = db.BooleanProperty(default=False, required=True)
  is_active = db.BooleanProperty(default=True, required=True)
  is_superuser = db.BooleanProperty(default=False, required=True)
  last_login = db.DateTimeProperty(auto_now_add=True, required=True)
  date_joined = db.DateTimeProperty(auto_now_add=True, required=True)
  groups = EmptyManager()
  user_permissions = EmptyManager()

  def __unicode__(self):
    return self.username

  def __str__(self):
    return unicode(self).encode('utf-8')

  @classmethod
  def get_djangouser_for_user(cls, user):
    query = cls.all().filter("user =", user)
    if query.count() == 0:
      django_user = cls(user=user, email=user.email(), username=user.nickname())
      django_user.save()
    else:
      django_user = query.get()
    return django_user

  def set_password(self, raw_password):
    raise NotImplementedError

  def check_password(self, raw_password):
    raise NotImplementedError

  def set_unusable_password(self):
    raise NotImplementedError

  def has_usable_password(self):
    raise NotImplementedError

  def get_group_permissions(self):
    return self.user_permissions

  def get_all_permissions(self):
    return self.user_permissions

  def has_perm(self, perm):
    return False

  def has_perms(self, perm_list):
    return False

  def has_module_perms(self, module):
    return False

  def get_and_delete_messages(self):
    """Gets and deletes messages for this user"""
    msgs = []
    for msg in self.message_set:
      msgs.append(msg)
      msg.delete()
    return msgs

  def is_anonymous(self):
    """Always return False"""
    return False

  def is_authenticated(self):
    """Always return True"""
    return True

  def get_absolute_url(self):
    return "/users/%s/" % urllib.quote(smart_str(self.username))

  def get_full_name(self):
    full_name = u'%s %s' % (self.first_name, self.last_name)
    return full_name.strip()

  def email_user(self, subject, message, from_email):
    """Sends an email to this user.

    According to the App Engine email API the from_email must be the
    email address of a registered administrator for the application.
    """
    mail.send_mail(subject,
                   message,
                   from_email,
                   [self.email])

  def get_profile(self):
    """
    Returns site-specific profile for this user. Raises
    SiteProfileNotAvailable if this site does not allow profiles.

    When using the App Engine authentication framework, users are created
    automatically.
    """
    from django.contrib.auth.models import SiteProfileNotAvailable
    if not hasattr(self, '_profile_cache'):
      from django.conf import settings
      if not hasattr(settings, "AUTH_PROFILE_MODULE"):
        raise SiteProfileNotAvailable
      try:
        app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
        model = models.get_model(app_label, model_name)
        self._profile_cache = model.all().filter("user =", self).get()
        if not self._profile_cache:
          raise model.DoesNotExist
      except (ImportError, ImproperlyConfigured):
        raise SiteProfileNotAvailable
    return self._profile_cache


class Group(BaseModel):
  """Group model not fully implemented yet."""
  # TODO: Implement this model, requires contenttypes
  name = db.StringProperty()
  permissions = EmptyManager()


class Message(BaseModel):
  """User message model"""
  user = db.ReferenceProperty(User)
  message = db.TextProperty()


class Permission(BaseModel):
  """Permission model not fully implemented yet."""
  # TODO: Implement this model, requires contenttypes
  name = db.StringProperty()