Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Timezone support #37

Merged
merged 3 commits into from

1 participant

Mathijs de Bruin
Mathijs de Bruin
Owner

Support for Django 1.4's timezone aware dates - with 1.3 backwards-support and tests.

Concludes #35.

Mathijs de Bruin dokterbob merged commit 9d57144 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 19, 2012
  1. Initial timezone support. Cherry-pick of 8fd922c.

    authored
    Conflicts:
    
    	newsletter/admin.py
    	newsletter/models.py
    
    Reference: sq9mev@8fd922c
This page is out of date. Refresh to see the latest.
13 newsletter/admin.py
View
@@ -1,9 +1,6 @@
import logging
-
logger = logging.getLogger(__name__)
-from datetime import datetime
-
from django.conf import settings
from django.conf.urls.defaults import patterns, url
@@ -30,6 +27,8 @@
EmailTemplate, Newsletter, Subscription, Article, Message, Submission
)
+from .utils import now
+
from .admin_forms import *
from .admin_utils import *
@@ -128,7 +127,7 @@ def admin_status(self, obj):
return u'<img src="%s" width="10" height="10" alt="%s"/>' % (
YES_ICON_URL, self.admin_status_text(obj))
else:
- if obj.publish_date > datetime.now():
+ if obj.publish_date > now():
return \
u'<img src="%s" width="10" height="10" alt="%s"/>' % (
WAIT_ICON_URL, self.admin_status_text(obj))
@@ -148,7 +147,7 @@ def admin_status_text(self, obj):
if obj.sent:
return ugettext("Sent.")
else:
- if obj.publish_date > datetime.now():
+ if obj.publish_date > now():
return ugettext("Delayed submission.")
else:
return ugettext("Submitting.")
@@ -282,7 +281,7 @@ def preview_html(self, request, object_id):
c = Context({'message': message,
'site': Site.objects.get_current(),
'newsletter': message.newsletter,
- 'date': datetime.now(),
+ 'date': now(),
'STATIC_URL': settings.STATIC_URL,
'MEDIA_URL': settings.MEDIA_URL})
@@ -298,7 +297,7 @@ def preview_text(self, request, object_id):
'message': message,
'site': Site.objects.get_current(),
'newsletter': message.newsletter,
- 'date': datetime.now(),
+ 'date': now(),
'STATIC_URL': settings.STATIC_URL,
'MEDIA_URL': settings.MEDIA_URL
}, autoescape=False
14 newsletter/models.py
View
@@ -24,6 +24,8 @@
from django.conf import settings
+from .utils import now
+
def make_activation_code():
""" Generate a unique activation code. """
@@ -270,7 +272,7 @@ def set_email(self, email):
def subscribe(self):
logger.debug(u'Subscribing subscription %s.', self)
- self.subscribe_date = datetime.now()
+ self.subscribe_date = now()
self.subscribed = True
self.unsubscribed = False
@@ -279,7 +281,7 @@ def unsubscribe(self):
self.subscribed = False
self.unsubscribed = True
- self.unsubscribe_date = datetime.now()
+ self.unsubscribe_date = now()
def save(self, *args, **kwargs):
assert self.user or self.email_field, \
@@ -333,7 +335,7 @@ def save(self, *args, **kwargs):
newsletter = models.ForeignKey('Newsletter', verbose_name=_('newsletter'))
- create_date = models.DateTimeField(editable=False, default=datetime.now)
+ create_date = models.DateTimeField(editable=False, default=now)
activation_code = models.CharField(
verbose_name=_('activation code'), max_length=40,
@@ -638,7 +640,7 @@ def submit(self):
{'submission': self, 'count': subscriptions.count()}
)
- assert self.publish_date < datetime.now(), \
+ assert self.publish_date < now(), \
'Something smells fishy; submission time in future.'
self.sending = True
@@ -704,7 +706,7 @@ def submit(self):
def submit_queue(cls):
todo = cls.objects.filter(
prepared=True, sent=False, sending=False,
- publish_date__lt=datetime.now()
+ publish_date__lt=now()
)
for submission in todo:
@@ -761,7 +763,7 @@ def get_absolute_url(self):
publish_date = models.DateTimeField(
verbose_name=_('publication date'), blank=True, null=True,
- default=datetime.now(), db_index=True
+ default=now(), db_index=True
)
publish = models.BooleanField(
default=True, verbose_name=_('publish'),
5 newsletter/tests/test_mailing.py
View
@@ -1,9 +1,10 @@
-from datetime import datetime, timedelta
+from datetime import timedelta
from django.core import mail
from newsletter.models import *
from newsletter.forms import *
+from newsletter.utils import now
from utils import *
@@ -154,7 +155,7 @@ def test_submitsubmission(self):
""" Test queue-based submission. """
self.sub.prepared = True
- self.sub.publish_date = datetime.now() - timedelta(seconds=1)
+ self.sub.publish_date = now() - timedelta(seconds=1)
self.sub.save()
Submission.submit_queue()
10 newsletter/tests/test_web.py
View
@@ -1,4 +1,5 @@
-from datetime import datetime, timedelta
+from datetime import timedelta
+
import time
from django.core import mail
@@ -7,6 +8,7 @@
from newsletter.models import *
from newsletter.forms import *
+from newsletter.utils import now
from utils import *
@@ -311,7 +313,7 @@ def test_unsubscribe_view(self):
subscription.save()
self.assertLessThan(
- subscription.subscribe_date, datetime.now() + timedelta(seconds=1)
+ subscription.subscribe_date, now() + timedelta(seconds=1)
)
response = self.client.get(self.unsubscribe_url)
@@ -348,7 +350,7 @@ def test_unsubscribe_post(self):
self.assert_(subscription.unsubscribed)
self.assertLessThan(
subscription.unsubscribe_date,
- datetime.now() + timedelta(seconds=1)
+ now() + timedelta(seconds=1)
)
def test_unsubscribe_twice(self):
@@ -657,7 +659,7 @@ def test_unsubscribe_request_activate(self):
self.assertEqual(subscription.name, testname2)
self.assertEqual(subscription.email, testemail2)
- dt = (datetime.now() - subscription.unsubscribe_date).seconds
+ dt = (now() - subscription.unsubscribe_date).seconds
self.assertLessThan(dt, 2)
def test_update_request_view(self):
11 newsletter/utils.py
View
@@ -0,0 +1,11 @@
+import logging
+logger = logging.getLogger(__name__)
+
+# Conditional import of 'now'
+# Django 1.4 should use timezone.now, Django 1.3 datetime.now
+try:
+ from django.utils.timezone import now
+except ImportError:
+ logger.warn('Timezone support not enabled.')
+ from datetime import datetime
+ now = datetime.now
3  test_settings.py
View
@@ -18,3 +18,6 @@
SITE_ID = 1
TEMPLATE_DIRS = ('test_templates', )
+
+# Enable time-zone support for Django 1.4 (ignored in older versions)
+USE_TZ = True
Something went wrong with that request. Please try again.