def identify_from_json_data(self,
                                input_data,
                                schema,
                                owner,
                                parrent_label=None):
        # setup how the loop handles each type of object occurrence

        builder = BuildDataObjectsFromJson(schema, owner)

        person = ObjectInstance(base=self.get_foaf_person())
        person.save()
        # TODO: Relate to logged in foaf person object instance instead

        if parrent_label is not None and isinstance(input_data, list):
            # if its a list, we have to create a base object for each element
            input_data = [{parrent_label: elm} for elm in input_data]
        elif parrent_label is not None and isinstance(input_data, dict):
            # if its a dict, just add the parrent label
            input_data = {
                parrent_label: input_data,
            }

        builder.build_from_json(input_data, parrent_object=person)

        return builder.added_instance_items
Exemple #2
0
    def mutate(self, info, meta_schema, meta_object, meta_attribute,
               value):

        raise NotImplementedError()
        # get the object type from metadata and create an instance
        try:
            schema = Schema.objects.get(label=meta_schema)
            object = Object.objects.get(
                label=meta_object,
                schema=schema)
            attribute = Attribute.objects.get(
                label=meta_attribute,
                object=object)
        except Exception as e:
            raise GraphQLError(
                """no metadata object correspond to the
                combination of source, category and label""")

        object_inst = ObjectInstance(
            base=object,
            schema=schema,
            owner=info.context.user
        )

        object_inst.save()

        return CreateDatapoint(datapoint)
    def _validate_and_create_if_real_parrent(self, attribute_inst,
                                             assumed_parrent):
        # if the parrent object is not the "real" parrent
        # of the attribute, then create it
        if assumed_parrent is None or \
                attribute_inst.base.object != assumed_parrent.base:

            real_parrent = ObjectInstance(base=attribute_inst.base.object, )
            real_parrent.save()
        else:
            real_parrent = assumed_parrent

        attribute_inst.object = real_parrent

        return real_parrent
    def add_schema_and_data(self,
                            input_data,
                            schema,
                            parrent_label=None,
                            owner=None):

        # setup how the loop handles each type of object occurrence
        self.owner = owner
        self._att_function = self.try_create_attribute_and_instance
        self._object_function = self.try_create_object__and_instance
        self._obj_rel_function = self.try_create_object_relation_and_instance

        self.schema = schema

        # test if this is an url
        url_data = self.validate_url(input_data)

        # try to get the last dir of the url if applicable
        parrent_label = parrent_label or (url_data
                                          and "/".split(input_data)[-1])

        input_data = url_data or input_data
        # json data can either be list or dict
        if not isinstance(input_data, (dict, list)):
            input_data = json.loads(input_data)

        # create a base person to relate the data to
        try:
            person = ObjectInstance(base=self.get_foaf_person())
            person.save()
        except Exception as e:
            raise Exception("foaf person was not found")
        # TODO: Relate to logged in foaf person object instance instead

        if parrent_label is not None and isinstance(input_data, list):
            # if its a list, we have to create a base object for each element
            input_data = [{parrent_label: elm} for elm in input_data]
        if parrent_label is not None and isinstance(input_data, dict):
            # if its a dict, just add the parrent label
            input_data = {
                parrent_label: input_data,
            }

        self.iterate_data_generic(input_data, person)

        return self.added_instance_items
    def test_(self):
        from MetaDataApi.metadata.services import (RdfSchemaService,
                                                   RdfInstanceService)

        from MetaDataApi.metadata.models import (Object, Schema, Attribute,
                                                 ObjectRelation)

        from MetaDataApi.metadata.models import (RawData, CategoryTypes,
                                                 ObjectInstance,
                                                 ObjectRelationInstance,
                                                 FloatAttributeInstance,
                                                 StringAttributeInstance)

        LoadTestData.init_foaf()

        service = RdfInstanceService()

        schema_label = "friend_of_a_friend"

        schema = service._try_get_item(Schema(label=schema_label))

        foaf_atts = Attribute.objects.filter(object__schema=schema)
        s = list(filter(lambda x: x.label, foaf_atts))

        foaf_person = service.get_foaf_person()
        foaf_name = Attribute.objects.get(label="first_name",
                                          object__schema=schema)

        foaf_knows = ObjectRelation.objects.get(label="knows", schema=schema)

        b1 = ObjectInstance(base=foaf_person)
        b2 = ObjectInstance(base=foaf_person)
        b1.save()
        b2.save()

        name1 = StringAttributeInstance(base=foaf_name, object=b1, value="B1")
        name2 = StringAttributeInstance(base=foaf_name, object=b2, value="B2")
        name1.save()
        name2.save()

        rel1 = ObjectRelationInstance(base=foaf_knows,
                                      from_object=b1,
                                      to_object=b2)

        rel1.save()

        objects = [b1, b2, name1, name2, rel1]

        rdf_file = service.export_instances_to_rdf_file(schema, objects)

        self.assertIsNotNone(rdf_file.url)
    def test_get_related_list(self):
        from MetaDataApi.metadata.models import ObjectInstance
        schema = LoadTestData.init_foaf()
        obj = Object.objects.get(label="person", schema=schema)

        obj_inst = ObjectInstance(base=obj)
        res_inst = obj_inst.get_related_list()

        res = obj.get_related_list()

        res_string = list(map(lambda x: x.label, res))

        expected = [
            'person', 'person', 'document', 'document', 'document', 'image',
            'document', 'document', 'surname', 'first_name', 'family_name',
            'geekcode', 'myers_briggs', 'plan'
        ]

        res_string.sort()
        expected.sort()

        self.assertEqual(res_string, expected)