def topic(request, topic_pk, topic_slug): ''' Display a thread and its posts using a pager ''' # TODO: Clean that up g_topic = get_object_or_404(PrivateTopic, pk=topic_pk) if not g_topic.author == request.user and not request.user in list( g_topic.participants.all()): raise Http404 # Check link if not topic_slug == slugify(g_topic.title): return redirect(g_topic.get_absolute_url()) if request.user.is_authenticated(): if never_privateread(g_topic): mark_read(g_topic) posts = PrivatePost.objects.filter(privatetopic__pk=g_topic.pk)\ .order_by('position_in_topic')\ .all() last_post_pk = g_topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = (last_page)[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) return render_template( 'mp/topic.html', { 'topic': g_topic, 'posts': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_post_pk': last_post_pk })
def topic(request, topic_pk, topic_slug): ''' Display a thread and its posts using a pager ''' # TODO: Clean that up g_topic = get_object_or_404(PrivateTopic, pk=topic_pk) if not g_topic.author == request.user and not request.user in list(g_topic.participants.all()): raise Http404 # Check link if not topic_slug == slugify(g_topic.title): return redirect(g_topic.get_absolute_url()) if request.user.is_authenticated(): if never_privateread(g_topic): mark_read(g_topic) posts = PrivatePost.objects.filter(privatetopic__pk=g_topic.pk)\ .order_by('position_in_topic')\ .all() last_post_pk = g_topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = (last_page)[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) return render_template('mp/topic.html', { 'topic': g_topic, 'posts': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_post_pk': last_post_pk })
def topic(request, topic_pk, topic_slug): """Display a thread and its posts using a pager.""" topic = get_object_or_404(Topic, pk=topic_pk) if not topic.forum.can_read(request.user): raise PermissionDenied # Check link if not topic_slug == slugify(topic.title): return redirect(topic.get_absolute_url()) # If the user is authenticated and has never read topic, we mark it as # read. if request.user.is_authenticated(): if never_read(topic): mark_read(topic) # Retrieves all posts of the topic and use paginator with them. posts = \ Post.objects.filter(topic__pk=topic.pk) \ .select_related() \ .order_by("position" ).all() last_post_pk = topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) # The category list is needed to move threads categories = Category.objects.all() try: page_nbr = int(request.GET["page"]) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = last_page[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) # Build form to send a post for the current topic. form = PostForm(topic, request.user) form.helper.form_action = reverse("zds.forum.views.answer") + "?sujet=" \ + str(topic.pk) form_move = MoveTopicForm(topic=topic) return render_template("forum/topic/index.html", { "topic": topic, "posts": res, "categories": categories, "pages": paginator_range(page_nbr, paginator.num_pages), "nb": page_nbr, "last_post_pk": last_post_pk, "form": form, "form_move": form_move, })
def topic(request, topic_pk, topic_slug): """Display a thread and its posts using a pager.""" topic = get_object_or_404(Topic, pk=topic_pk) if not topic.forum.can_read(request.user): raise PermissionDenied # Check link if not topic_slug == slugify(topic.title): return redirect(topic.get_absolute_url()) # If the user is authenticated and has never read topic, we mark it as # read. if request.user.is_authenticated(): if never_read(topic): mark_read(topic) # Retrieves all posts of the topic and use paginator with them. posts = \ Post.objects.filter(topic__pk=topic.pk) \ .select_related() \ .order_by("position" ).all() last_post_pk = topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) # The category list is needed to move threads categories = Category.objects.all() if "page" in request.GET: try: page_nbr = int(request.GET["page"]) except: # problem in variable format raise Http404 else: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = last_page[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) # Build form to send a post for the current topic. form = PostForm(topic, request.user) form.helper.form_action = reverse("zds.forum.views.answer") + "?sujet=" \ + str(topic.pk) form_move = MoveTopicForm(topic=topic) return render_template("forum/topic/index.html", { "topic": topic, "posts": res, "categories": categories, "pages": paginator_range(page_nbr, paginator.num_pages), "nb": page_nbr, "last_post_pk": last_post_pk, "form": form, "form_move": form_move, })