def post(request, slug): if request.path[-1] == '/': return redirect(request.path[:-1]) post = get_object_or_404(Post, slug=slug) # Comments top_lvl_comments = Comment.objects.filter(post=post, parent=None) rankby = "hot" # Rank comments if rankby == "hot": ranked_comments = rank_hot(top_lvl_comments) elif rankby == "top": ranked_comments = rank_top(top_lvl_comments, timespan="all-time") elif rankby == "new": ranked_comments = top_lvl_comments.order_by('-pub_date') else: ranked_comments = [] # Nested comments comments = list(get_comment_list(ranked_comments, rankby=rankby)) # # Submit comments if request.method == 'POST': if chapter: submit_comment(request, chapter) else: submit_comment(request, post) form = CommentForm() # Footer info if request.user.is_authenticated(): upvoted = request.user.upvoted.all() # subscribed_to = request.user.subscribed_to.all() comments_upvoted = request.user.comments_upvoted.all() else: upvoted = [] # subscribed_to = [] comments_upvoted = [] # hubs = post.hubs.all() # Increment views counter. Do clever memcache laters. if not request.user.is_staff and request.user != post.author: post.views += 1 post.save() return render( request, 'posts/post.html', { 'post': post, 'upvoted': upvoted, 'comments': comments, 'comments_upvoted': comments_upvoted, 'form': form, # 'hubs':hubs, # 'subscribed_to':subscribed_to, })
def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['now'] = timezone.now() post = self.get_object() if not self.request.user.is_staff and self.request.user != post.author: post.views +=1 post.save() qs = super(PostDetailView, self).get_queryset() other_posts = qs.filter(series=post.series) context['first'] = other_posts.order_by('pub_date')[0] context['prev'] = post.prev_by_author() context['next'] = post.next_by_author() context['last'] = other_posts.order_by('-pub_date')[0] more_by = Post.objects.filter(author=post.author, published=True).order_by('?')[:4] context['more_by'] = more_by # next_post = post.get_next_by_pub_date(post, author=post.author) # next_post = next_or_prev_in_order(self, True, other_posts) ##### COMMENTS #### context['form'] = CommentForm() top_lvl_comments = Comment.objects.filter(post = post, parent = None) rankby = "new" # Rank comments # if rankby == "hot" or True: # ranked_comments = rank_hot(top_lvl_comments, top=32) # elif rankby == "top": # ranked_comments = rank_top(top_lvl_comments, timespan = "all-time") # elif rankby == "new": # ranked_comments = top_lvl_comments.order_by('-pub_date') # else: # ranked_comments = [] ranked_comments = top_lvl_comments.order_by('-pub_date') # Nested comments comments = list(get_comment_list(ranked_comments, rankby=rankby)) # if request.user.is_authenticated(): # comments_upvoted = request.user.comments_upvoted.all() # comments_downvoted = request.user.comments_downvoted.all() # else: # comments_upvoted = [] # comments_downvoted = [] context['comments'] = comments # 'comments_upvoted': comments_upvoted, # 'comments_downvoted': comments_downvoted, return context
def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['submitform'] = PostForm() categories = Category.objects.all() context['categories'] = categories post = self.object # Comments top_lvl_comments = Comment.objects.filter(post=post, parent=None) ranked_comments = top_lvl_comments.order_by('score', '-created_at') nested_comments = list(get_comment_list(ranked_comments, rankby="hot")) context['comments'] = nested_comments # Prev/next chapters if post.series: chapters = post.series.chapters.filter(published=True) this_index = 0 prev_chapter = 0 next_chapter = 0 for index, chapter in enumerate(chapters): if post == chapter: this_index = index if this_index > 0: prev_chapter = chapters[this_index - 1] if this_index + 1 < len(chapters) and chapters[this_index + 1].published: next_chapter = chapters[this_index + 1] context['chapters'] = chapters context['prev_chapter'] = prev_chapter context['next_chapter'] = next_chapter # Paywall paywall = False user = self.request.user # Show paywall if series isn't free, and isn't purchased by user, # and this isn't a free chapter if user != post.author and user.is_authenticated \ and post.series.price > 0 \ and not post.series in user.purchased_series.all() \ and this_index > post.series.free_chapters - 1: paywall = True if not user.is_authenticated \ and post.series.price > 0 \ and this_index > post.series.free_chapters - 1: paywall = "login" context['paywall'] = paywall # Increment view counter if self.request.user != post.author: post.views += 1 post.save() return context
def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['submitform'] = PostForm() categories = Category.objects.all() context['categories'] = categories post = self.object # Comments top_lvl_comments =Comment.objects.filter(post=post, parent = None) ranked_comments = top_lvl_comments.order_by('score', '-created_at') nested_comments = list(get_comment_list(ranked_comments, rankby="hot")) context['comments'] = nested_comments # Prev/next chapters if post.series: chapters = post.series.chapters.filter(published=True) this_index = 0 prev_chapter = 0 next_chapter = 0 for index, chapter in enumerate(chapters): if post == chapter: this_index = index if this_index > 0: prev_chapter = chapters[this_index - 1] if this_index + 1 < len(chapters) and chapters[this_index + 1].published: next_chapter = chapters[this_index + 1] context['chapters'] = chapters context['prev_chapter'] = prev_chapter context['next_chapter'] = next_chapter # Paywall paywall = False user = self.request.user # Show paywall if series isn't free, and isn't purchased by user, # and this isn't a free chapter if user != post.author and user.is_authenticated \ and post.series.price > 0 \ and not post.series in user.purchased_series.all() \ and this_index > post.series.free_chapters - 1: paywall = True if not user.is_authenticated \ and post.series.price > 0 \ and this_index > post.series.free_chapters - 1: paywall = "login" context['paywall'] = paywall # Increment view counter if self.request.user != post.author: post.views +=1 post.save() return context
def post(request, story, comment_id="", chapter="", rankby="new", filterby=""): try: story = Post.objects.get(slug=story) except: return HttpResponseRedirect('/404') try: # doesn't work if 2 chapters #1 first_chapter = Post.objects.get(parent=story, number=1) # first_chapter = story.children.filter(number=1)[0] except: first_chapter = [] # If chapter if chapter: chapter = Post.objects.get(parent=story,slug=chapter) first_chapter = [] # empty first chapter to show the right button in post template try: prev_chapter = Post.objects.get(parent=story, number=chapter.number-1) except: prev_chapter = [] try: next_chapter = Post.objects.get(parent=story, number=chapter.number+1) except: next_chapter = [] else: chapter = [] prev_chapter = [] next_chapter = [] hubs = story.hubs.all() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) # return post but don't save it to db just yet comment.author = request.user comment.parent = None if chapter: comment.post = chapter else: comment.post = story comment.save() if comment.comment_type == "comment": if chapter: return HttpResponseRedirect('/story/'+story.slug+'/'+chapter.slug+'#comments') else: return HttpResponseRedirect('/story/'+story.slug+'#comments') else: if chapter: return HttpResponseRedirect('/story/'+story.slug+'/'+chapter.slug+'/reviews#comments') else: return HttpResponseRedirect('/story/'+story.slug+'/reviews#comments') else: form = CommentForm() if request.user.is_authenticated(): upvoted = request.user.upvoted.all() downvoted = request.user.downvoted.all() else: upvoted = [] downvoted = [] # For subscribe button if not request.user.is_anonymous(): subscribed_to = request.user.subscribed_to.all() else: subscribed_to = [] # Get top lvl comments if filterby == "reviews": filterurl = "reviews" if chapter: top_lvl_comments = Comment.objects.filter(post = chapter, comment_type="review", parent = None) else: top_lvl_comments = Comment.objects.filter(post = story, comment_type="review", parent = None) else: if chapter: top_lvl_comments = Comment.objects.filter(post = chapter, comment_type="comment", parent = None) else: top_lvl_comments = Comment.objects.filter(post = story, comment_type="comment", parent = None) # Rank comments if rankby == "hot": ranked_comments = rank_hot(top_lvl_comments, top=32) elif rankby == "top": ranked_comments = rank_top(top_lvl_comments, timespan = "all-time") elif rankby == "new": ranked_comments = top_lvl_comments.order_by('-pub_date') else: ranked_comments = [] # Permalink to one comment if comment_id: comment = [] comment.append(Comment.objects.get(id = comment_id)) ranked_comments = comment # Nested comments comments = list(get_comment_list(ranked_comments, rankby=rankby)) if request.user.is_authenticated(): comments_upvoted = request.user.comments_upvoted.all() comments_downvoted = request.user.comments_downvoted.all() else: comments_upvoted = [] comments_downvoted = [] if chapter: post = chapter else: post = story if filterby: filterby = "/" + filterby else: filterby = "/comments" # Increment views counter. Do clever memcache laters. if not request.user.is_staff and request.user != post.author: post.views +=1 post.save() return render(request, 'posts/post.html',{ 'post': post, 'first_chapter':first_chapter, 'chapter': chapter, 'prev_chapter': prev_chapter, 'next_chapter': next_chapter, 'upvoted': upvoted, 'downvoted': downvoted, 'comments': comments, 'comments_upvoted': comments_upvoted, 'comments_downvoted': comments_downvoted, 'rankby': rankby, 'form': form, 'hubs':hubs, 'subscribed_to':subscribed_to, 'filterby':filterby })