def get(self, request, num_page=1): response = {} 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() response['form'] = CommentForm() pag = Paginator(result, 3) response['content'] = pag.page(num_page) response['count_page'] = list(range(1, pag.num_pages + 1)) response['bookform'] = BookForm() return render(request, "index.html", response)
def get(self, request, id): book = Book.objects.get(id=id) bf = BookForm(instance=book) return render(request, 'update_book.html', { 'book_form': bf, "id": book.id })
def get(self, request, book_slug): if request.user.is_authenticated: book = Book.objects.get(slug=book_slug) if request.user in book.author.all(): bf = BookForm(instance=book) return render(request, "update_book.html", {"form": bf, "slug": book.slug}) return redirect("hello")
def post(self, request): bf = BookForm(renderer=request, data=request.POST) if bf.is_valid(): new_book = bf.save(commit=False) new_book.slug = slugify(bf.cleaned_data['title']) new_book.save() new_book.author.add(request.user) bf.save_m2m() return redirect('hello')
def get(self, request, book_slug): if request.user.is_authenticated: book = Book.objects.get(slug=book_slug) if request.user in book.author.all(): bf = BookForm(instance=book) return render(request, 'update_book.html', { 'form': bf, 'slug': book_slug }) return redirect('hello')
def post(self, request): book = BookForm(data=request.POST) if book.is_valid(): nb = book.save(commit=False) nb.slug = slugify(nb.title) try: nb.save() except IntegrityError: nb.slug += datetime.now().strftime("%Y:%m:%d:%H:%M:%S:%f") nb.save() nb.author.add(request.user) book.save_m2m() return redirect('hello') return redirect('add_book')
def post(self, request, book_slug): book = Book.objects.get(slug=book_slug) bf = BookForm(instance=book, data=request.POST) if bf.is_valid(): bf.save() return redirect("hello") # class AddLikeAjax(View): # def post(self, request): # if request.user.is_authenticated: # cl_id = request.POST['cl_id'][3:] # flag = CommentLike(user=request.user, comment_id=cl_id).save() # comment = Comment.objects.get(id=cl_id) # return JsonResponse({"ok": True, # 'count_like': comment.cached_like, # 'flag': flag, # 'user': request.user.username # }) # return JsonResponse({"ok": False}) # # # class AddRateAjax(View): # def post(self, request): # if request.user.is_authenticated: # book_id = request.POST['book_id'] # rate = request.POST['book_rate'] # bl = BookLike(user=request.user, book_id=book_id, rate=rate) # flag = bl.save() # bl.book.refresh_from_db() # return JsonResponse({'user': request.user.username, "flag": flag, 'cached_rate': bl.book.cached_rate, 'rate': bl.rate }) # return JsonResponse({"ok": False}) # # # class DeleteCommentAjax(View): # def delete(self, request, comment_id): # if request.user.is_authenticated: # Comment.objects.filter(id=comment_id, user=request.user).delete() # return JsonResponse({"ok": False}) # # # class AddNewBookAjax(View): # def post(self): # pass
def post(self, request, id): book = Book.objects.get(id=id) f = BookForm(request.POST, instance=book) f.save() return redirect('hello')
def get(self, request): bf = BookForm() return render(request, "add_new_book.html", {'book_form': bf, 'id': 0})
def get(self, request): form = BookForm() return render(request, "create_book.html", {"form": form})
def post(self, request, book_slug): book = Book.objects.get(slug=book_slug) bf = BookForm(instance=book, data=request.POST) if bf.is_valid(): bf.save() return redirect('hello')
def get(self, request): form = BookForm() return render(request, 'create_book.html', {'form': form})