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

add endpoint /curation/suggest/proficiency-levels

parent 5ccdad41
Branches
No related tags found
No related merge requests found
Pipeline #1552136 passed
from typing import List
from rdflib import RDF, RDFS, Variable
from project.dalia.api_models.api_models import LabelValueItem
from project.dalia.query.utils import query_ontologies_dataset
from project.dalia.query_builder.query_builder import Aggregates, FunctionExpressions, PropertyPaths, QueryBuilder
from project.dalia.rdf.namespace import MoDalia
# data for endpoint /curation/suggest/proficiency-levels
def get_proficiency_levels_suggestions() -> List[LabelValueItem]:
query = prepare_query_to_get_proficiency_levels()
results = query_ontologies_dataset(query)
return [_process_result(result) for result in results]
_VARIABLES = {
"level": Variable("level"),
"label": Variable("label"),
}
def prepare_query_to_get_proficiency_levels() -> str:
var_level = _VARIABLES["level"]
return QueryBuilder().SELECT(
*_VARIABLES.values()
).WHERE(
QueryBuilder().SELECT(
var_level,
count=Aggregates.COUNT(var_level),
).WHERE(
(var_level, RDF.type, MoDalia.Proficiency),
(var_level, PropertyPaths.ZeroOrMorePath(MoDalia.isLessProficientThan), Variable("moreProficientLevel")),
).GROUP_BY(
var_level
).ORDER_BY(
FunctionExpressions.DESC(Variable("count"))
).build(),
(var_level, RDFS.label, _VARIABLES["label"]),
).build()
def _process_result(result) -> LabelValueItem:
return LabelValueItem(
label=str(result.level),
value=str(result.label)
)
......@@ -9,6 +9,7 @@ NS = "https://purl.org/ontology/modalia#"
hasLearningType = URIRef(NS + "hasLearningType")
hasMediaType = URIRef(NS + "hasMediaType")
hasTargetGroup = URIRef(NS + "hasTargetGroup")
isLessProficientThan = URIRef(NS + "isLessProficientThan")
requiresProficiencyLevel = URIRef(NS + "requiresProficiencyLevel")
# Types
......@@ -27,6 +28,7 @@ MediaType = URIRef(NS + "MediaType")
Multipart = URIRef(NS + "Multipart")
PhDStudent = URIRef(NS + "PhDStudent")
Poster = URIRef(NS + "Poster")
Proficiency = URIRef(NS + "Proficiency")
Researcher = URIRef(NS + "Researcher")
StudentSchool = URIRef(NS + "StudentSchool")
TargetGroup = URIRef(NS + "TargetGroup")
......
......@@ -33,6 +33,11 @@ class CurationSuggestSearchRequestSerializer(DataclassSerializer):
dataclass = api_models.CurationSuggestSearchRequest
class LabelValueItemSerializer(DataclassSerializer):
class Meta:
dataclass = api_models.LabelValueItem
class CurationSuggestPaginatedResultSerializer(DataclassSerializer):
class Meta:
dataclass = api_models.CurationSuggestPaginatedResult
......
......@@ -15,4 +15,5 @@ urlpatterns = [
path('v1/curation/suggest/communities', views.CurationSuggestCommunitiesView.as_view(), name="curation_suggest_communities"),
path('v1/curation/suggest/disciplines', views.CurationSuggestDisciplinesView.as_view(), name="curation_suggest_disciplines"),
path('v1/curation/suggest/licenses', views.CurationSuggestLicensesView.as_view(), name="curation_suggest_licenses"),
path('v1/curation/suggest/proficiency-levels', views.CurationSuggestProficiencyLevelsView.as_view(), name="curation_suggest_proficiency_levels"),
]
......@@ -8,6 +8,7 @@ from rest_framework.views import APIView
from project.dalia.api_models.api_models import ItemSearchResult
from project.dalia.curation.suggest.communities import get_communities_suggestions
from project.dalia.curation.suggest.disciplines import get_disciplines_suggestions
from project.dalia.curation.suggest.proficiency_levels import get_proficiency_levels_suggestions
from project.dalia.curation.suggest.licenses import get_licenses_suggestions
from project.dalia.query.communities.communities import get_metadata_for_community
from project.dalia.query.communities.community_items import get_items_for_community
......@@ -119,3 +120,12 @@ class CurationSuggestLicensesView(APIView):
get_licenses_suggestions(request_serializer.validated_data)
)
return Response(result_serializer.data)
# endpoint /curation/suggest/proficiency-levels
class CurationSuggestProficiencyLevelsView(APIView):
def get(self, request: Request):
serializer = serializers.LabelValueItemSerializer(
get_proficiency_levels_suggestions(), many=True
)
return Response(serializer.data)
from django.urls import reverse
from rest_framework import status
from project.dalia.api_models.api_models import LabelValueItem
from project.dalia.curation.suggest.proficiency_levels import prepare_query_to_get_proficiency_levels
from project.dalia.serializers import LabelValueItemSerializer
from tests.project.dalia.utils import dedent_and_normalize, normalize
def test_prepare_query_to_get_proficiency_levels():
query = prepare_query_to_get_proficiency_levels()
assert normalize(query) == dedent_and_normalize("""
SELECT ?level ?label
WHERE {
{
SELECT ?level (COUNT( ?level ) as ?count)
WHERE {
?level <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://purl.org/ontology/modalia#Proficiency> .
?level <https://purl.org/ontology/modalia#isLessProficientThan>* ?moreProficientLevel .
}
GROUP BY ?level
ORDER BY DESC ( ?count )
}
?level <http://www.w3.org/2000/01/rdf-schema#label> ?label .
}
""")
def test_get_on_CurationSuggestProficiencyLevelsView_returns_200_and_valid_data(triplestore, api_client):
response = api_client.get(reverse("curation_suggest_proficiency_levels"))
assert response.status_code == status.HTTP_200_OK
serializer = LabelValueItemSerializer(data=response.data, many=True)
assert serializer.is_valid()
data = serializer.validated_data
assert data == [
LabelValueItem(label='https://purl.org/ontology/modalia#Novice', value='Novice'),
LabelValueItem(label='https://purl.org/ontology/modalia#Beginner', value='Advanced Beginner'),
LabelValueItem(label='https://purl.org/ontology/modalia#Competent', value='Competent'),
LabelValueItem(label='https://purl.org/ontology/modalia#Proficient', value='Proficient'),
LabelValueItem(label='https://purl.org/ontology/modalia#Expert', value='Expert'),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment