def get(self, request, num_page=1): response = {'form': CommentForm()} if request.user.is_authenticated: sub_query_1 = BookLike.objects.filter( user=request.user, book=OuterRef('pk')).values('rate') sub_query_2 = Exists( User.objects.filter(id=request.user.id, book=OuterRef('pk'))) sub_query_3 = Exists( User.objects.filter(id=request.user.id, comment=OuterRef('pk'))) sub_query_4 = Exists( User.objects.filter(id=request.user.id, like=OuterRef('pk'))) comment = Comment.objects.annotate(is_owner=sub_query_3, is_liked=sub_query_4). \ select_related('user').prefetch_related('like') comment_prefetch = Prefetch('comment', comment) result = Book.objects.annotate(user_rate=Cast(sub_query_1, CharField()), is_owner=sub_query_2). \ prefetch_related(comment_prefetch, 'author', 'genre', 'rate') else: result = Book.objects. \ prefetch_related('author', 'genre', 'comment', 'comment__user').all() pag = Paginator(result, 5) response['content'] = pag.page(num_page) response['count_page'] = list(range(1, pag.num_pages + 1)) response['book_form'] = BookForm() response['comment_form'] = CommentForm() return render(request, 'index.html', response)
def post(self, request, book_id): if request.user.is_authenticated: cf = CommentForm(data=request.POST) comment = cf.save(commit=False) comment.user = request.user comment.book_id = book_id comment.save() return redirect('hello')
def post(self, request, book_id): if request.user.is_authenticated: new_comment = CommentForm(request.POST) if new_comment.is_valid(): nc = new_comment.save(commit=False) nc.user = request.user nc.book_id = book_id nc.save() return redirect("hello")
def post(self, request, comment_id): comment = Comment.objects.get(id=comment_id) form = CommentForm(instance=comment, data=request.POST) if form.is_valid(): comment.save() return redirect("hello") form = CommentForm(instance=comment) return render(request, "update_comment.html", { "form": form, "comment_id": comment_id })
def get(self, request, page_id=1): if request.user.is_authenticated: subquery_1 = Subquery( BookRate.objects.filter(book=OuterRef("pk"), user=request.user).values("rate")) subquery_2 = Exists( CommentLike.objects.filter(comment=OuterRef("pk"), user=request.user)) subquery_3 = Exists( User.objects.filter(book=OuterRef('pk'), id=request.user.id)) subquery_4 = Exists( User.objects.filter(comment=OuterRef("pk"), id=request.user.id)) queryset = Comment.objects.annotate(isliked=subquery_2, is_owner=subquery_4) \ .select_related('user').prefetch_related('like').order_by('date') prefetch = Prefetch("comment", queryset=queryset) content = Book.objects.annotate(user_rate=Cast(subquery_1, CharField()), is_owner=subquery_3). \ prefetch_related('genre', 'author', prefetch) else: content = Book.objects.prefetch_related('genre', 'author', 'comment__user') content = content.order_by('-publish_date') p = Paginator(content, 5) context = { "content": p.page(page_id), "comment_form": CommentForm(), "num_pages": range(1, p.num_pages + 1) } return render(request, "index.html", context)
def get(self, request, comment_id): if request.user.is_authenticated: comment = Comment.objects.get(id=comment_id) if comment.user == request.user: cf = CommentForm(instance=comment) return render(request, 'update_comment.html', {'form': cf, 'id': comment.id}) return redirect('hello')
def get(self, request, comment_id): if request.user.is_authenticated: comment = Comment.objects.get(id=comment_id) if request.user == comment.user: form = CommentForm(instance=comment) return render(request, "update_comment.html", { "form": form, "comment_id": comment_id }) return redirect("hello")
def post(self, request, id): comment = CommentForm(renderer=request, data=request.POST) if comment.is_valid(): new_comment = comment.save(commit=False) new_comment.book_id = id new_comment.user_id = request.user.id new_comment.save() comment.save_m2m() return redirect('hello')
def post(self, request, comment_id): comment = Comment.objects.get(id=comment_id) cf = CommentForm(instance=comment, date=request.POST) if cf.is_valid(): cf.save return redirect('hello')