Upgraded django.contrib.auth to be compatible with time zone support.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17122 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-11-20 10:50:18 +00:00
parent 4ac594f8a5
commit 03cfad4198
2 changed files with 8 additions and 5 deletions

View File

@ -1,4 +1,3 @@
import datetime
import urllib import urllib
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -7,6 +6,7 @@ from django.db import models
from django.db.models.manager import EmptyManager from django.db.models.manager import EmptyManager
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.contrib import auth from django.contrib import auth
from django.contrib.auth.signals import user_logged_in from django.contrib.auth.signals import user_logged_in
@ -21,7 +21,7 @@ def update_last_login(sender, user, **kwargs):
A signal receiver which updates the last_login date for A signal receiver which updates the last_login date for
the user logging in. the user logging in.
""" """
user.last_login = datetime.datetime.now() user.last_login = timezone.now()
user.save() user.save()
user_logged_in.connect(update_last_login) user_logged_in.connect(update_last_login)
@ -91,7 +91,7 @@ class UserManager(models.Manager):
""" """
Creates and saves a User with the given username, email and password. Creates and saves a User with the given username, email and password.
""" """
now = datetime.datetime.now() now = timezone.now()
# Normalize the address by lowercasing the domain part of the email # Normalize the address by lowercasing the domain part of the email
# address. # address.
@ -181,8 +181,8 @@ class User(models.Model):
is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site.")) is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site."))
is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user should be treated as active. Unselect this instead of deleting accounts.")) is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user should be treated as active. Unselect this instead of deleting accounts."))
is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_("Designates that this user has all permissions without explicitly assigning them.")) is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_("Designates that this user has all permissions without explicitly assigning them."))
last_login = models.DateTimeField(_('last login'), default=datetime.datetime.now) last_login = models.DateTimeField(_('last login'), default=timezone.now)
date_joined = models.DateTimeField(_('date joined'), default=datetime.datetime.now) date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True, groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True,
help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.")) help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in."))
user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True) user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True)

View File

@ -4,6 +4,7 @@ from django.conf import settings
from django.contrib.auth.backends import RemoteUserBackend from django.contrib.auth.backends import RemoteUserBackend
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from django.utils import timezone
class RemoteUserTest(TestCase): class RemoteUserTest(TestCase):
@ -80,6 +81,8 @@ class RemoteUserTest(TestCase):
user = User.objects.create(username='knownuser') user = User.objects.create(username='knownuser')
# Set last_login to something so we can determine if it changes. # Set last_login to something so we can determine if it changes.
default_login = datetime(2000, 1, 1) default_login = datetime(2000, 1, 1)
if settings.USE_TZ:
default_login = default_login.replace(tzinfo=timezone.utc)
user.last_login = default_login user.last_login = default_login
user.save() user.save()