Skip to content
Snippets Groups Projects
Commit 1846f289 authored by Frank Lange's avatar Frank Lange
Browse files

generate the slug from the resource's/community's title instead of pulling it via SPARQL

parent 60d21334
Branches
No related tags found
No related merge requests found
Pipeline #1485211 passed
from typing import Dict, Iterable, List
from typing import Dict, Iterable
from django.utils.text import slugify
from rdflib import DCTERMS, Literal, RDF, URIRef, Variable
from project.dalia.api_models.api_models import Community, SocialMedia
......@@ -12,7 +13,7 @@ from project.dalia.query_builder.query_builder import (
QueryBuilder,
VALUES
)
from project.dalia.rdf.namespace import MoDalia, SCHEMA, content_inventory, wdt
from project.dalia.rdf.namespace import MoDalia, SCHEMA, wdt
def get_one_to_one_metadata_for_communities(community_uri_refs: Iterable[URIRef]) -> Dict[URIRef, Community]:
......@@ -35,7 +36,6 @@ def process_result_for_one_to_one_metadata_for_community(result) -> Community:
community = Community()
community.id = str(result.community).split("/")[-1]
community.slug = str(result.slug)
community.title = str(result.title)
community.image = None
community.url = str(result.url) if result.url else ""
......@@ -45,6 +45,9 @@ def process_result_for_one_to_one_metadata_for_community(result) -> Community:
community.followers = 0
community.social_media = process_social_media_in_result(result)
# TODO: could this be moved to the dataclass definition using the @property decorator?
community.slug = slugify(community.title)
return community
......@@ -85,7 +88,6 @@ _VARIABLES = {
"linkedin_id": Variable("linkedin_id"),
"zenodo_community_id": Variable("zenodo_community_id"),
"url": Variable("url"),
"slug": Variable("slug")
}
......@@ -119,5 +121,4 @@ def prepare_query_for_one_to_one_metadata_for_communities(community_uri_refs: It
OPTIONAL((var_community, wdt.LinkedIn_company_or_organization_ID, _VARIABLES["linkedin_id"])),
OPTIONAL((var_community, wdt.Zenodo_communities_ID, _VARIABLES["zenodo_community_id"])),
OPTIONAL((var_community, SCHEMA.url, _VARIABLES["url"])),
OPTIONAL((var_community, content_inventory.slug, _VARIABLES["slug"])),
).build()
from typing import Dict, List
from django.utils.text import slugify
from rdflib import DCTERMS, RDF, URIRef, Variable
from project.dalia.api_models.api_models import License, Resource
from project.dalia.query.items.metadata.license import get_license_info
from project.dalia.query.utils import query_dalia_dataset
from project.dalia.query_builder.query_builder import FILTER, FunctionExpressions, OPTIONAL, QueryBuilder, VALUES
from project.dalia.rdf.namespace import MoDalia, SCHEMA, content_inventory, educor, fabio
from project.dalia.rdf.namespace import MoDalia, SCHEMA, educor, fabio
_VARIABLES = {
"lr": Variable("lr"),
......@@ -17,7 +18,6 @@ _VARIABLES = {
"license": Variable("license"),
"fileSize": Variable("fileSize"),
"url": Variable("url"),
"slug": Variable("slug"),
"attachment": Variable("attachment"),
}
......@@ -43,7 +43,6 @@ def prepare_query_for_one_to_one_metadata_for_resources(resource_uri_refs: List[
OPTIONAL((var_lr, DCTERMS.license, _VARIABLES["license"])),
OPTIONAL((var_lr, SCHEMA.fileSize, _VARIABLES["fileSize"])),
OPTIONAL((var_lr, SCHEMA.url, _VARIABLES["url"])),
OPTIONAL((var_lr, content_inventory.slug, _VARIABLES["slug"])),
OPTIONAL(
(var_lr, MoDalia.hasMediaType, var_attachment),
FILTER(
......@@ -57,7 +56,6 @@ def process_result_for_one_to_one_metadata_for_resources(result) -> Resource:
resource = Resource()
resource.id = str(result.lr).split("/")[-1]
resource.slug = str(result.slug)
resource.title = str(result.title) if not result.subtitle else str(result.title) + ": " + str(result.subtitle)
resource.description = str(result.description) if result.description else ""
resource.url = str(result.url)
......@@ -66,6 +64,9 @@ def process_result_for_one_to_one_metadata_for_resources(result) -> Resource:
resource.file_size = str(result.fileSize) if result.fileSize else None
resource.type = str(result.attachment) if result.attachment else None
# TODO: could this be moved to the dataclass definition using the @property decorator?
resource.slug = slugify(resource.title)
return resource
......
"""
Terms from 'A Content Inventory Vocabulary' (https://vocab.methodandstructure.com/content-inventory)
"""
from rdflib import URIRef
NS = "https://vocab.methodandstructure.com/content-inventory#"
# Properties
slug = URIRef(NS + "slug")
......@@ -12,7 +12,7 @@ def test_prepare_query_for_one_to_one_metadata_for_communities():
query = prepare_query_for_one_to_one_metadata_for_communities([URIRef("community1"), URIRef("community2")])
assert normalize(query) == dedent_and_normalize("""
SELECT ?community ?description ?title ?bluesky_handle ?youtube_channel_id ?mastodon_address ?linkedin_id ?zenodo_community_id ?url ?slug
SELECT ?community ?description ?title ?bluesky_handle ?youtube_channel_id ?mastodon_address ?linkedin_id ?zenodo_community_id ?url
WHERE {
VALUES ( ?community ) {
( <community1> )
......@@ -44,9 +44,6 @@ def test_prepare_query_for_one_to_one_metadata_for_communities():
OPTIONAL {
?community <https://schema.org/url> ?url .
} .
OPTIONAL {
?community <https://vocab.methodandstructure.com/content-inventory#slug> ?slug .
} .
}
""")
......@@ -66,7 +63,7 @@ def test_get_one_to_one_metadata_for_communities_with_many_community_uris(triple
assert communities == {
URIRef('https://id.dalia.education/community/51340f34-d6fc-4ed5-b37d-b00a4d6f663a'): Community(
id='51340f34-d6fc-4ed5-b37d-b00a4d6f663a',
slug='None',
slug='data-carpentry',
title='Data Carpentry',
image=None,
url='',
......
......@@ -13,7 +13,7 @@ def test_prepare_query_for_one_to_one_metadata_for_resources():
query = prepare_query_for_one_to_one_metadata_for_resources([URIRef("abc"), URIRef("def")])
assert normalize(query) == dedent_and_normalize("""
SELECT ?lr ?description ?title ?subtitle ?created ?license ?fileSize ?url ?slug ?attachment
SELECT ?lr ?description ?title ?subtitle ?created ?license ?fileSize ?url ?attachment
WHERE {
VALUES ( ?lr ) {
( <abc> )
......@@ -42,9 +42,6 @@ def test_prepare_query_for_one_to_one_metadata_for_resources():
?lr <https://schema.org/url> ?url .
} .
OPTIONAL {
?lr <https://vocab.methodandstructure.com/content-inventory#slug> ?slug .
} .
OPTIONAL {
?lr <https://purl.org/ontology/modalia#hasMediaType> ?attachment .
FILTER ( ISLITERAL ( ?attachment ) ) .
} .
......@@ -56,7 +53,7 @@ def test_process_result_for_one_to_one_metadata_for_resources_with_minimal_resul
result = type('', (), {})() # https://stackoverflow.com/a/24448351
result.lr = "http://some.domain/a/cool/id/is/42"
result.slug = "my-slug"
result.slug = "my-title"
result.title = "My Title"
result.subtitle = None
result.description = None
......@@ -81,7 +78,7 @@ def test_process_result_for_one_to_one_metadata_for_resources_with_minimal_resul
learning_time=None,
versions=None,
id='42',
slug='my-slug',
slug='my-title',
title='My Title',
communities=None,
description='',
......@@ -185,7 +182,7 @@ def test_get_one_to_one_metadata_for_resources(triplestore):
learning_time=None,
versions=None,
id='65ae2d0f-7d94-4f0b-9714-470a2c372839',
slug='None',
slug='wageningenx-big-data-for-agri-food-principles-and-tools',
title='WageningenX: Big Data for Agri-Food: Principles and Tools',
communities=None,
description="During this course, you will understand how and why certain principles – such as "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment