-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #376 Also refactors importer to allow for related entities in a generic way; adapts events config to that logic
- Loading branch information
Showing
3 changed files
with
43 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
from django.apps import apps | ||
from apis_core.generic.importers import GenericModelImporter | ||
from apis_core.utils.helpers import create_object_from_uri | ||
from apis_ontology.models import FandStattIn, Place | ||
|
||
|
||
class PersonImporter(GenericModelImporter): | ||
def mangle_data(self, data): | ||
if "profession" in data: | ||
del data["profession"] | ||
return data | ||
class BaseEntityImporter(GenericModelImporter): | ||
"""Importer for all OEBL entities. Allows to define related objects directly in the RDF variable names. | ||
Use `?something__RELATED_OBEJCT_CLASS__RELATION_CLASS` in your variables to auto create relations.""" | ||
|
||
|
||
class EventImporter(GenericModelImporter): | ||
def create_instance(self): | ||
data = self.get_data(drop_unknown_fields=False) | ||
modelfields = [field.name for field in self.model._meta.fields] | ||
data_croped = {key: data[key] for key in data if key in modelfields} | ||
subj = self.model.objects.create(**data_croped) | ||
if "related_place" in data: | ||
place = create_object_from_uri(data["related_place"], Place) | ||
FandStattIn.objects.create(subj=subj, obj=place) | ||
related_keys = [ | ||
(x, x.split("__")[1], x.split("__")[2]) for x in data.keys() if "__" in x | ||
] | ||
for rk in related_keys: | ||
key, obj, rel = rk | ||
RelatedModel = apps.get_model("apis_ontology", obj) | ||
RelationType = apps.get_model("apis_ontology", rel) | ||
if key in data: | ||
related_obj = create_object_from_uri(data[key], RelatedModel) | ||
RelationType.objects.create(subj=subj, obj=related_obj) | ||
|
||
return subj | ||
|
||
|
||
class EventImporter(BaseEntityImporter): | ||
pass | ||
|
||
|
||
class PersonImporter(BaseEntityImporter): | ||
def mangle_data(self, data): | ||
if "profession" in data: | ||
del data["profession"] | ||
return data | ||
|
||
|
||
class InstitutionImporter(BaseEntityImporter): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters