Skip to content

Commit

Permalink
add base version of #1890
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-RLI committed Oct 21, 2024
1 parent 0e14cb3 commit ee146b8
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
from omi.dialects.oep.compiler import JSONCompiler
from omi.structure import OEPMetadata
from rest_framework import generics, status
from rest_framework.permissions import IsAuthenticated

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

import api.parser
Expand All @@ -42,12 +46,15 @@
from api.serializers import (
EnergyframeworkSerializer,
EnergymodelSerializer,
ScenarioBundleScenarioDatasetSerializer,
ScenarioDataTablesSerializer,
)
from api.sparql_helpers import add_datasets_to_scenario, remove_datasets_from_scenario
from dataedit.models import Embargo
from dataedit.models import Schema as DBSchema
from dataedit.models import Table as DBTable
from dataedit.views import get_tag_keywords_synchronized_metadata, schema_whitelist
from factsheet.permission_decorator import only_if_user_is_owner_of_scenario_bundle
from modelview.models import Energyframework, Energymodel
from oeplatform.settings import PLAYGROUNDS, UNVERSIONED_SCHEMAS, USE_LOEP, USE_ONTOP

Expand Down Expand Up @@ -1571,3 +1578,58 @@ class ScenarioDataTablesListAPIView(generics.ListAPIView):
topic = "scenario"
queryset = DBTable.objects.filter(schema__name=topic)
serializer_class = ScenarioDataTablesSerializer


class ManageScenarioDatasets(APIView):
permission_classes = [IsAuthenticated] # Require authentication

@only_if_user_is_owner_of_scenario_bundle
def post(self, request):
serializer = ScenarioBundleScenarioDatasetSerializer(data=request.data)
if serializer.is_valid():
scenario_uuid = serializer.validated_data["scenario"]
datasets = serializer.validated_data["dataset"]
dataset_type = serializer.validated_data["type"]

# Add datasets to the scenario in the bundle (implementation depends
# on your model)
success = add_datasets_to_scenario(scenario_uuid, datasets, dataset_type)

if success:
return Response(
{"message": "Datasets added successfully"},
status=status.HTTP_200_OK,
)
else:
return Response(
{"error": "Failed to add datasets"},
status=status.HTTP_400_BAD_REQUEST,
)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@only_if_user_is_owner_of_scenario_bundle
def delete(self, request):
serializer = ScenarioBundleScenarioDatasetSerializer(data=request.data)
if serializer.is_valid():
scenario_uuid = serializer.validated_data["scenario"]
datasets = serializer.validated_data["dataset"]
dataset_type = serializer.validated_data["type"]

# Remove datasets from the scenario in the bundle
success = remove_datasets_from_scenario(
scenario_uuid, datasets, dataset_type
)

if success:
return Response(
{"message": "Datasets removed successfully"},
status=status.HTTP_200_OK,
)
else:
return Response(
{"error": "Failed to remove datasets"},
status=status.HTTP_400_BAD_REQUEST,
)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit ee146b8

Please sign in to comment.