Skip to content

Commit

Permalink
feat(apis_entities): ship CIDOC serializers for E21_Person and E53_Place
Browse files Browse the repository at this point in the history
Those serializers are based on the GenericModelCidocSerializer and add
addional attributes like birth and death information and geocoordinates.
  • Loading branch information
b1rger committed Dec 19, 2024
1 parent e5f6409 commit db23878
Showing 1 changed file with 177 additions and 1 deletion.
178 changes: 177 additions & 1 deletion apis_core/apis_entities/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from rdflib import Literal, URIRef
from rdflib.namespace import GEO, RDF, RDFS, XSD
from rest_framework import serializers

from apis_core.generic.serializers import GenericHyperlinkedIdentityField
from apis_core.generic.serializers import (
GenericHyperlinkedIdentityField,
GenericModelCidocSerializer,
)


class MinimalEntitySerializer(serializers.Serializer):
Expand All @@ -11,3 +16,174 @@ class MinimalEntitySerializer(serializers.Serializer):

def get_name(self, object) -> str:
return str(object)


class E53_PlaceCidocSerializer(GenericModelCidocSerializer):
def to_representation(self, instance):
g = super().to_representation(instance)

g.add((self.instance_uri, RDF.type, self.namespaces["crm"].E53_Place))
if instance.latitude is not None and instance.longitude is not None:
node_spaceprimitive = self.namespaces[self.attr_nsp_prefix][
f"spaceprimitive.{instance.id}"
]
g.add(
(
self.instance_uri,
self.namespaces["crm"].P168_place_is_defined_by,
node_spaceprimitive,
)
)
g.add(
(
node_spaceprimitive,
RDF.type,
self.namespaces["crm"].E94_Space_Primitive,
)
)
g.add(
(
node_spaceprimitive,
self.namespaces["crm"].P168_place_is_defined_by,
Literal(
(
f"Point ( {'+' if instance.longitude > 0 else ''}{instance.longitude} {'+' if instance.latitude > 0 else ''}{instance.latitude} )"
),
datatype=GEO.wktLiteral,
),
)
)
return g


class E21_PersonCidocSerializer(GenericModelCidocSerializer):
def to_representation(self, instance):
g = super().to_representation(instance)

g.add((self.instance_uri, RDF.type, self.namespaces["crm"].E21_Person))

if hasattr(instance, "forename"):
forename_uri = URIRef(
self.namespaces[self.appelation_nsp_prefix][f"forename_{instance.id}"]
)
g.add(
(
forename_uri,
RDF.type,
self.namespaces["crm"].E33_E41_Linguistic_Appellation,
)
)
g.add(
(
self.appellation_uri,
self.namespaces["crm"].P106_is_composed_of,
forename_uri,
)
)
g.add((forename_uri, RDFS.label, Literal(instance.forename)))

if hasattr(instance, "surname"):
surname_uri = URIRef(
self.namespaces[self.appelation_nsp_prefix][f"surname_{instance.id}"]
)
g.add(
(
surname_uri,
RDF.type,
self.namespaces["crm"].E33_E41_Linguistic_Appellation,
)
)
g.add(
(
self.appellation_uri,
self.namespaces["crm"].P106_is_composed_of,
surname_uri,
)
)
g.add((surname_uri, RDFS.label, Literal(instance.surname)))

if instance.start_date_written:
birth_event = URIRef(
self.namespaces[self.attr_nsp_prefix][f"birth_{instance.id}"]
)
birth_time_span = URIRef(
self.namespaces[self.attr_nsp_prefix][f"birth_time-span_{instance.id}"]
)
g.add((birth_event, RDF.type, self.namespaces["crm"].E67_Birth))
g.add(
(
birth_event,
self.namespaces["crm"].P98_brought_into_life,
self.instance_uri,
)
)
g.add(
(
birth_event,
self.namespaces["crm"]["P4_has_time-span"],
birth_time_span,
)
)
g.add((birth_time_span, RDF.type, self.namespaces["crm"]["E52_Time-Span"]))
g.add(
(
birth_time_span,
self.namespaces["crm"].P82a_begin_of_the_begin,
Literal(instance.start_date, datatype=XSD.date)
if instance.start_date is not None
else Literal(instance.start_date_written),
)
)
g.add(
(
birth_time_span,
self.namespaces["crm"].P82b_end_of_the_end,
Literal(instance.start_date, datatype=XSD.date)
if instance.start_date is not None
else Literal(instance.start_date_written),
)
)

if instance.end_date_written:
death_event = URIRef(
self.namespaces[self.attr_nsp_prefix][f"death_{instance.id}"]
)
death_time_span = URIRef(
self.namespaces[self.attr_nsp_prefix][f"death_time-span_{instance.id}"]
)
g.add((death_event, RDF.type, self.namespaces["crm"].E69_Death))
g.add(
(
death_event,
self.namespaces["crm"].P100_was_death_of,
self.instance_uri,
)
)
g.add(
(
death_event,
self.namespaces["crm"]["P4_has_time-span"],
death_time_span,
)
)
g.add((death_time_span, RDF.type, self.namespaces["crm"]["E52_Time-Span"]))
g.add(
(
death_time_span,
self.namespaces["crm"].P82a_begin_of_the_begin,
Literal(instance.end_date, datatype=XSD.date)
if instance.end_date is not None
else Literal(instance.end_date_written),
)
)
g.add(
(
death_time_span,
self.namespaces["crm"].P82b_end_of_the_end,
Literal(instance.end_date, datatype=XSD.date)
if instance.end_date is not None
else Literal(instance.end_date_written),
)
)

return g

0 comments on commit db23878

Please sign in to comment.