Ejemplo n.º 1
0
    def put(self, request, graph_slug, format=None):
        """
        Edit an existing graph. Omitted fields are removed.
        """
        graph = get_object_or_404(Graph, slug=graph_slug)
        self.check_object_permissions(self.request, graph)

        # We get the data from the request
        post_data = request.data
        post_data['name'] = request.data.get('name', None)

        post_data['owner'] = graph.owner.pk
        post_data['schema'] = graph.schema_id

        if post_data['name'] is None:
            post_data['name'] = graph.name

        # We get the serializer of the graph to get the fields
        serializer = GraphSerializer(graph)
        fields = serializer.fields.keys()

        # We need to omit the fields that are not included
        # We need to take into account the fields restrictions
        new_data = dict()
        for field in fields:
            if field == 'public':
                new_data[field] = graph.public
            else:
                new_data[field] = post_data.get(field, None)

        # And now we update the instance
        serializer = GraphSerializer(graph, data=new_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)
Ejemplo n.º 2
0
    def put(self, request, graph_slug, format=None):
        """
        Edit an existing graph. Omitted fields are removed.
        """
        graph = get_object_or_404(Graph, slug=graph_slug)
        self.check_object_permissions(self.request, graph)

        # We get the data from the request
        post_data = request.data
        post_data["name"] = request.data.get("name", None)

        post_data["owner"] = graph.owner.pk
        post_data["schema"] = graph.schema_id

        if post_data["name"] is None:
            post_data["name"] = graph.name

        # We get the serializer of the graph to get the fields
        serializer = GraphSerializer(graph)
        fields = serializer.fields.keys()

        # We need to omit the fields that are not included
        # We need to take into account the fields restrictions
        new_data = dict()
        for field in fields:
            if field == "public":
                new_data[field] = graph.public
            else:
                new_data[field] = post_data.get(field, None)

        # And now we update the instance
        serializer = GraphSerializer(graph, data=new_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)
Ejemplo n.º 3
0
    def patch(self, request, graph_slug, format=None):
        """
        Edit an existing graph. Only fields provided will be modified
        """
        graph = get_object_or_404(Graph, slug=graph_slug)
        self.check_object_permissions(self.request, graph)

        # We get the data from the request
        post_data = request.data
        post_data['name'] = request.data.get('name', None)

        post_data['owner'] = graph.owner.pk
        post_data['schema'] = graph.schema_id

        if post_data['name'] is None:
            post_data['name'] = graph.name

        serializer = GraphSerializer(graph, data=post_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)
Ejemplo n.º 4
0
    def patch(self, request, graph_slug, format=None):
        """
        Edit an existing graph. Only fields provided will be modified
        """
        graph = get_object_or_404(Graph, slug=graph_slug)
        self.check_object_permissions(self.request, graph)

        # We get the data from the request
        post_data = request.data
        post_data["name"] = request.data.get("name", None)

        post_data["owner"] = graph.owner.pk
        post_data["schema"] = graph.schema_id

        if post_data["name"] is None:
            post_data["name"] = graph.name

        serializer = GraphSerializer(graph, data=post_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)