Ejemplo n.º 1
0
 def test_existence_query(self, kg_client):
     obj = ModelProject(name="foo",
                        owners=Person("Holmes", "Sherlock"),
                        authors=Person("Holmes", "Sherlock"),
                        description="",
                        private=True,
                        date_created=datetime(2000, 1, 1))
     expected = {
         "op":
         "and",
         "value": [{
             "path": "schema:name",
             "op": "eq",
             "value": "foo"
         }, {
             "path": "schema:dateCreated",
             "op": "eq",
             "value": "2000-01-01T00:00:00"
         }]
     }
     generated = obj._build_existence_query(api="nexus")
     assert expected == generated
    def migrate_models(self):

        models = ScientificModel.objects.all()

        for model in models:
            authors = self._get_people_from_Persons_table(model.author)
            for author in authors:
                author.save(NAR_client)
            owners = self._get_people_from_Persons_table(model.owner)
            for owner in owners:
                owner.save(NAR_client)
            if len(owners) == 0:
                owners = authors[-1:]
            if len(owners) > 1:
                owners = owners[
                    0]  # temporary, need to fix schema to remove maxCount: 1
            organization = self.get_organization(model.organization)
            brain_region = self.get_parameters("brain_region",
                                               model.brain_region)
            species = self.get_parameters("species", model.species)
            cell_type = self.get_parameters("cell_type", model.cell_type)
            abstraction_level = self.get_parameters("abstraction_level",
                                                    model.abstraction_level)
            model_scope = self.get_parameters("model_scope", model.model_scope)

            model_project = ModelProject(
                name=model.name,
                owners=owners,
                authors=authors,
                description=model.description,
                date_created=model.creation_date,
                private=model.private,
                collab_id=model.app.collab_id,
                alias=model.alias,
                organization=organization,
                pla_components=model.pla_components,
                brain_region=brain_region,
                species=species,
                celltype=cell_type,
                abstraction_level=abstraction_level,
                model_of=model_scope,  # to fix
                images=[{
                    "url": im.url,
                    "caption": im.caption
                } for im in model.images.all()],
                old_uuid=str(model.id))

            #print ("--authors",model_project.authors)
            #print ("--organization",model_project.organization)
            #print ("--brain_region",model_project.brain_region)
            #print ("--species",model_project.species)

            try:
                model_project.save(NAR_client)
            except Exception as err:
                if "internal server error" in err.response.text:
                    logger.error(err)
                else:
                    raise
            else:
                logger.info("ModelProject saved: %s", model_project)
                print(model_project)

        # models_with_parents = ScientificModel.objects.filter(parents__isnull=False)
        # for model in models_with_parents:
        #     mp = ModelProject.by_name(model.name, NAR_client)
        #     for parent_obj in model.parents.all():
        #         parent_kg = ModelProject.by_name(parent_obj.name, NAR_client)
        #         mp.parents = as_list(mp.parents) + [parent_kg]
        #     mp.save(NAR_client)
        return ''
    def save(self, allow_update=True):
        if self.obj is None:  # create
            for key in ("author", "owner"):
                if isinstance(self.data[key], dict):
                    self.data[key] = [self.data[key]]
            self.obj = ModelProject(
                self.data["name"],
                [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["owner"])
                ],
                [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["author"])
                ],  # need to update person representation in clients,
                self.data.get("description"),
                datetime.now(),
                self.data.get("private", True),
                self.context["collab_id"],
                self.data.get("alias"),
                Organization(self.data["organization"]) if self.data.get(
                    "organization", False) else None,
                pla_components=None,
                brain_region=self._get_ontology_obj(BrainRegion,
                                                    "brain_region"),
                species=self._get_ontology_obj(Species, "species"),
                celltype=self._get_ontology_obj(CellType, "cell_type"),
                abstraction_level=self._get_ontology_obj(
                    AbstractionLevel, "abstraction_level"),
                model_of=self._get_ontology_obj(ModelScope, "model_scope"),
                old_uuid=self.data.get("old_uuid"),
                images=self.data.get("images"))
        else:  # update
            if "name" in self.data:
                self.obj.name = self.data["name"]
            if "alias" in self.data:
                self.obj.alias = self.data["alias"]
            if "author" in self.data:
                self.obj.authors = [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["author"])
                ]  # need to update person representation in clients
            if "owner" in self.data:
                self.obj.owners = [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["owner"])
                ]  # need to update person representation in clients
            if "app" in self.data:
                self.obj.collab_id = self.data["app"]["collab_id"]
            if "organization" in self.data and self.data[
                    "organization"] is not None:
                self.obj.organization = Organization(self.data["organization"])
            if "private" in self.data:
                self.obj.private = self.data["private"]
            if "cell_type" in self.data:
                self.obj.celltype = self._get_ontology_obj(
                    CellType, "cell_type")
            if "model_scope" in self.data:
                self.obj.model_of = self._get_ontology_obj(
                    ModelScope, "model_scope")
            if "abstraction_level" in self.data:
                self.obj.abstraction_level = self._get_ontology_obj(
                    AbstractionLevel, "abstraction_level")
            if "brain_region" in self.data:
                self.obj.brain_region = self._get_ontology_obj(
                    BrainRegion, "brain_region")
            if "species" in self.data:
                self.obj.species = self._get_ontology_obj(Species, "species")
            if "description" in self.data:
                self.obj.description = self.data["description"]
            if "old_uuid" in self.data:
                self.obj.old_uuid = self.data["old_uuid"]
            if "images" in self.data:
                self.obj.images = self.data["images"]

        # now save people, organization, model. No easy way to make this atomic, I don't think.
        for person in chain(as_list(self.obj.authors),
                            as_list(self.obj.owners)):
            if not isinstance(person, KGProxy):
                # no need to save if we have a proxy object, as
                # that means the person hasn't been updated
                person.save(self.client)
        if self.obj.organization and not isinstance(self.obj.organization,
                                                    KGProxy):
            self.obj.organization.save(self.client)
        self.obj.save(self.client)
        return self.obj