Fix django timezone support

This commit is contained in:
Aleksey Porfirov 2012-10-15 02:13:52 +04:00
parent e4af0e361a
commit 0a89899ad0
3 changed files with 13 additions and 8 deletions

View File

@ -1,5 +1,3 @@
import datetime
from mongoengine import * from mongoengine import *
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
@ -33,6 +31,7 @@ except ImportError:
hash = get_hexdigest(algo, salt, raw_password) hash = get_hexdigest(algo, salt, raw_password)
return '%s$%s$%s' % (algo, salt, hash) return '%s$%s$%s' % (algo, salt, hash)
from .utils import datetime_now
REDIRECT_FIELD_NAME = 'next' REDIRECT_FIELD_NAME = 'next'
@ -62,9 +61,9 @@ class User(Document):
is_superuser = BooleanField(default=False, is_superuser = BooleanField(default=False,
verbose_name=_('superuser status'), verbose_name=_('superuser status'),
help_text=_("Designates that this user has all permissions without explicitly assigning them.")) help_text=_("Designates that this user has all permissions without explicitly assigning them."))
last_login = DateTimeField(default=datetime.datetime.now, last_login = DateTimeField(default=datetime_now,
verbose_name=_('last login')) verbose_name=_('last login'))
date_joined = DateTimeField(default=datetime.datetime.now, date_joined = DateTimeField(default=datetime_now,
verbose_name=_('date joined')) verbose_name=_('date joined'))
meta = { meta = {
@ -130,7 +129,7 @@ class User(Document):
"""Create (and save) a new user with the given username, password and """Create (and save) a new user with the given username, password and
email address. email address.
""" """
now = datetime.datetime.now() now = datetime_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.

View File

@ -1,5 +1,3 @@
from datetime import datetime
from django.conf import settings from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase, CreateError from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
@ -10,6 +8,8 @@ from mongoengine import fields
from mongoengine.queryset import OperationError from mongoengine.queryset import OperationError
from mongoengine.connection import DEFAULT_CONNECTION_NAME from mongoengine.connection import DEFAULT_CONNECTION_NAME
from .utils import datetime_now
MONGOENGINE_SESSION_DB_ALIAS = getattr( MONGOENGINE_SESSION_DB_ALIAS = getattr(
settings, 'MONGOENGINE_SESSION_DB_ALIAS', settings, 'MONGOENGINE_SESSION_DB_ALIAS',
@ -33,7 +33,7 @@ class SessionStore(SessionBase):
def load(self): def load(self):
try: try:
s = MongoSession.objects(session_key=self.session_key, s = MongoSession.objects(session_key=self.session_key,
expire_date__gt=datetime.now())[0] expire_date__gt=datetime_now())[0]
return self.decode(force_unicode(s.session_data)) return self.decode(force_unicode(s.session_data))
except (IndexError, SuspiciousOperation): except (IndexError, SuspiciousOperation):
self.create() self.create()

View File

@ -0,0 +1,6 @@
try:
# django >= 1.4
from django.utils.timezone import now as datetime_now
except ImportError:
from datetime import datetime
datetime_now = datetime.now