def create(self, request): serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) time.sleep(2) return Response(serializer.errors, status=400)
def put(self, request, pk, format=None): book = self.get_object(pk) serializer = BookSerializer(book, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): book = self.get_object(pk) serializer = BookSerializer(book, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_add(request): if request.method == 'POST': serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_list(request): if request.method == 'GET': books = Book.objects.all() isbn = request.GET.get('isbn', None) if isbn is not None: books = books.filter(book__icontains=isbn) book_serializer = BookSerializer(books, many=True) return JsonResponse(book_serializer.data, safe=False) elif request.method == 'POST': book_data = JSONParser().parse(request) book_serializer = BookSerializer(data=book_data) if book_serializer.is_valid(): book_serializer.save() return JsonResponse(book_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': count = Book.objects.all().delete() return JsonResponse( { 'message': '{} Books were deleted successfully!'.format( count[0]) }, status=status.HTTP_204_NO_CONTENT)
def post(self, request, format=None): data = JSONParser().parse(request) serializer = BookSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_add(request): if request.method == 'POST': serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): time.sleep(3) try: item = Book.objects.get(pk=pk) except Book.DoesNotExist: return Response(status=404) serializer = BookSerializer(item, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=400)
def book_list(request): if request.method == 'GET': books = Book.objects.all() serializer = BookSerializer(books, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): queryset = self.get_queryset() serializer = BookSerializer(queryset, data=request.data) if serializer.is_valid(): serializer.save() return make_formatted_response( status_code=status.HTTP_200_OK, data=serializer.data, message=f'The book {queryset.name} was updated successfully') return make_formatted_response(status_code=status.HTTP_400_BAD_REQUEST, error=serializer.errors)
def put(self, request, pk=None): try: book = Book.objects.get(pk=pk) except Book.DoesNotExist: return Response({'message': 'The book does not exist'}, status=status.HTTP_404_NOT_FOUND) book_serializer = BookSerializer(book, data=request.data) if book_serializer.is_valid(): book_serializer.save() return Response(book_serializer.data) return Response(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_books_view(request): #request.data['teste'] #request.data['sell_price'] = calcular_valor_por_margem(request.data['margem'], request.data['cost_price']) if request.method == "POST": serializer = BookSerializer(data=request.data) data = {} if serializer.is_valid(): serializer.save() data['success'] = "Book created with success" return Response(data=data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_list(request): if request.method == 'GET': snippets = Books.objects.all() serializer = BookSerializer(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = BookSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def book_list(request): if request.method == 'GET': books = Book.objects.all() book_serializer = BookSerializer(books, many=True) return JSONResponse(book_serializer.data) elif request.method == 'POST': book_data = JSONParser().parse(request) book_serializer = BookSerializer(data=book_data) if book_serializer.is_valid(): book_serializer.save() return JSONResponse(book_serializer.data, status=status.HTTP_201_CREATED) else: return JSONResponse(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): form = GoogleBookAPISearchForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data url = self.construct_url(cleaned_data) resp = requests.get(url).json() if resp: volumes = resp.json()["items"] for volume in volumes: volume_info = self.prepare_to_serialize(volume) if volume_info: serializer = BookSerializer(data=volume_info) if serializer.is_valid(): serializer.save() return redirect(reverse("expand"))
def update_books_view(request, slug): try: book = Book.objects.get(slug=slug) except Book.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "PUT": serializer = BookSerializer(book, data=request.data) data = {} if serializer.is_valid(): serializer.save() data['success'] = "Book updated with success" return Response(data=data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): book_serializer = BookSerializer(data=request.data) if book_serializer.is_valid(): saved_book_data = book_serializer.save() return Response(book_serializer.data, status=status.HTTP_201_CREATED) return Response(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_read_only_fields_should_not_being_altered(self): """ We check that read only fields of serializers are not altered """ serializer = BookSerializer(self.book, data={'total_rating': 5}, partial=True) self.assertEquals(serializer.is_valid(), True) instance = serializer.save() self.assertEquals(serializer.errors, {}) # Avg_rate should be unchanged self.assertEquals(instance.total_rating, Decimal('0.00'))
def book_detail(request, book_id): try: book = Book.objects.get(id=book_id) except Book.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = BookSerializer(book) return Response(serializer.data) elif request.method == 'PUT': serializer = BookSerializer(book, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': book.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def book_detail(request, pk): try: book = Book.objects.get(pk=pk) except: return JSONResponse(None, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': book_serializer = BookSerializer(book) return JSONResponse(book_serializer.data) elif request.method == 'PUT': book_data = JSONParser().parse(request) book_serializer = BookSerializer(book, data=book_data) if book_serializer.is_valid(): book_serializer.save() return JSONResponse(book_serializer.data) else: return JSONResponse(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': book.delete() return JSONResponse(None, status=status.HTTP_204_NO_CONTENT)
def book_detail(request, pk): try: book = Book.objects.get(pk=pk) except Book.DoesNotExist: return JsonResponse({'message': 'The book does not exist'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': book_serializer = BookSerializer(book) return JsonResponse(book_serializer.data) elif request.method == 'PUT': book_data = JSONParser().parse(request) book_serializer = BookSerializer(book, data=book_data) if book_serializer.is_valid(): book_serializer.save() return JsonResponse(book_serializer.data) return JsonResponse(book_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': book.delete() return JsonResponse({'message': 'Book was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
def book_detail(request, pk): try: snippet = Books.objects.get(pk=pk) except Books.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = BookSerializer(snippet) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = BookSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204)
def post(self, request): book = request.data.get('book') serializer = BookSerializer(data=book) if serializer.is_valid(raise_exception=True): book_saved = serializer.save() return Response( { 'success': 'Book \'{book}\' added successfully'.format( book=book_saved.__str__(), ), }, status=201, )
def put(self, request, book_id): book = get_object_or_404(Book.objects.filter(id=book_id)) book_upd = request.data.get('book') serializer = BookSerializer(instance=book, data=book_upd) if serializer.is_valid(raise_exception=True): book_saved = serializer.save() return Response( { 'success': 'Book \'{book}\' updated successfully'.format( book=book_saved.__str__(), ), }, status=204, )
def book_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: book = Book.objects.get(pk=pk) except Book.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = BookSerializer(book) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = BookSerializer(book, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': book.delete() return HttpResponse(status=204)
def post(self, request, format=None): serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=self.request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)