Skip to content

Commit

Permalink
Merge pull request #1928 from OpenEnergyPlatform/feature-implement-su…
Browse files Browse the repository at this point in the history
…pported-sparql-query--response-formats

Feature implement supported sparql query  response formats and web api for oekg
  • Loading branch information
jh-RLI authored Feb 20, 2025
2 parents aa5bc18 + ece28cd commit 4fd7787
Show file tree
Hide file tree
Showing 42 changed files with 9,180 additions and 798 deletions.
5 changes: 5 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
path("grpprop/", views.get_groups),
path("oeo-search", views.oeo_search),
path("oevkg-query", views.oevkg_search),
re_path(
r"^v0/oekg/sparql/?$",
views.OekgSparqlAPIView.as_view(),
name="oekg-sparql-http-api",
),
re_path(
r"^v0/factsheet/frameworks/?$",
views.EnergyframeworkFactsheetListAPIView.as_view(),
Expand Down
36 changes: 31 additions & 5 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from omi.dialects.oep.compiler import JSONCompiler
from omi.structure import OEPMetadata
from rest_framework import generics, status
from rest_framework.authentication import TokenAuthentication
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated

# views.py
from rest_framework.response import Response
from rest_framework.views import APIView

Expand All @@ -57,9 +57,11 @@
from dataedit.views import get_tag_keywords_synchronized_metadata, schema_whitelist
from factsheet.permission_decorator import post_only_if_user_is_owner_of_scenario_bundle
from modelview.models import Energyframework, Energymodel

# from oekg.sparqlQuery import remove_datasets_from_scenario
from oekg.utils import process_datasets_sparql_query
from oekg.utils import (
execute_sparql_query,
process_datasets_sparql_query,
validate_public_sparql_query,
)
from oeplatform.settings import PLAYGROUNDS, UNVERSIONED_SCHEMAS, USE_LOEP, USE_ONTOP

if USE_LOEP:
Expand Down Expand Up @@ -1516,6 +1518,30 @@ def oeo_search(request):
return JsonResponse(res, safe=False)


class OekgSparqlAPIView(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request):
sparql_query = request.data.get("query", "")
response_format = request.data.get("format", "json") # Default format

if not validate_public_sparql_query(sparql_query):
raise ValidationError(
"Invalid SPARQL query. Update/delete queries are not allowed."
)

try:
content, content_type = execute_sparql_query(sparql_query, response_format)
except ValueError as e:
raise ValidationError(str(e))

if content_type == "application/sparql-results+json":
return Response(content)
else:
return Response(content, content_type=content_type)


def oevkg_search(request):
if USE_ONTOP:
# get query from user request # TODO validate input to prevent sneaky stuff
Expand Down
2 changes: 1 addition & 1 deletion dataedit/templates/dataedit/meta_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1 class="main-header__title">
<br>
<div>
Find a detailed description for all fields of the OEMetaData visible on <a href="{{oem_key_desc}}" target="_blank">GitHub</a>.
For a brief introduction on how to work with the oemetadata and the OEMetaBuilder tool find our <a href="{{oemetadata_tutorial}}" target="_blank">tutorial</a> in the academy.
For a brief introduction on how to work with the oemetadata and the <a href="{{oemetabuilder_tutorial}}" target="_blank">OEMetaBuilder</a> tool find our <a href="{{oemetadata_tutorial}}" target="_blank">tutorial</a> in the academy.
</div>
</div>
<div class=" d-flex justify-content-center">
Expand Down
4 changes: 3 additions & 1 deletion dataedit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def listschemas(request):
"schemas": schemas,
"query": searched_query_string,
"tags": searched_tag_ids,
"doc_oem_builder_link": DOCUMENTATION_LINKS["oemetabuilder"],
"doc_oem_builder_link": EXTERNAL_URLS["tutorials_oemetabuilder"],
},
)

Expand Down Expand Up @@ -1856,6 +1856,7 @@ def get(self, request, schema, table):
"doc_links": DOCUMENTATION_LINKS,
"oem_key_desc": EXTERNAL_URLS["oemetadata_key_description"],
"oemetadata_tutorial": EXTERNAL_URLS["tutorials_oemetadata"],
"oemetabuilder_tutorial": EXTERNAL_URLS["tutorials_oemetabuilder"],
}

return render(
Expand All @@ -1873,6 +1874,7 @@ def get(self, request):
),
"oem_key_desc": EXTERNAL_URLS["oemetadata_key_description"],
"oemetadata_tutorial": EXTERNAL_URLS["tutorials_oemetadata"],
"oemetabuilder_tutorial": EXTERNAL_URLS["tutorials_oemetabuilder"],
}
return render(
request,
Expand Down
8 changes: 8 additions & 0 deletions docs/installation/guides/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ The oeo-viewer is a visualization tool for our OEO ontology and it is under deve
After these steps, a `static` folder inside `oep-website/oeplatform/oeo_viewer/` will be created which includes the results of the `npm run build` command. These files are necessary for the oeo-viewer.
### 6.2 Setup the oeo extended app
The oeo extended is used to create new ontology classes on the fly while annotating a oemetadata document. Especially if one wants to annotate composed units which are more complex than a simple meter or square meter unit. These composed units are stored separate from the OEO ontology in the OEOX which is located in its own owl file.
The use case described above requires to setup the oeo extended template file in the `media/` directory. We offer a template file to simplify this task.
cp oeo_ext/oeo_extended_store/oeox_template/oeo_ext_template_empty.owl media/oeo_ext/oeo_ext.owl
## 7 Setup the Scenario-Bundles app
!!! note "Optional Step"
Expand Down
Loading

0 comments on commit 4fd7787

Please sign in to comment.