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.copy() # We need to get all the fields to create the property relationshiptype = get_object_or_404(RelationshipType, slug=type_slug, schema__graph__slug=graph_slug) if not isinstance(post_data, (dict)): # We check if we need to deserialize the object post_data = post_data.dict() try: post_data['relationship'] = relationshiptype rel_property = RelationshipProperty(**post_data) # We need to check if we have the datatype in the properties try: datatype = post_data['datatype'] try: real_datatype = rel_property.get_datatype_dict()[datatype] rel_property.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 rel_property.save() serializer = NodeTypeSchemaPropertiesSerializer(relationshiptype) 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