Permalink
Browse files

Added compatibility for timezone aware datetimes as supported by Djan…

…go > 1.4.
  • Loading branch information...
1 parent 8aa77ea commit 89fd518a20d2354cd6d0b9b235abd9a042f50bbc @jezdez jezdez committed Jan 19, 2012
Showing with 16 additions and 6 deletions.
  1. +7 −2 djangoratings/fields.py
  2. +9 −4 djangoratings/models.py
View
@@ -20,7 +20,12 @@
from hashlib import md5
except ImportError:
from md5 import new as md5
-
+
+try:
+ from django.utils.timezone import now
+except ImportError:
+ now = datetime.now
+
def md5_hexdigest(value):
return md5(value).hexdigest()
@@ -162,7 +167,7 @@ def add(self, score, user, ip_address, cookies={}, commit=True):
use_cookies = (self.field.allow_anonymous and self.field.use_cookies)
if use_cookies:
- defaults['cookie'] = datetime.now().strftime('%Y%m%d%H%M%S%f') # -> md5_hexdigest?
+ defaults['cookie'] = now().strftime('%Y%m%d%H%M%S%f') # -> md5_hexdigest?
# TODO: move 'vote-%d.%d.%s' to settings or something
cookie_name = 'vote-%d.%d.%s' % (kwargs['content_type'].pk, kwargs['object_id'], kwargs['key'][:6],) # -> md5_hexdigest?
cookie = cookies.get(cookie_name) # try to get existent cookie value
View
@@ -1,9 +1,14 @@
+from datetime import datetime
+
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User
-import datetime
+try:
+ from django.utils.timezone import now
+except ImportError:
+ now = datetime.now
from managers import VoteManager, SimilarUserManager
@@ -15,8 +20,8 @@ class Vote(models.Model):
user = models.ForeignKey(User, blank=True, null=True, related_name="votes")
ip_address = models.IPAddressField()
cookie = models.CharField(max_length=32, blank=True, null=True)
- date_added = models.DateTimeField(default=datetime.datetime.now, editable=False)
- date_changed = models.DateTimeField(default=datetime.datetime.now, editable=False)
+ date_added = models.DateTimeField(default=now, editable=False)
+ date_changed = models.DateTimeField(default=now, editable=False)
objects = VoteManager()
@@ -29,7 +34,7 @@ def __unicode__(self):
return u"%s voted %s on %s" % (self.user_display, self.score, self.content_object)
def save(self, *args, **kwargs):
- self.date_changed = datetime.datetime.now()
+ self.date_changed = now()
super(Vote, self).save(*args, **kwargs)
def user_display(self):

0 comments on commit 89fd518

Please sign in to comment.