def post_view(request, post_pk): if 'tree' in request.GET: is_tree = bool(request.GET.get('tree')) res = redirect('post_view', post_pk=post_pk) if is_tree: res.set_cookie('comments_tree', is_tree) else: res.delete_cookie('comments_tree') return res is_tree = request.COOKIES.get('comments_tree') page = request.GET.get('page', 1) post = BlogInterface(request.user).get_post_with_comments(post_pk, as_tree=is_tree, get_recommends=True) comments = post.comments_post try: page = int(page) except ValueError: return redirect('post_view', post_pk) paginate = Paginator(comments, COMMENTS_PER_PAGE) try: page = paginate.page(page) except (EmptyPage, InvalidPage): page = paginate.page(paginate.num_pages) comments = page.object_list reply_to = request.GET.get('reply_to') if reply_to: reply_to = get_object_or_404(Comment, post=post_pk, number=reply_to) form_p = FormProcessor(PostForm, request) form_p.process() if form_p.is_valid(): message = form_p.data['body'] Comment.add_comment(post, request.user, message, reply_to) return redirect('post_view', post_pk=post.pk) context = {} context['post'] = post context['user_blog'] = post.user context['page'] = page context['comments'] = comments context['is_tree'] = is_tree context['recommends'] = post.recommend_users context['reply_form'] = form_p.form context['reply_to'] = reply_to return render_template(request, 'blog/post_view.html', context)
def reply_add(request, post_pk, reply_to=None): post = get_object_or_404(Post, pk=post_pk) if reply_to: reply_to = get_object_or_404(Comment, post=post_pk, number=reply_to) form_p = FormProcessor(PostForm, request) form_p.process() if form_p.is_valid(): user = request.user data = form_p.data message = data['body'] Comment.add_comment(post, user, message, reply_to) return redirect('post_view', post_pk=post.pk) context = {} context['form'] = form_p.form return render_template(request, 'blog/post_add.html', context)
def add_reply(self, text, post_id, reply_number=None, from_client='web'): from models import BlackList, Comment user = self.user post = self.get_post(post_id) if user.pk != post.user_id and post.tags.filter(name='readonly'): raise PostInterfaceError( "Sorry, you can't reply to this post, it is *readonly.") if BlackList.objects.filter(user=post.user_id, blacklisted_user=user).exists(): raise PostInterfaceError( "Sorry, you can't reply to this user posts.") reply_to = None if reply_number: reply_to = self.get_comment(post_id, reply_number) reply = Comment.add_comment(post, user, text, reply_to, from_client=from_client) return reply