예제 #1
0
파일: views.py 프로젝트: vovagod/blog
 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)
예제 #2
0
파일: views.py 프로젝트: chiho924/pyweb09b
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)
예제 #3
0
파일: views.py 프로젝트: vovagod/blog
    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)
예제 #4
0
파일: views.py 프로젝트: vovagod/blog
 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)
예제 #5
0
파일: views.py 프로젝트: vovagod/blog
    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)
예제 #6
0
파일: views.py 프로젝트: vovagod/blog
 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)
예제 #7
0
파일: views.py 프로젝트: chiho924/pyweb09b
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)
예제 #8
0
파일: views.py 프로젝트: dawitnida/PyLabs
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)
예제 #9
0
파일: views.py 프로젝트: vovagod/blog
 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)
예제 #10
0
파일: views.py 프로젝트: vovagod/blog
 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)