Skip to content

Commit

Permalink
Merge pull request #101 from opendata-swiss/fix/handle-contact-point-…
Browse files Browse the repository at this point in the history
…without-email

Fix/handle contact point without email
  • Loading branch information
bellisk authored Apr 3, 2024
2 parents 8cfbb02 + 4cd4861 commit cd90fec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
19 changes: 16 additions & 3 deletions ckanext/dcatapchharvest/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,25 @@ def graph_from_dataset(self, dataset_dict, dataset_ref): # noqa

# Contact details
if dataset_dict.get('contact_points'):
contact_points = self._get_dataset_value(dataset_dict, 'contact_points') # noqa
contact_points = self._get_dataset_value(
dataset_dict, 'contact_points'
)
for contact_point in contact_points:
if not contact_point.get('email') \
or not contact_point.get('name'):
continue

contact_details = BNode()
contact_point_email = \
EMAIL_MAILTO_PREFIX + contact_point['email']
contact_point_name = contact_point['name']

g.add((contact_details, RDF.type, VCARD.Organization))
g.add((contact_details, VCARD.hasEmail, URIRef(contact_point_email))) # noqa
g.add((
contact_details,
VCARD.hasEmail,
URIRef(contact_point_email)
))
g.add((contact_details, VCARD.fn, Literal(contact_point_name)))

g.add((dataset_ref, DCAT.contactPoint, contact_details))
Expand Down Expand Up @@ -1247,8 +1257,11 @@ def contact_details(self, dataset_dict, dataset_ref, g):
if dataset_dict.get("contact_points"):
contact_points = self._get_dataset_value(
dataset_dict, "contact_points"
) # noqa
)
for contact_point in contact_points:
if not contact_point.get('email') \
or not contact_point.get('name'):
continue
contact_details = BNode()
contact_point_email = \
EMAIL_MAILTO_PREFIX + contact_point["email"]
Expand Down
12 changes: 12 additions & 0 deletions ckanext/dcatapchharvest/tests/fixtures/dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@
"https://example.com/documentation-dataset-1",
"https://example.com/documentation-dataset-2"
],
"contact_points": [
{
"name": "Maria Muster",
"email": "[email protected]"
},
{
"name": "Maria Muster"
},
{
"email": "[email protected]"
}
],
"resources": [
{
"id": "e2c50e70-67ad-4f86-bb1b-3f93867eadaa",
Expand Down
13 changes: 12 additions & 1 deletion ckanext/dcatapchharvest/tests/test_dcatap_ch_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ckanext.dcat import utils
from ckanext.dcat.processors import RDFSerializer
from ckanext.dcat.profiles import DCAT, DCT, FOAF, OWL, SCHEMA, XSD
from ckanext.dcat.profiles import DCAT, DCT, FOAF, OWL, SCHEMA, VCARD, XSD

import ckanext.dcatapchharvest.dcat_helpers as dh

Expand Down Expand Up @@ -63,6 +63,17 @@ def test_graph_from_dataset(self):
for documentation_link in dataset['documentation']:
assert self._triple(g, dataset_ref, FOAF.page, URIRef(documentation_link))

# Contact points
eq_(len([t for t in g.triples((dataset_ref, DCAT.contactPoint, None))]), 1)

contact_point = next(g.objects(dataset_ref, DCAT.contactPoint))
eq_(next(g.objects(contact_point, RDF.type)), VCARD.Organization)
eq_(
next(g.objects(contact_point, VCARD.hasEmail)),
URIRef("mailto:[email protected]")
)
eq_(next(g.objects(contact_point, VCARD.fn)), Literal("Maria Muster"))

# Conformance
conforms_to = dataset.get("conforms_to", [])
# Check if the number of triples matches the number of conformance uris
Expand Down
13 changes: 12 additions & 1 deletion ckanext/dcatapchharvest/tests/test_swiss_schemaorg_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ckanext.dcat import utils
from ckanext.dcat.processors import RDFSerializer
from ckanext.dcat.profiles import SCHEMA
from ckanext.dcat.profiles import SCHEMA, VCARD

from rdflib import URIRef
import ckanext.dcatapchharvest.dcat_helpers as dh
Expand Down Expand Up @@ -40,6 +40,17 @@ def test_graph_from_dataset(self):
assert self._triple(g, dataset_ref, SCHEMA.version, dataset['version'])
assert self._triple(g, dataset_ref, SCHEMA.identifier, extras['identifier'])

# Contact points
eq_(len([t for t in g.triples((dataset_ref, SCHEMA.contactPoint, None))]), 1)

contact_point = next(g.objects(dataset_ref, SCHEMA.contactPoint))
eq_(next(g.objects(contact_point, RDF.type)), VCARD.Organization)
eq_(
next(g.objects(contact_point, VCARD.hasEmail)),
URIRef("mailto:[email protected]")
)
eq_(next(g.objects(contact_point, VCARD.fn)), Literal("Maria Muster"))

# Dates
assert self._triple(g, dataset_ref, SCHEMA.datePublished, dataset['issued'])
assert len(list(g.objects(dataset_ref, SCHEMA.dateModified))) == 0
Expand Down

0 comments on commit cd90fec

Please sign in to comment.