comparison forums/latest.py @ 1177:e9f6a2c5c1de

Daylight savings time fix
author Brian Neal <bgneal@gmail.com>
date Sun, 10 Mar 2019 14:03:47 -0500
parents 90e8cc6eff77
children
comparison
equal deleted inserted replaced
1176:0436a2ff6ff1 1177:e9f6a2c5c1de
53 import datetime 53 import datetime
54 import json 54 import json
55 import logging 55 import logging
56 import time 56 import time
57 57
58 import dateutil.parser
58 from django.conf import settings 59 from django.conf import settings
59 from django.dispatch import receiver 60 from django.dispatch import receiver
60 from django.template.loader import render_to_string 61 from django.template.loader import render_to_string
62 from django.utils.timezone import get_default_timezone, make_aware
61 import pytz 63 import pytz
62 import redis 64 import redis
63 65
64 from forums.signals import post_content_update, topic_content_update 66 from forums.signals import post_content_update, topic_content_update
65 from forums.models import Forum, Topic, Post, Attachment 67 from forums.models import Forum, Topic, Post, Attachment
294 posts = [] 296 posts = []
295 for raw_post in raw_posts: 297 for raw_post in raw_posts:
296 post = json.loads(raw_post) 298 post = json.loads(raw_post)
297 299
298 # fix up the pubdate; turn it back into a datetime object 300 # fix up the pubdate; turn it back into a datetime object
299 pubdate = datetime.datetime.utcfromtimestamp(post['pubdate']) 301 post['pubdate'] = _deserialize_date(post['pubdate'])
300 pubdate.replace(tzinfo=SERVER_TZ)
301 post['pubdate'] = pubdate
302 302
303 posts.append(post) 303 posts.append(post)
304 304
305 return posts 305 return posts
306
307
308 def _deserialize_date(pubdate):
309 if isinstance(pubdate, (int, long)):
310 # legacy data, fix up and watch out for timezone glitches
311 new_date = datetime.datetime.utcfromtimestamp(pubdate)
312 new_date.replace(tzinfo=SERVER_TZ)
313
314 tz = get_default_timezone()
315 try:
316 make_aware(new_date, tz)
317 except pytz.NonExistentTimeError:
318 new_date += datetime.timedelta(hours=1)
319 return new_date
320
321 return dateutil.parser.parse(pubdate)
306 322
307 323
308 @receiver(topic_content_update, dispatch_uid='forums.latest_posts') 324 @receiver(topic_content_update, dispatch_uid='forums.latest_posts')
309 def on_topic_update(sender, **kwargs): 325 def on_topic_update(sender, **kwargs):
310 """ 326 """
478 post_content = { 494 post_content = {
479 'id': post.id, 495 'id': post.id,
480 'title': post.topic.name, 496 'title': post.topic.name,
481 'content': content, 497 'content': content,
482 'author': post.user.username, 498 'author': post.user.username,
483 'pubdate': int(time.mktime(post.creation_date.utctimetuple())), 499 'pubdate': post.creation_date.isoformat(),
484 'forum_name': post.topic.forum.name, 500 'forum_name': post.topic.forum.name,
485 'url': post.get_absolute_url() 501 'url': post.get_absolute_url()
486 } 502 }
487 503
488 return json.dumps(post_content) 504 return json.dumps(post_content)