From c6691ec0c09c8d4d76e39d15b56a135fdfab7730 Mon Sep 17 00:00:00 2001 From: Mathieu Agopian Date: Wed, 11 Apr 2012 20:34:54 +0200 Subject: [PATCH 1/2] Django1.4 with USE_TZ=True needs timezone aware datetimes --- notification/atomformat.py | 4 ++-- notification/feeds.py | 3 ++- notification/management/commands/remove_old_notices.py | 3 ++- notification/models.py | 7 +++---- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/notification/atomformat.py b/notification/atomformat.py index 756feeb8..21e93004 100644 --- a/notification/atomformat.py +++ b/notification/atomformat.py @@ -29,7 +29,7 @@ # from xml.sax.saxutils import XMLGenerator -from datetime import datetime +from django.utils.timezone import now GENERATOR_TEXT = 'django-atompub' @@ -232,7 +232,7 @@ def latest_updated(self): updates.sort() return updates[-1] else: - return datetime.now() # @@@ really we should allow a feed to define its "start" for this case + return now() # @@@ really we should allow a feed to define its "start" for this case def write_text_construct(self, handler, element_name, data): diff --git a/notification/feeds.py b/notification/feeds.py index 7c1f46d0..bb2b46dd 100644 --- a/notification/feeds.py +++ b/notification/feeds.py @@ -4,6 +4,7 @@ from django.conf import settings from django.shortcuts import get_object_or_404 from django.template.defaultfilters import linebreaks, escape, striptags +from django.utils.timezone import utc from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User @@ -66,7 +67,7 @@ def feed_updated(self, user): # must be a feed_updated field as per the Atom specifications, however # there is no real data to go by, and an arbitrary date can be static. if qs.count() == 0: - return datetime.datetime(year=2008, month=7, day=1) + return datetime.datetime(year=2008, month=7, day=1, tzinfo=utc) return qs.latest("added").added def feed_links(self, user): diff --git a/notification/management/commands/remove_old_notices.py b/notification/management/commands/remove_old_notices.py index 1139bd49..e037fdcf 100644 --- a/notification/management/commands/remove_old_notices.py +++ b/notification/management/commands/remove_old_notices.py @@ -5,6 +5,7 @@ from django.core.management.base import BaseCommand from django.conf import settings +from django.utils.timezone import make_aware, utc from notification.models import Notice @@ -24,7 +25,7 @@ class Command(BaseCommand): def handle(self, *args, **options): dryrun = options['dryrun'] limit = time() - settings.NOTICES_MAX_AGE - limit = datetime.fromtimestamp(limit) + limit = make_aware(datetime.fromtimestamp(limit), utc) notices = Notice.objects.filter(added__lte=limit) if dryrun: count = notices.count() diff --git a/notification/models.py b/notification/models.py index 2a2e3403..b66dae28 100644 --- a/notification/models.py +++ b/notification/models.py @@ -1,5 +1,3 @@ -import datetime - try: import cPickle as pickle except ImportError: @@ -13,6 +11,7 @@ from django.core.urlresolvers import reverse from django.template import Context from django.template.loader import render_to_string +from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext, get_language, activate @@ -151,7 +150,7 @@ class Notice(models.Model): sender = models.ForeignKey(User, null=True, related_name="sent_notices", verbose_name=_("sender")) message = models.TextField(_("message")) notice_type = models.ForeignKey(NoticeType, verbose_name=_("notice type")) - added = models.DateTimeField(_("added"), default=datetime.datetime.now) + added = models.DateTimeField(_("added"), default=now) unseen = models.BooleanField(_("unseen"), default=True) archived = models.BooleanField(_("archived"), default=False) on_site = models.BooleanField(_("on site")) @@ -415,7 +414,7 @@ class ObservedItem(models.Model): notice_type = models.ForeignKey(NoticeType, verbose_name=_("notice type")) - added = models.DateTimeField(_("added"), default=datetime.datetime.now) + added = models.DateTimeField(_("added"), default=now) # the signal that will be listened to send the notice signal = models.TextField(verbose_name=_("signal")) From 47a27835ac437be26b5429f6e1041ca4ec414c81 Mon Sep 17 00:00:00 2001 From: Mathieu Agopian Date: Wed, 11 Apr 2012 22:09:28 +0200 Subject: [PATCH 2/2] compatible with 1.3.1 and 1.4 --- notification/atomformat.py | 7 ++++++- notification/feeds.py | 7 +++++-- notification/management/commands/remove_old_notices.py | 7 +++++-- notification/models.py | 7 ++++++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/notification/atomformat.py b/notification/atomformat.py index 21e93004..2fe27a9c 100644 --- a/notification/atomformat.py +++ b/notification/atomformat.py @@ -28,8 +28,13 @@ # THE SOFTWARE. # +try: + from django.utils.timezone import now +except ImportError: + from datetime import datetime + now = datetime.now + from xml.sax.saxutils import XMLGenerator -from django.utils.timezone import now GENERATOR_TEXT = 'django-atompub' diff --git a/notification/feeds.py b/notification/feeds.py index bb2b46dd..3620bd55 100644 --- a/notification/feeds.py +++ b/notification/feeds.py @@ -4,7 +4,6 @@ from django.conf import settings from django.shortcuts import get_object_or_404 from django.template.defaultfilters import linebreaks, escape, striptags -from django.utils.timezone import utc from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User @@ -67,7 +66,11 @@ def feed_updated(self, user): # must be a feed_updated field as per the Atom specifications, however # there is no real data to go by, and an arbitrary date can be static. if qs.count() == 0: - return datetime.datetime(year=2008, month=7, day=1, tzinfo=utc) + try: + from django.utils.timezone import utc + return datetime.datetime(year=2008, month=7, day=1, tzinfo=utc) + except ImportError: + return datetime.datetime(year=2008, month=7, day=1) return qs.latest("added").added def feed_links(self, user): diff --git a/notification/management/commands/remove_old_notices.py b/notification/management/commands/remove_old_notices.py index e037fdcf..0abe93b9 100644 --- a/notification/management/commands/remove_old_notices.py +++ b/notification/management/commands/remove_old_notices.py @@ -5,7 +5,6 @@ from django.core.management.base import BaseCommand from django.conf import settings -from django.utils.timezone import make_aware, utc from notification.models import Notice @@ -25,7 +24,11 @@ class Command(BaseCommand): def handle(self, *args, **options): dryrun = options['dryrun'] limit = time() - settings.NOTICES_MAX_AGE - limit = make_aware(datetime.fromtimestamp(limit), utc) + try: + from django.utils.timezone import make_aware, utc + limit = make_aware(datetime.fromtimestamp(limit), utc) + except ImportError: + limit = datetime.fromtimestamp(limit) notices = Notice.objects.filter(added__lte=limit) if dryrun: count = notices.count() diff --git a/notification/models.py b/notification/models.py index b66dae28..899fde25 100644 --- a/notification/models.py +++ b/notification/models.py @@ -3,6 +3,12 @@ except ImportError: import pickle +try: + from django.utils.timezone import now +except ImportError: + from datetime import datetime + now = datetime.now + from django.db import models from django.db.models.query import QuerySet from django.conf import settings @@ -11,7 +17,6 @@ from django.core.urlresolvers import reverse from django.template import Context from django.template.loader import render_to_string -from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext, get_language, activate