def get(self, request, graph_slug, format=None): """ Returns the information of a graph """ graph = get_object_or_404(Graph, slug=graph_slug) self.check_object_permissions(self.request, graph) serializer = GraphSerializer(graph) return Response(serializer.data)
def delete(self, request, graph_slug, format=None): """ Delete an existing graph. """ graph = get_object_or_404(Graph, slug=graph_slug) self.check_object_permissions(self.request, graph) serializer = GraphSerializer(graph) serializer.instance.destroy() return Response(status=status.HTTP_204_NO_CONTENT)
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)
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)
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)
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)