comparison forums/latest.py @ 1168:90e8cc6eff77

Fix ambiguous date errors in forum feeds.
author Brian Neal <bgneal@gmail.com>
date Sun, 05 Nov 2017 14:59:26 -0600
parents 7429c98c8ece
children e9f6a2c5c1de
comparison
equal deleted inserted replaced
1162:d551b6004213 1168:90e8cc6eff77
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 from django.conf import settings
58 from django.dispatch import receiver 59 from django.dispatch import receiver
59 from django.template.loader import render_to_string 60 from django.template.loader import render_to_string
61 import pytz
60 import redis 62 import redis
61 63
62 from forums.signals import post_content_update, topic_content_update 64 from forums.signals import post_content_update, topic_content_update
63 from forums.models import Forum, Topic, Post, Attachment 65 from forums.models import Forum, Topic, Post, Attachment
64 from forums.views.subscriptions import notify_topic_subscribers 66 from forums.views.subscriptions import notify_topic_subscribers
69 # This constant controls how many latest posts per forum we store 71 # This constant controls how many latest posts per forum we store
70 MAX_POSTS = 50 72 MAX_POSTS = 50
71 73
72 # This controls how many updated topics we track 74 # This controls how many updated topics we track
73 MAX_UPDATED_TOPICS = 50 75 MAX_UPDATED_TOPICS = 50
76
77 SERVER_TZ = pytz.timezone(settings.TIME_ZONE)
74 78
75 # Redis key names: 79 # Redis key names:
76 POST_COUNT_KEY = "forums:public_post_count" 80 POST_COUNT_KEY = "forums:public_post_count"
77 TOPIC_COUNT_KEY = "forums:public_topic_count" 81 TOPIC_COUNT_KEY = "forums:public_topic_count"
78 UPDATED_TOPICS_SET_KEY = "forums:updated_topics:set" 82 UPDATED_TOPICS_SET_KEY = "forums:updated_topics:set"
290 posts = [] 294 posts = []
291 for raw_post in raw_posts: 295 for raw_post in raw_posts:
292 post = json.loads(raw_post) 296 post = json.loads(raw_post)
293 297
294 # fix up the pubdate; turn it back into a datetime object 298 # fix up the pubdate; turn it back into a datetime object
295 post['pubdate'] = datetime.datetime.fromtimestamp(post['pubdate']) 299 pubdate = datetime.datetime.utcfromtimestamp(post['pubdate'])
300 pubdate.replace(tzinfo=SERVER_TZ)
301 post['pubdate'] = pubdate
296 302
297 posts.append(post) 303 posts.append(post)
298 304
299 return posts 305 return posts
300 306
472 post_content = { 478 post_content = {
473 'id': post.id, 479 'id': post.id,
474 'title': post.topic.name, 480 'title': post.topic.name,
475 'content': content, 481 'content': content,
476 'author': post.user.username, 482 'author': post.user.username,
477 'pubdate': int(time.mktime(post.creation_date.timetuple())), 483 'pubdate': int(time.mktime(post.creation_date.utctimetuple())),
478 'forum_name': post.topic.forum.name, 484 'forum_name': post.topic.forum.name,
479 'url': post.get_absolute_url() 485 'url': post.get_absolute_url()
480 } 486 }
481 487
482 return json.dumps(post_content) 488 return json.dumps(post_content)