Skip to content

Commit

Permalink
Enhance URL encoding and validation in SchemingDCATRDFProfile to supp…
Browse files Browse the repository at this point in the history
…ort fallback URLs
  • Loading branch information
mjanez committed Dec 24, 2024
1 parent 345a06b commit 11e102a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions ckanext/schemingdcat/profiles/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import logging
import json
from urllib.parse import quote

from rdflib import term, URIRef, Literal

Expand Down Expand Up @@ -536,12 +537,15 @@ def _add_valid_url_to_graph(self, graph, subject, predicate, url, fallback_url):
url (str): The URL to validate and add.
fallback_url (str): The fallback URL to use if the URL is not valid.
"""
valid_url = URIRef(url) if is_url(url) else None
if valid_url:
graph.add((subject, predicate, valid_url))
elif fallback_url:
graph.add((subject, predicate, URIRef(fallback_url)))

if isinstance(url, str):
encoded_url = quote(url, safe=':/?&=')
valid_url = URIRef(encoded_url) if is_url(encoded_url) else None
if valid_url:
graph.add((subject, predicate, valid_url))
elif fallback_url and isinstance(fallback_url, str):
encoded_fallback_url = quote(fallback_url, safe=':/?&=')
graph.add((subject, predicate, URIRef(encoded_fallback_url)))

def _is_direct_download_url(self, url):
"""
Check if the URL is a direct download link.
Expand Down
3 changes: 2 additions & 1 deletion ckanext/schemingdcat/profiles/eu_dcat_ap_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ def _graph_from_dataset_base(self, dataset_dict, dataset_ref):

# Use access_url/download_url if it exists and is a valid URL, otherwise use url
self._add_valid_url_to_graph(g, distribution, DCAT.accessURL, access_url, url)
self._add_valid_url_to_graph(g, distribution, DCAT.downloadURL, download_url, url)
if download_url:
self._add_valid_url_to_graph(g, distribution, DCAT.downloadURL, download_url, url)

# Dates
items = [
Expand Down

0 comments on commit 11e102a

Please sign in to comment.