Robert Osborne avatar Robert Osborne committed 2a3254c

support localtime zones

Comments (0)

Files changed (2)

articles/models.py

 from django.utils.text import truncate_html_words
 
 try:
+    from django.utils import timezone
+    safe_now = timezone.now
+except ImportError:
+    import datetime
+    safe_now = datetime.datetime.now
+
+try:
     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) |
                 Q(expiration_date__gte=now),
     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
                 self.save()
 
         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)

articles/views.py

     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:
         try:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.