def post(self, request): blogger = User.objects.get(username=request.user) request.data.update({'blogger': blogger}) serializer = PostSerializer(data=request.data) if serializer.is_valid(): blogger = serializer.validated_data['blogger'] serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post_list(request, format=None): if request.method == 'GET': posts = Post.objects.all() 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 get(self, request): blogger = Post.objects.filter(blogger=self.request.user).order_by('id') if not blogger: raise exceptions.AuthenticationFailed('User inactive or deleted.') blogger = Post.objects.filter( id__in=[c for c in blogger[0].subscriptions]).order_by( 'time_created') page = self.paginate_queryset(blogger) if page is not None: serializer = PostSerializer(blogger, many=True) return self.get_paginated_response(serializer.data) serializer = PostSerializer(blogger, many=True) return Response(serializer.data)
def put(self, request, pk): blogger = User.objects.get(username=self.request.user) sub = Post.objects.filter(blogger=blogger).order_by('id') sublist = sub[0].subscriptions + str(pk) inst = self.get_object(sub[0].id) serializer = PostSerializer(inst, data={ 'subscriptions': sublist, 'blogger': blogger }, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): blogger = Post.objects.filter(blogger=self.request.user).order_by('id') if not blogger: raise exceptions.AuthenticationFailed('User inactive or deleted.') blogger = Post.objects.exclude( id__in=[c for c in blogger[0].readposts]).order_by('time_created') serializer = PostSerializer(blogger, many=True) return Response(serializer.data)
def get(self, request): postmen = request.GET.get('blogger') if not postmen: raise exceptions.AuthenticationFailed('No blogger set') blogger = Post.objects.filter(blogger=postmen).order_by('time_created') if not blogger: raise exceptions.AuthenticationFailed('User inactive or deleted.') serializer = PostSerializer(blogger, many=True) return Response(serializer.data)
def post_detail(request, pk, format=None): 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.HTTP_204_NO_CONTENT)
def apiblog(request): if request.method == 'GET': blog = Post.objects.all() serializer = PostSerializer(blog, many=True) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) test_data = { "title": "True Story", "content": "Implement a RESTful web service", "timestamp": "2016-10-25T13:20:20.473Z", "author": 4 } serializer = PostSerializer(data=data) permission_classes = (permissions.IsAuthenticatedOrReadOnly,) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status = 201) return JSONResponse(serializer.errors, status = 400)
def get(self, request, format=None): bloggers = Post.objects.order_by('blogger').distinct('blogger') if bloggers is None: # check the condition! raise Http404 for b in bloggers: q = Post.objects.filter(blogger=b.blogger).annotate(Count('title')) Post.objects.filter(blogger=b.blogger).update(posts=len(q)) bloggers = Post.objects.order_by('blogger', 'posts').distinct('blogger') serializer = PostSerializer(bloggers, many=True) return Response(serializer.data)
def get(self, request): abc = Post.objects.order_by('blogger').distinct('blogger') if abc is None: # check the condition! raise Http404 serializer = PostSerializer(abc, many=True) return Response(serializer.data)