Skip to content

Commit

Permalink
Merge pull request #314 from IFB-ElixirFr/bugfix-orga-info-in-subnode
Browse files Browse the repository at this point in the history
Bugfix orga info in subnode
  • Loading branch information
bryan-brancotte authored Aug 5, 2024
2 parents da085cf + 361d33e commit be1f78b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
35 changes: 24 additions & 11 deletions ifbcat_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
ManyToManyRel,
ForeignKey,
)
from django.db.models.fields.related_descriptors import ReverseManyToOneDescriptor
from django.db.models.fields.related_descriptors import (
ReverseManyToOneDescriptor,
ManyToManyDescriptor,
)
from django.urls import reverse
from rdflib import ConjunctiveGraph, URIRef, Namespace, Literal, BNode
from rdflib.namespace import RDF, XSD
Expand Down Expand Up @@ -110,18 +113,28 @@ def get_rdf_mapping(instance_id=None):
if attr_name[0] == '_':
continue
if type(mapping) == dict and (sub_fields := mapping.get('_fields', None)) is not None:
sub_node = BNode()
G.add((sub_node, RDF.type, getattr(SCHEMA, mapping["_type"])))
for attr_name, schema_attr in sub_fields.items():
G.add(
(
sub_node,
getattr(SCHEMA, schema_attr),
Literal(getattr(model.objects.get(id=item['id']), attr_name)),
# by default, we use the object itself as source for the sub node
sub_nodes_src = model.objects.filter(id=item['id'])
try:
if type(getattr(model, attr_name)) in (ManyToManyDescriptor,):
# if the attr_name exists and is an M2M, we use them as sources of the sub nodes
sub_nodes_src = getattr(model.objects.get(id=item['id']), attr_name).all()
except AttributeError:
pass
for sub_node_src in sub_nodes_src:
# for each source, create a new sub node, and get info from this source
sub_node = BNode()
G.add((sub_node, RDF.type, getattr(SCHEMA, mapping["_type"])))
for sub_attr_name, schema_attr in sub_fields.items():
G.add(
(
sub_node,
getattr(SCHEMA, schema_attr),
Literal(getattr(sub_node_src, sub_attr_name)),
)
)
)

G.add((object_uri, getattr(SCHEMA, mapping['schema_attr']), sub_node))
G.add((object_uri, getattr(SCHEMA, mapping['schema_attr']), sub_node))
continue
try:
# get the value from the serialized object
Expand Down
9 changes: 8 additions & 1 deletion ifbcat_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,14 @@ class Meta:
costs=dict(schema_attr='offers', _type='Demand'),
homepage='url',
maxParticipants='maximumAttendeeCapacity',
organisedByOrganisations='organizer',
organisedByOrganisations=dict(
schema_attr='organizer',
_type="Organization",
_fields=dict(
name='name',
homepage='url',
),
),
organisedByTeams=dict(
schema_attr='organizer',
_type="Organization",
Expand Down

0 comments on commit be1f78b

Please sign in to comment.