From 49b895a8a03f3f7805974de80d8f64acb1c0ae6f Mon Sep 17 00:00:00 2001 From: Nathan Dinsmore Date: Mon, 13 May 2013 19:35:33 -0400 Subject: [PATCH] Added bookmarkable new post URLs --- .../templates/djangobb_forum/forum.html | 2 +- .../mobile/includes/topic_list.html | 2 +- djangobb_forum/templatetags/forum_extras.py | 27 ------------------- djangobb_forum/urls.py | 2 ++ djangobb_forum/views.py | 26 +++++++++++++++++- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/djangobb_forum/templates/djangobb_forum/forum.html b/djangobb_forum/templates/djangobb_forum/forum.html index 371515b..45f8219 100644 --- a/djangobb_forum/templates/djangobb_forum/forum.html +++ b/djangobb_forum/templates/djangobb_forum/forum.html @@ -54,7 +54,7 @@ {% endif %}

{% link topic %}

{% trans "by" %} {{ topic.user.username }} - {% if topic|has_unreads:user %}{% trans "(New Posts)" %}{% endif %} + {% if topic|has_unreads:user %}{% trans "(New Posts)" %}{% endif %} diff --git a/djangobb_forum/templates/djangobb_forum/mobile/includes/topic_list.html b/djangobb_forum/templates/djangobb_forum/mobile/includes/topic_list.html index 09370d4..0fd27d0 100644 --- a/djangobb_forum/templates/djangobb_forum/mobile/includes/topic_list.html +++ b/djangobb_forum/templates/djangobb_forum/mobile/includes/topic_list.html @@ -2,7 +2,7 @@ {% load forum_extras %}
    {% for topic in topics %}{% with unread=topic|has_unreads:user %}
  1. - + {{ topic.name }} {% blocktrans count topic.reply_count as replies %}{{ replies }} reply{% plural %}{{ replies }} replies{% endblocktrans %} diff --git a/djangobb_forum/templatetags/forum_extras.py b/djangobb_forum/templatetags/forum_extras.py index 9f8bd83..4571cdb 100644 --- a/djangobb_forum/templatetags/forum_extras.py +++ b/djangobb_forum/templatetags/forum_extras.py @@ -137,33 +137,6 @@ def has_unreads(topic, user): return False return True -def forum_unread_object(topic, user): - """ - Returns the first unread post in a topic, or the topic if it post tracking does not exist for the given user. - """ - if user.posttracking is not None: - topics = user.posttracking.topics; - if isinstance(topics, dict): - pk = topics.get(str(topic.id), 0) - return Post.objects.filter(topic=topic, pk__gt=pk).order_by('pk')[0] - last_read = user.posttracking.last_read - if last_read is not None: - posts = Post.objects.filter(Q(topic=topic) & (Q(created__gte=last_read) | Q(updated__gte=last_read))).order_by('pk') - try: - return posts[0] - except Post.DoesNotExist: - pass - - return topic.posts.all()[0] - -@register.filter -def forum_unread_link(topic, user): - return forum_unread_object(topic, user).get_absolute_url(); - -@register.filter -def mobile_unread_link(topic, user): - return forum_unread_object(topic, user).get_mobile_url(); - @register.filter def forum_unreads(forum, user): """ diff --git a/djangobb_forum/urls.py b/djangobb_forum/urls.py index 68f2f56..fabb58f 100644 --- a/djangobb_forum/urls.py +++ b/djangobb_forum/urls.py @@ -23,6 +23,7 @@ urlpatterns = patterns('', # Topic url('^topic/(?P\d+)/$', forum_views.show_topic, name='topic'), + url('^topic/(?P\d+)/unread/$', forum_views.show_unread_posts, name='topic_unread'), url('^topic/(?P\d+)/title/$', forum_views.get_topic_title, name='topic_title'), url(r'^(?P\d+)/topic/add/$', forum_views.add_topic, name='add_topic'), url('^topic/(?P\d+)/delete_posts/$', forum_views.delete_posts, name='delete_posts'), @@ -67,6 +68,7 @@ if (forum_settings.LOFI_SUPPORT): url('^m/post/(?P\d+)/$', forum_views.show_post, {'full':False}, name='mobile_post'), url('^m/post/(?P\d+)/reply/$', forum_views.mobile_reply, name='mobile_reply'), url('^m/topic/(?P\d+)/$', forum_views.show_topic, {'full':False}, name='mobile_topic'), + url('^m/topic/(?P\d+)/unread/$', forum_views.show_unread_posts, {'full':False}, name='mobile_topic_unread'), ) # REPUTATION Extension diff --git a/djangobb_forum/views.py b/djangobb_forum/views.py index e160a09..a49b2a4 100644 --- a/djangobb_forum/views.py +++ b/djangobb_forum/views.py @@ -15,7 +15,7 @@ from django.core.urlresolvers import reverse from django.db import transaction from django.db.models import Q, F from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseForbidden -from django.shortcuts import get_object_or_404, render +from django.shortcuts import get_object_or_404, render, redirect from django.utils.encoding import smart_str from django.utils.translation import ugettext as _ from django.utils import timezone @@ -491,6 +491,30 @@ def show_topic(request, topic_id, full=True): 'reply_form': reply_form, }) +def show_unread_posts(request, topic_id, full=True): + post = None + user = request.user + topic = get_object_or_404(Topic, pk=topic_id) + if user.posttracking is not None: + topics = user.posttracking.topics; + if isinstance(topics, dict): + pk = topics.get(str(topic_id), 0) + try: + post = Post.objects.filter(topic=topic, pk__gt=pk).order_by('pk')[0] + except IndexError: + pass + else: + last_read = user.posttracking.last_read + if last_read is not None: + posts = Post.objects.filter(Q(topic=topic) & (Q(created__gte=last_read) | Q(updated__gte=last_read))).order_by('pk') + try: + post = posts[0] + except Post.DoesNotExist: + pass + if post is None: + post = topic.last_post + + return redirect(post.get_absolute_url() if full else post.get_mobile_url()); @login_required @transaction.commit_on_success