def posts_details(request, pk): try: post = Post.objects.get(pk=pk) except Post.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = PostSerializer(post) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = PostSerializer(post, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'PATCH': data = JSONParser().parse(request) serializer = PostSerializer(post, data=data, partial=True) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': post.delete() return HttpResponse(status=204)
def post_detail(request, pk): """ Retrieve, update or remove specific post """ try: post = Post.objects.get(pk = pk) except Post.DoesNotExist: return Response(status = status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PostSerializer(post) return Response(serializer.data) elif request.method == 'PUT': serializer = PostSerializer(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': post.delete() return Response(status = status.HTTP_204_NO_CONTENT)
def add_new_post(request): if request.method == 'POST': data = JSONParser().parse(request) serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) else: return JsonResponse(serializer.errors, status=400)
def post_list(request, format=None): if request.method == "GET": posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': serializers = PostSerializer(data=request.data) if serializers.is_valid(): serializers.save() return Response(serializers.data, status=status.HTTP_201_CREATED) return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)
def post_list(request): if request.method == "GET": posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return JSONResponse(serializer.data) elif request.method == "POST": data = JSONParser.parse(request) serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save(author=request.user) return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def post_list(request): if request.method == 'GET': posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def post_list_rest_api(request, format=None): ''' List all urls, or create a new url ''' if request.method == 'GET': post = Post.objects.all() serializer = PostSerializer(post, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = PostSerializer(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 post(self, request, format=None): serializer = PostSerializer(data=request.data,context={'user_id': request.user.id}) if serializer.is_valid(): serializer_2 = PostSerializer(serializer.save()) r.hset("POSTS",serializer_2.data["id"],json.dumps(serializer_2.data)) return Response(json.dumps(serializer.data), status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): serializer = PostSerializer(data=request.data,context={'user_id': request.user.id,'method':'PUT','id':pk}) if serializer.is_valid(): serializer_2 = PostSerializer(serializer.save()) r.hset("POSTS",pk,json.dumps(serializer_2.data)) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def PostList(request): """ Create or list Posts """ if request.method=='GET': post = Post.objects.all() serializer = PostSerializer(post, many=True) return Response(serializer.data) elif request.method=='POST': serializer = PostSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post_list(request): """ List latest 10 post lists, or create a post """ if request.method == 'GET': posts = Post.objects.all()[:10] serializer = PostSerializer(posts, many = True) return Response(serializer.data) elif request.method == 'POST': serializer = PostSerializer(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 post_detail(request, pk, format=None): post = get_object_or_404(Post, pk=pk) if request.method == "GET": serializer = PostSerializer(post) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = PostSerializer(post, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': post.delete() return HttpResponse(status=400)
def update_post(request, id_post): try: post_object = Post.objects.get(id=id_post) except Post.DoesNotExist: return HttpResponse('Błąd serwera!', status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer_post = PostSerializer(post_object) return JsonResponse(serializer_post.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer_post = PostSerializer(post_object, data=data) if serializer_post.is_valid(): serializer_post.save() return JsonResponse(serializer_post.data, status=201) return JsonResponse(serializer_post.errors, status=400) elif request.method == 'DELETE': post_object.delete() return HttpResponse(status=204)
def create(self, request, format='html'): serializer = PostSerializer(data=request.data) if serializer.is_valid(): post = serializer.save(author=request.user, published_date=timezone.now()) return self.retrieve(request, post.pk, format) else: return Response({'errors': serializer.errors}, template_name='blog/post_edit.html', status=400)
def post(self, request, *args, **kwargs): ser_post = PostSerializer(data = request.data) if ser_post.is_valid(): post_obj = ser_post.save() ser_obj = PostSerializer(post_obj) return Response(ser_obj.data, status = status.HTTP_201_CREATED) else: return Response(ser_post.errors, status = status.HTTP_400_BAD_REQUEST)
def post_detail_rest_api(request, pk, format=None): ''' Retrieve, update or delete url instance ''' try: post = Post.objects.get(pk=pk) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PostSerializer(post) return Response(serializer.data) elif request.method == 'PUT': serializer = PostSerializer(post, 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': post.delete() return Response(status=status.HTP_204_NO_CONTENT)
def post_detail(request, pk): """ Retrieve, update or delete a category. """ try: post = Post.objects.get(pk=pk) except Post.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = PostSerializer(post) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = PostSerializer(post, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': post.delete() return HttpResponse(status=204)
def add_post(request): serializer = PostSerializer(data=request.data) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data)