def post(self, request, graph_slug, type_slug, format=None): """ Create a new property """ graph = get_object_or_404(Graph, slug=graph_slug) self.check_object_permissions(self.request, graph) post_data = request.data if isinstance(post_data, (str, unicode)): # We check if we need to deserialize the object post_data = json.loads(post_data) # We need to get all the fields to create the property post_data = post_data.copy() nodetype = get_object_or_404(NodeType, slug=type_slug, schema__graph__slug=graph_slug) try: post_data['node'] = nodetype nodeproperty = NodeProperty(**post_data) # We need to check if we have the datatype in the properties try: datatype = post_data['datatype'] try: real_datatype = nodeproperty.get_datatype_dict()[datatype] nodeproperty.datatype = real_datatype except Exception as e: # We dont save the node property # We create our json response error = {} error['detail'] = e.message return Response(error, status=status.HTTP_400_BAD_REQUEST) except KeyError: # We save the node property because we dont have the datatype # field pass nodeproperty.save() serializer = NodeTypeSchemaPropertiesSerializer(nodetype) return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: # We create our json response error = {} error['detail'] = e.message return Response(error, status=status.HTTP_400_BAD_REQUEST)
def _clone_schema(self, schema, new_schema): nodetypes_map = {} # map old/new nodetypes IDs relationtypes_map = {} # map old/new relationtypes IDs nodetypes = schema.nodetype_set.all() for nt in nodetypes: new_nt = NodeType(schema=new_schema, name=nt.name, plural_name=nt.plural_name, description=nt.description, order=nt.order, total=0, validation=nt.validation) new_nt.save() nodetypes_map[nt.id] = new_nt.id properties = nt.properties.all() for np in properties: new_np = NodeProperty(node=new_nt, key=np.key, value=np.value, default=np.default, datatype=np.datatype, required=np.required, display=np.display, description=np.description, validation=np.validation, order=np.order) new_np.save() relationtypes = schema.relationshiptype_set.all() for rt in relationtypes: source = NodeType.objects.get(schema=new_schema, name=rt.source.name) target = NodeType.objects.get(schema=new_schema, name=rt.target.name) new_rt = RelationshipType(schema=new_schema, source=source, target=target, name=rt.name, plural_name=rt.plural_name, description=rt.description, order=rt.order, total=0, validation=rt.validation, inverse=rt.inverse, plural_inverse=rt.plural_inverse, arity_source=rt.arity_source, arity_target=rt.arity_target) new_rt.save() relationtypes_map[rt.id] = new_rt.id properties = rt.properties.all() for rp in properties: new_rp = RelationshipProperty(relationship=new_rt, key=rp.key, value=rp.value, default=rp.default, datatype=rp.datatype, required=rp.required, display=rp.display, description=rp.description, validation=rp.validation, order=rp.order) new_rp.save() return nodetypes_map, relationtypes_map