def blog_detail(request, pk): try: blog = Blog.objects.get(pk=pk) except blog.DoesNotExist: return JsonResponse({'message': 'The blog does not exist'}, status=status.HTTP_404_NOT_FOUND) # find blog by pk (id) if request.method == 'GET': blog_serializer = BlogSerializer(blog) return JsonResponse(blog_serializer.data) elif request.method == 'PUT': blog_data = JSONParser().parse(request) blog_serializer = BlogSerializer(blog, data=blog_data) if blog_serializer.is_valid(): blog_serializer.save() return JsonResponse(blog_serializer.data) return JsonResponse(blog_serializer.errors, status=status.HTTP_400_BAD_REQUEST) # GET / PUT / DELETE blog elif request.method == 'DELETE': blog.delete() return JsonResponse({'message': 'blog was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
def serializer_put(request, requested_blog_id): blog = Blog.objects.get(id=requested_blog_id) serializer = BlogSerializer(blog, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
def post(self, request): serializer = BlogSerializer(data=request.data) if serializer.is_valid(): serializer.save() return create_response(data=serializer.data, status=status.HTTP_201_CREATED) return create_response(error=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, format=None): payload = request.data payload['user_id'] = request.user.id serializer = BlogSerializer(data=request.data) if (serializer.is_valid()): serializer.save() obj = {"success": True, "message": "Blog created Successfully!"} return Response(obj, status=status.HTTP_201_CREATED) obj = {"success": False, "message": serializer.errors} return Response(obj, status=status.HTTP_400_BAD_REQUEST)
def blog_collection(request): if request.method == 'GET': blogs = Blog.objects.all() serializer = BlogSerializer(blogs, many=True) return Response(serializer.data) elif request.method == 'POST': data = {'title': request.data.get('title'), 'content': request.data.get('content')} serializer = BlogSerializer(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 post(self, request, format=None, **kwargs): data = request.data data['author'] = self.request.user.id serializer = BlogSerializer(data=data) if serializer.is_valid(): serializer.save() return Response({"blog": serializer.data})
def blog_list_api(request): """ List all code blogs, or create a new blog. """ if request.method == 'GET': snippets = Blog.objects.all() serializer = BlogSerializer(snippets, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = BlogSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def blog_list_api(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': blogs = Blog.objects.all() serializer = BlogSerializer(blogs, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = BlogSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def blog_list(request): """" List all blogs """ if request.method =="GET": blogs=Blog.objects.all() serializer=BlogSerializer(blogs,many=True) return JsonResponse(serializer.data, safe=False) elif request.method =="POST": data = JSONParser().parse(request) serializer=BlogSerializer(data=data) if serializer.is_valid(): serializer.save() print(data) return JsonResponse(serializer.data, status=201) return JsonResponse (serializer.errors, status=400)
def blog_detail(request, pk): # find blog by id try: blog = Blog.objects.get(pk=pk) # retrive a single blog if request.method == 'GET': blog_serializer = BlogSerializer(blog) return JsonResponse(blog_serializer.data) # Update a Blog by the id in the request elif request.method == 'PUT': blog_data = JSONParser().parse(request) blog_serializer = BlogSerializer(blog, data=blog_data) if blog_serializer.is_valid(): blog_serializer.save() return JsonResponse(blog_serializer.data) return JsonResponse(blog_serializer.errors, status=status.HTTP_400_BAD_REQUEST) # Delete a Blog with the specified id elif request.method == 'DELETE': blog.delete() return JsonResponse({'message':'Blod was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT) except Blog.DoesNotExist: return JsonResponse({'message':'The blog does not exist'}, status=status.HTTP_404_NOT_FOUND)
def update(self, request, pk=None): user = request.user queryset = Blog.objects.filter(user_id=user.id).get(pk=pk) if not queryset: obj = {"success": False, "message": "Blog Not Found!"} return Response(obj, status=status.HTTP_401_UNAUTHORIZED) payload = request.data payload['user_id'] = user.id serializer = BlogSerializer(queryset, data=payload) if (serializer.is_valid()): serializer.save() obj = {"success": True, "message": "Blog Updated Successfully!"} return Response(obj, status=status.HTTP_201_CREATED) obj = { "success": False, "message": serializer.errors, } return Response(obj, status=status.HTTP_400_BAD_REQUEST)
def blogs_list(request): # GET list of blogs, POST a new blog, DELETE all blogs if request.method == 'POST': blog_data = JSONParser().parse(request) blog_serializer = BlogSerializer(data=blog_data) if blog_serializer.is_valid(): blog_serializer.save() return JsonResponse(blog_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(blog_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'GET': blogs = Blog.objects.all() title = request.GET.get('title', None) if title is not None: blogs = blogs.filter(title__icontains=title) blogs_serializer = BlogSerializer(blogs, many=True) return JsonResponse(blogs_serializer.data, safe=False)
def blog_list(request): # retrive all blogs if request.method == 'GET': blogs = Blog.objects.all() title = request.GET.get('title', None) if title is not None: blogs = blogs.filter(title_icontains=title) blogs_serializer = BlogSerializer(blogs, many=True) return JsonResponse(blogs_serializer.data, safe=False) # 'safe=False' for objects serialization # delete all blogs elif request.method == 'DELETE': count = Blog.objects.all().delete() return JsonResponse({'message': '{} Blogs were deleted successfully!'.format(count[0])}, status=status.HTTP_204_NO_CONTENT) # Create and save new blog elif request.method == 'POST': blog_data = JSONParser().parse(request) blog_serializer = BlogSerializer(data=blog_data) if blog_serializer.is_valid(): blog_serializer.save() return JsonResponse(blog_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(blog_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def blog_detail_api(request, blog_id): """ Retrieve, update or delete a code snippet. """ try: blog = Blog.objects.get(id=blog_id) except Blog.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = BlogSerializer(blog) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = BlogSerializer(blog, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': blog.delete() return HttpResponse(status=204)
def post(self, request, *args, **kwargs): blogSerializer = BlogSerializer(data=request.data) if blogSerializer.is_valid(): blog = blogSerializer.save() for image in self.request.FILES.getlist('image'): blogImageSerializer = BlogImageSerializer(data={ 'blog': blog.id, 'image': image }) if blogImageSerializer.is_valid(): blogImageSerializer.save() else: return Response(blogImageSerializer.errors, status.HTTP_403_FORBIDDEN) else: return Response(blogImageSerializer.errors, status.HTTP_403_FORBIDDEN) return Response(blogSerializer.data, status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): serializer = BlogSerializer(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)