Mercurial > public > sg101
comparison forums/latest.py @ 679:89b240fe9297
For Django 1.5.2: import json; django.utils.simplejson is deprecated.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 15 Aug 2013 20:14:33 -0500 |
parents | f3fded5df64b |
children | 7429c98c8ece |
comparison
equal
deleted
inserted
replaced
678:38a198ea8c61 | 679:89b240fe9297 |
---|---|
49 # | 49 # |
50 # We also maintain topic and post counts in Redis since select(*) can take a | 50 # We also maintain topic and post counts in Redis since select(*) can take a |
51 # while with MySQL InnoDb. | 51 # while with MySQL InnoDb. |
52 # | 52 # |
53 import datetime | 53 import datetime |
54 import json | |
54 import logging | 55 import logging |
55 import time | 56 import time |
56 | 57 |
57 from django.dispatch import receiver | 58 from django.dispatch import receiver |
58 from django.utils import simplejson | |
59 from django.template.loader import render_to_string | 59 from django.template.loader import render_to_string |
60 import redis | 60 import redis |
61 | 61 |
62 from forums.signals import post_content_update, topic_content_update | 62 from forums.signals import post_content_update, topic_content_update |
63 from forums.models import Forum, Topic, Post, Attachment | 63 from forums.models import Forum, Topic, Post, Attachment |
235 'title': post.topic.name, | 235 'title': post.topic.name, |
236 'author': post.user.username, | 236 'author': post.user.username, |
237 'date': topic_score, | 237 'date': topic_score, |
238 'url': post.topic.get_latest_post_url() | 238 'url': post.topic.get_latest_post_url() |
239 } | 239 } |
240 json = simplejson.dumps(topic_content) | 240 topic_json = json.dumps(topic_content) |
241 key = UPDATED_TOPIC_KEY % topic_id | 241 key = UPDATED_TOPIC_KEY % topic_id |
242 | 242 |
243 pipeline = conn.pipeline() | 243 pipeline = conn.pipeline() |
244 pipeline.set(key, json) | 244 pipeline.set(key, topic_json) |
245 pipeline.zadd(UPDATED_TOPICS_SET_KEY, topic_score, topic_id) | 245 pipeline.zadd(UPDATED_TOPICS_SET_KEY, topic_score, topic_id) |
246 pipeline.zcard(UPDATED_TOPICS_SET_KEY) | 246 pipeline.zcard(UPDATED_TOPICS_SET_KEY) |
247 results = pipeline.execute() | 247 results = pipeline.execute() |
248 | 248 |
249 # delete topics beyond our maximum count | 249 # delete topics beyond our maximum count |
286 raw_posts = conn.mget(post_keys) | 286 raw_posts = conn.mget(post_keys) |
287 raw_posts = [s for s in raw_posts if s is not None] | 287 raw_posts = [s for s in raw_posts if s is not None] |
288 | 288 |
289 posts = [] | 289 posts = [] |
290 for raw_post in raw_posts: | 290 for raw_post in raw_posts: |
291 post = simplejson.loads(raw_post) | 291 post = json.loads(raw_post) |
292 | 292 |
293 # fix up the pubdate; turn it back into a datetime object | 293 # fix up the pubdate; turn it back into a datetime object |
294 post['pubdate'] = datetime.datetime.fromtimestamp(post['pubdate']) | 294 post['pubdate'] = datetime.datetime.fromtimestamp(post['pubdate']) |
295 | 295 |
296 posts.append(post) | 296 posts.append(post) |
348 This function contains updated topic processing. Update the title only. | 348 This function contains updated topic processing. Update the title only. |
349 | 349 |
350 """ | 350 """ |
351 conn = get_redis_connection() | 351 conn = get_redis_connection() |
352 key = UPDATED_TOPIC_KEY % topic_id | 352 key = UPDATED_TOPIC_KEY % topic_id |
353 json = conn.get(key) | 353 topic_json = conn.get(key) |
354 if json is not None: | 354 if topic_json is not None: |
355 try: | 355 try: |
356 topic = Topic.objects.get(pk=topic_id) | 356 topic = Topic.objects.get(pk=topic_id) |
357 except Topic.DoesNotExist: | 357 except Topic.DoesNotExist: |
358 logger.warning("topic %d does not exist", topic_id) | 358 logger.warning("topic %d does not exist", topic_id) |
359 return | 359 return |
360 | 360 |
361 topic_dict = simplejson.loads(json) | 361 topic_dict = json.loads(topic_json) |
362 | 362 |
363 if topic.name != topic_dict['title']: | 363 if topic.name != topic_dict['title']: |
364 topic_dict['title'] = topic.name | 364 topic_dict['title'] = topic.name |
365 json = simplejson.dumps(topic_dict) | 365 topic_json = json.dumps(topic_dict) |
366 conn.set(key, json) | 366 conn.set(key, topic_json) |
367 | 367 |
368 | 368 |
369 def get_stats(): | 369 def get_stats(): |
370 """ | 370 """ |
371 This function returns the topic and post count statistics as a tuple, in | 371 This function returns the topic and post count statistics as a tuple, in |
419 logger.error(e) | 419 logger.error(e) |
420 return [] | 420 return [] |
421 | 421 |
422 topics = [] | 422 topics = [] |
423 for s in json_list: | 423 for s in json_list: |
424 item = simplejson.loads(s) | 424 item = json.loads(s) |
425 item['date'] = datetime.datetime.fromtimestamp(item['date']) | 425 item['date'] = datetime.datetime.fromtimestamp(item['date']) |
426 topics.append(item) | 426 topics.append(item) |
427 | 427 |
428 return topics | 428 return topics |
429 | 429 |
475 'pubdate': int(time.mktime(post.creation_date.timetuple())), | 475 'pubdate': int(time.mktime(post.creation_date.timetuple())), |
476 'forum_name': post.topic.forum.name, | 476 'forum_name': post.topic.forum.name, |
477 'url': post.get_absolute_url() | 477 'url': post.get_absolute_url() |
478 } | 478 } |
479 | 479 |
480 return simplejson.dumps(post_content) | 480 return json.dumps(post_content) |
481 | 481 |
482 | 482 |
483 # Down here to avoid a circular import | 483 # Down here to avoid a circular import |
484 import forums.tasks | 484 import forums.tasks |