1. Robert Osborne
  2. django-articles


Robert Osborne  committed 2a3254c

support localtime zones

  • Participants
  • Parent commits 5ac448b
  • Branches default

Comments (0)

Files changed (2)

File articles/models.py

View file
 from django.utils.text import truncate_html_words
+    from django.utils import timezone
+    safe_now = timezone.now
+except ImportError:
+    import datetime
+    safe_now = datetime.datetime.now
     from django.contrib.auth import get_user_model
     User = get_user_model()
 except ImportError:
         Retrieves all active articles which have been published and have not
         yet expired.
-        now = datetime.now()
+        now = safe_now()
         return self.get_query_set().filter(
                 Q(expiration_date__isnull=True) |
     followup_for = models.ManyToManyField('self', symmetrical=False, blank=True, help_text=_('Select any other articles that this article follows up on.'), related_name='followups')
     related_articles = models.ManyToManyField('self', blank=True)
-    publish_date = models.DateTimeField(default=datetime.now, help_text=_('The date and time this article shall appear online.'))
+    publish_date = models.DateTimeField(default=safe_now, help_text=_('The date and time this article shall appear online.'))
     expiration_date = models.DateTimeField(blank=True, null=True, help_text=_('Leave blank if the article does not expire.'))
     is_active = models.BooleanField(default=True, blank=True)
         if self.id:
             # mark the article as inactive if it's expired and still active
-            if self.expiration_date and self.expiration_date <= datetime.now() and self.is_active:
+            if self.expiration_date and self.expiration_date <= safe_now() and self.is_active:
                 self.is_active = False
         get_latest_by = 'publish_date'
 class Attachment(models.Model):
-    upload_to = lambda inst, fn: 'attach/%s/%s/%s' % (datetime.now().year, inst.article.slug, fn)
+    upload_to = lambda inst, fn: 'attach/%s/%s/%s' % (safe_now().year, inst.article.slug, fn)
     article = models.ForeignKey(Article, related_name='attachments')
     attachment = models.FileField(upload_to=upload_to)

File articles/views.py

View file
     Yes, it's dirty to have so many URLs go to one view, but I'd rather do that
     than duplicate a bunch of code.  I'll probably revisit this in the future.
     context = {'request': request}
     if tag: