task-ify read/view updates on show_topic

Fixes 
This commit is contained in:
Matthew Taylor 2014-12-05 14:21:55 -05:00
parent 69cae145b9
commit 585d94f35a
2 changed files with 26 additions and 3 deletions
djangobb_forum

View file

@ -1,5 +1,9 @@
from django.contrib.auth.models import User
from celery.decorators import task
from djangobb_forum.models import Topic
@task
def scratch_notify_topic_subscribers(post_id):
"""
@ -19,3 +23,22 @@ def scratch_notify_topic_subscribers(post_id):
object = topic,
)
social_action.save()
@task
def update_topic_on_view(user_id, topic_id, is_authenticated):
"""
Update the views and reads for a topic - part of show_topic.
Turned into async task to reduce page load times and increase
scalability.
"""
try:
topic = Topic.objects.get(pk=topic_id)
Topic.objects.filter(pk=topic_id).update(views=F('views') + 1)
if is_authenticated:
try:
user = User.objects.get(pk=user_id)
topic.update_read(request.user)
except User.DoesNotExist:
pass
except Topic.DoesNotExist:
pass

View file

@ -31,6 +31,7 @@ from djangobb_forum.forms import AddPostForm, EditPostForm, UserSearchForm, \
VotePollForm, ReportForm, VotePollForm, PollForm, PostStatus
from djangobb_forum.models import Category, Forum, Topic, Post, Reputation, \
Report, Attachment, PostTracking
from djangobb_forum.tasks import update_topic_on_view
from djangobb_forum.templatetags import forum_extras
from djangobb_forum.templatetags.forum_extras import forum_moderated_by
from djangobb_forum.util import build_form, paginate, set_language, smiles, convert_text_to_html, UnapprovedImageError, can_close_topic
@ -387,11 +388,10 @@ def show_topic(request, topic_id, full=True):
topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
if not topic.forum.category.has_access(request.user):
return HttpResponseForbidden()
Topic.objects.filter(pk=topic.id).update(views=F('views') + 1)
last_post = topic.last_post
if request.user.is_authenticated():
topic.update_read(request.user)
update_topic_on_view.delay(user_id=request.user.id, topic_id=topic.id, is_authenticated=user_is_authenticated)
# without specifying, following query wouldn't select related properly
posts = topic.posts.select_related('user__userprofile',
'user__forum_profile',