예제 #1
0
def post_add(request):
    if request.method == 'PUT':
        data = JSONParser().parse(request)
        if 'post_data' in data:
            hash = calcHash(data['post_data'])
            data['post_uniqueCode'] = hash
        else:
            return HttpResponse("Bad syntax", status=400)

        serializer = PostSerializer(data=data)
        if serializer.is_valid():
            try:
                post = Post.objects.get(
                    post_uniqueCode=data['post_uniqueCode'])
                if post != None:
                    return HttpResponse(
                        "post with requested code already exists", status=409)
            except Post.DoesNotExist:
                serializer.save()
                id = serializer.data['post_id']
                return HttpResponse(str(id), status=200)
        else:
            return HttpResponse("Bad syntax", status=400)
    else:
        return HttpResponse(status=404)
예제 #2
0
 def post(self, request):
     serializer = PostSerializer(data=request.data)
     print(request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #3
0
def get_all_posts(request):
    """
        Retorna um json com todos os post cadastrados
    """

    response = {
        "status": "failure",
    }

    if request.method == 'GET':
        posts = Post.objects.all()
        serializer = PostSerializer(posts, many=True)
        response = {
            "status": "success",
            "shows": serializer.data,
        }
        return Response(response)

    elif request.method == 'POST':
        serializer = PostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            response = {
                "status": "success",
                "shows": serializer.data,
            }
            return Response(response, status=status.HTTP_201_CREATED)
        response = {
            "status": "failure",
            "errors": serializer.errors,
        }
        return Response(response, status=status.HTTP_400_BAD_REQUEST)

    return Response(response)
예제 #4
0
 def post(self, request):
     serialized = PostSerializer(data=request.data)
     if serialized.is_valid(raise_exception=True):
         serialized.save()
         response = {"data": serialized.data}
         return Response(response, status=201)
     return Response({"message": "Error - not created"}, status=400)
예제 #5
0
 def put(self, request, id):
     post = self.get_post(id)
     serializer = PostSerializer(instance=post, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors})
예제 #6
0
    def post(self, request):
        data = JSONParser().parse(request)
        serialized = PostSerializer(data=data)

        if serialized.is_valid():
            serialized.save()
            return JsonResponse(serialized.data, status=status.HTTP_201_CREATED)
    def put(self, request, pk, format=None):
        data = request.data

        # ensure user is authenticated
        if (not request.user.is_authenticated()):
            return Response({'message':'Not authenticated'}, status=status.HTTP_401_UNAUTHORIZED)

        try:
            post = Post.objects.get(id=pk)
        except Post.DoesNotExist as e:
            return Response({"message":"Post does not exist"}, status=status.HTTP_404_NOT_FOUND)

        # only allow author of the post to modify it
        try:
            loggedInAuthor = Author.objects.get(user=request.user)
        except Author.DoesNotExist as e:
            return Response({"message":"Author does not exist"}, status=status.HTTP_401_UNAUTHORIZED)

        # only allow author of the post to modify it
        if loggedInAuthor != post.author:
            return Response({"message": "User is not the author of this post & is not allowed to update this post"}, status=status.HTTP_403_FORBIDDEN)


        # else logged in user is the author of the post
        serializer = PostSerializer(post, data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #8
0
def json_to_posts(json, node):
    posts = json["posts"]
    out = []
    for p in posts:
        guid = p.get('guid', None)
        if guid is None:
            # Bad json
            continue

        post = Post.objects.filter(guid=guid).first()
        # Try except here because if they pass us a post
        # from a host we don't know then we error out and 
        # do nothing, realistically we should just pass over
        # that post... I know Mark doesn't like this but meh
        # trying to get thigns to work...
        try:
            if post is None:
                # create post
                serializer = PostSerializer(data=p)
                serializer.is_valid(raise_exception=True)
                post = serializer.save(node=node)
            else:
                # update post
                serializer = PostSerializer(post, data=p)
                serializer.is_valid(raise_exception=True)
                post = serializer.save(node=node)
                out.append(post)
        except Exception as e:
            logger.log("Error serializing post: "+ str(e) + "\r\n Post JSON:" + str(p))
            continue
    return out
예제 #9
0
 def put(self, request, user_id, pid, format=None):
     post = self.get_objects(user_id, pid)
     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)
예제 #10
0
def get_all_posts(request):
    """
        Retorna um json com todos os post cadastrados
    """

    response = {
        "status": "failure",
    }

    if request.method == 'GET':
        posts = Post.objects.all()
        serializer = PostSerializer(posts, many=True)
        response = {
            "status": "success",
            "shows": serializer.data,
        }
        return Response(response)

    elif request.method == 'POST':
        serializer = PostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            response = {
                "status": "success",
                "shows": serializer.data,
            }
            return Response(response, status=status.HTTP_201_CREATED)
        response = {
            "status": "failure",
            "errors": serializer.errors,
        }
        return Response(response, status=status.HTTP_400_BAD_REQUEST)

    return Response(response)
예제 #11
0
def api_create_blog_view(request):
    account = User.objects.get(pk=1)
    post = Post(author=account)

    if request.method == 'POST':
        serializer = PostSerializer(post, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# User views
# @api_view(['POST'])
# def api_registration_view(request):
#     if request.method == 'POST':
#         serializer = RegistrationSerializer(data=request.data)
#         data = {}
#         if serializer.is_valid():
#             user = serializer.save()
#             data['response'] = 'successfully registered a new user'
#             data['email'] = user.email
#             data['username'] = user.username
#         else:
#             data = serializer.errors
#         return Response(data)
예제 #12
0
    def post(self, request):  #Allows user to create a new post
        serializer = PostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(
                owner=request.user)  #Turns data into actual book object
            return Response(serializer.data, status=201)

        return Response(serializer.errors, status=400)
예제 #13
0
    def put(self, request, id):
        post = self.get_object(id)
        serializer = PostSerializer(post, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #14
0
 def post(self, request, pk):
     post = self.get_object(pk)
     post.like_count += 1
     request.data['title'] = post.title
     serializer = PostSerializer(instance=post, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors)
예제 #15
0
 def post(self, request, id):
     category = self.get_category(id)
     profile = Profile.objects.get(user=self.request.user)
     serializer = PostSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save(category=category, author_id=profile.id)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #16
0
    def put(self, request, id):
        post = self.get_object(id)
        data = JSONParser().parse(request)
        serialized = PostSerializer(post, data=data)

        if serialized.is_valid():
            serialized.save()
            return JsonResponse(serialized.data, status=status.HTTP_202_ACCEPTED)

        return JsonResponse(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #17
0
 def put(self, request, id, format=None):
     post = self.get_object(id)
     if post == False:
         return JsonResponse({'msg': 'Post was not found'}, status=404)
     request.data['created_at'] = post.created_at
     serializer = PostSerializer(post, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return JsonResponse(serializer.data, status=204)
     return JsonResponse(serializer.errors, status=400)
예제 #18
0
    def update(self, request, pk=None):
        post = PostModel.objects.get(id=pk)
        data = JSONParser().parse(request)
        serialized = PostSerializer(post, data=data)

        if serialized.is_valid():
            serialized.save()
            return Response(serialized.data)

        return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #19
0
 def put(self, request, id, format=None):
     try:
         item = Post.objects.get(pk=id)
     except Post.DoesNotExist:
         return Response(status=404)
     serializer = PostSerializer(item, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=400)
예제 #20
0
 def post(self, request, pk=None):
     if pk:
         return Response({'error': 'Unhandled Method !!'}, status=403)
     # there is no need for us to do parsing as APIView does it for us !! Thanks APIView.....
     data = request.data
     post = PostSerializer(data=data)
     if post.is_valid():
         post.save(user=request.user)
         return Response(post.data, status=201)
     return Response(post.errors, status=400)
예제 #21
0
 def update(self, request, pk=None):
     try:
         item = Post.objects.get(pk=pk)
     except Post.DoesNotExist:
         return Response(status=404)
     serializer = PostSerializer(item, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=400)
예제 #22
0
def addPost(request):
    
    if request.method == "POST":
        data = JSONParser().parse(request)
        serialized = PostSerializer(data=data)

        if serialized.is_valid():
            serialized.save()
            return JsonResponse(serialized.data, status=status.HTTP_201_CREATED)
        return JsonResponse(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #23
0
def post(request):
    if request.method == 'GET':
        posts = Post.objects.all()
        serializer = PostSerializer(posts, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
    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_500_INTERNAL_SERVER_ERROR)
예제 #24
0
파일: views.py 프로젝트: pandye/ecommerce
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)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #25
0
def posts_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 = json.loads(request.body)
        serializer = PostModelSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse({'error': 'invalid data'})
예제 #26
0
 def put(self, request, pk=None):
     if not pk:
         return Response({'error': 'Unhandled Method !!'}, status=403)
     try:
         post = Post.objects.get(pk=pk)
     except ObjectDoesNotExist:
         return JsonResponse({'error': 'Post not found with {}'.format(pk)},
                             status=404)
     data = request.data
     post = PostSerializer(post, data=data)
     if post.is_valid():
         post.save(user=request.user)
         return Response(post.data)
예제 #27
0
def posts(request):
    if request.method == 'GET':
        queryset = Post.objects.all()
        serializer = PostSerializer(queryset, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        parser = JSONParser()
        data = parser.parse(request)
        post = PostSerializer(data=data)
        if post.is_valid():
            post.save(user=request.user)
            return JsonResponse(post.data, status=201)
        return JsonResponse(post.errors, status=400)
    def post(self, request, post_pk=None, format=None):
        # ensure user is authenticated
        if (not request.user.is_authenticated()):
            return Response({'message':'Not authenticated'}, status=status.HTTP_401_UNAUTHORIZED)

        data = request.data

        '''
        Gets the author from the request
        '''
        try:
            author = Author.objects.get(user=request.user)
        except Author.DoesNotExist as e:
            return Response({"message":"Author does not exist / is not a local author"}, status=status.HTTP_400_BAD_REQUEST)

        # If its a remote node - not allowed to make posts
        if request.get_host() not in author.host:
            return Response({"message":"Only local authors can make posts"}, status=status.HTTP_403_FORBIDDEN)


        statusCode = status.HTTP_201_CREATED
        serializer = PostSerializer(data=data)

        '''
        Handles : EDIT Posts via POST method
        '''
        if post_pk != None:
            try:
                post = Post.objects.get(id=post_pk)
            except Post.DoesNotExist as e:
                return Response({"message": "Post does not exist"}, status=status.HTTP_404_NOT_FOUND)


            # only allow author of the post to modify it
            if author != post.author:
                return Response({"message":"Only the author of this post can make changes to it"}, status=status.HTTP_403_FORBIDDEN)

            statusCode = status.HTTP_200_OK
            serializer = PostSerializer(post, data=data)



        if serializer.is_valid():
            print "DEBUG : API - views.py - PostList"
            serializer.validated_data["author"] = author
            serializer.validated_data["published"] = timezone.now()
            serializer.save()
            return Response(serializer.data, status=statusCode)

        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #29
0
def published_list(request):
    if request.method == 'GET':
        # def get_queryset(self):
        posts = Post.objects.filter(is_published=True)
        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({'error': serializer.errors},
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #30
0
def api_update_blog_view(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'PUT':
        serializer = PostSerializer(post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['success'] = 'update successful'
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #31
0
 def post(self, request):
     # POST: Create a post
     curAuthor = Author.objects.get(id=request.user.id)
     origin = request.scheme + "://" + request.get_host() + "/"
     serializer = PostSerializer(data=request.data,
                                 context={
                                     'author': curAuthor,
                                     'origin': origin
                                 })
     if serializer.is_valid():
         serializer.save()
         #Todo: response success message on json format
         return Response()
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #32
0
 def patch(self, request, pk):
     post_obj = self.get_post(pk)
     serialized = PostSerializer(
         post_obj,
         data=request.data,
         partial=True,
     )
     if serialized.is_valid(raise_exception=True):
         serialized.save()
         response = {
             "data": serialized.data,
             "message": "All good, updated",
         }
         return Response(response, status=200)
     return Response({"message": "Error - not updated"}, status=400)
예제 #33
0
파일: views.py 프로젝트: pandye/ecommerce
def post_single(request, id, format=None):
    post = get_object_or_404(Post, id=id)
    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, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #34
0
def wish_detail(request, pk=None):
    if 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)
    else:
        try:
            post = Post.objects.get(pk=pk)
        except Post.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.method == 'DELETE':
            post.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
예제 #35
0
def post_detail(request, pk):
    try:
        post = Post.objects.get(id=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(instance=post, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
    elif request.method == 'DELETE':
        post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #36
0
def post_details(request, post_id):
    try:
        post = Post.objects.get(id=post_id)
    except Post.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        serializer = PostSerializer(post)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = PostSerializer(instance=post, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})

    elif request.method == 'DELETE':
        post.delete()
        return Response({'deleted': True})
예제 #37
0
def archive_detail(request, id, format=None):
    try:
        snippet = Post.objects.get(id=id)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = PostSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = PostSerializer(snippet, 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':
        # if not request.user.has_perm('OpsManage.can_delete_wiki_post'):
        # return Response(status=status.HTTP_403_FORBIDDEN)
        snippet.delete()
예제 #38
0
파일: wiki_api.py 프로젝트: amoyx/OpsManage
def archive_detail(request, id,format=None):
    try:
        snippet = Post.objects.get(id=id)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
 
    if request.method == 'GET':
        serializer = PostSerializer(snippet)
        return Response(serializer.data)
 
    elif request.method == 'PUT':
        serializer = PostSerializer(snippet, 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':
        if not request.user.has_perm('OpsManage.can_delete_wiki_post'):
            return Response(status=status.HTTP_403_FORBIDDEN)
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)      
예제 #39
0
def post_detail(request, pk):
    try:
        # we cant use get_object_or_404 as ite returns 404 page as this is api we need to send a response
        post = Post.objects.get(pk=pk)
    except ObjectDoesNotExist:
        return JsonResponse({'error': 'Post not found with {}'.format(pk)},
                            status=404)
    if request.method == 'GET':
        post = PostSerializer(post)
        return JsonResponse(post.data)
    elif request.method == 'PUT':
        parser = JSONParser()
        data = parser.parse(request)
        post = PostSerializer(post, data=data)
        if post.is_valid():
            post.save(user=request.user)
            return JsonResponse(post.data)
        return JsonResponse(post.errors, status=400)
    elif request.method == 'DELETE':
        post.delete()
        return JsonResponse({'message': 'Successfully deleted the post !'},
                            status=204)
예제 #40
0
 def create(self, request):
     serializer = PostSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=201)
     return Response(serializer.errors, status=400)