3131from omi .dialects .oep .compiler import JSONCompiler
3232from omi .structure import OEPMetadata
3333from rest_framework import generics , status
34+ from rest_framework .permissions import IsAuthenticated
35+
36+ # views.py
37+ from rest_framework .response import Response
3438from rest_framework .views import APIView
3539
3640import api .parser
4347from api .serializers import (
4448 EnergyframeworkSerializer ,
4549 EnergymodelSerializer ,
50+ ScenarioBundleScenarioDatasetSerializer ,
4651 ScenarioDataTablesSerializer ,
4752)
53+ from api .utils import get_dataset_configs
4854from dataedit .models import Embargo
4955from dataedit .models import Schema as DBSchema
5056from dataedit .models import Table as DBTable
5157from dataedit .views import get_tag_keywords_synchronized_metadata , schema_whitelist
58+ from factsheet .permission_decorator import post_only_if_user_is_owner_of_scenario_bundle
5259from modelview .models import Energyframework , Energymodel
60+
61+ # from oekg.sparqlQuery import remove_datasets_from_scenario
62+ from oekg .utils import process_datasets_sparql_query
5363from oeplatform .settings import PLAYGROUNDS , UNVERSIONED_SCHEMAS , USE_LOEP , USE_ONTOP
5464
5565if USE_LOEP :
@@ -244,11 +254,11 @@ class Sequence(APIView):
244254 @api_exception
245255 def put (self , request , schema , sequence ):
246256 if schema not in PLAYGROUNDS and schema not in UNVERSIONED_SCHEMAS :
247- raise APIError (' Schema is not in allowed set of schemes for upload' )
257+ raise APIError (" Schema is not in allowed set of schemes for upload" )
248258 if schema .startswith ("_" ):
249- raise APIError (' Schema starts with _, which is not allowed' )
259+ raise APIError (" Schema starts with _, which is not allowed" )
250260 if request .user .is_anonymous :
251- raise APIError (' User is anonymous' , 401 )
261+ raise APIError (" User is anonymous" , 401 )
252262 if actions .has_table (dict (schema = schema , sequence_name = sequence ), {}):
253263 raise APIError ("Sequence already exists" , 409 )
254264 return self .__create_sequence (request , schema , sequence , request .data )
@@ -257,11 +267,11 @@ def put(self, request, schema, sequence):
257267 @require_delete_permission
258268 def delete (self , request , schema , sequence ):
259269 if schema not in PLAYGROUNDS and schema not in UNVERSIONED_SCHEMAS :
260- raise APIError (' Schema is not in allowed set of schemes for upload' )
270+ raise APIError (" Schema is not in allowed set of schemes for upload" )
261271 if schema .startswith ("_" ):
262- raise APIError (' Schema starts with _, which is not allowed' )
272+ raise APIError (" Schema starts with _, which is not allowed" )
263273 if request .user .is_anonymous :
264- raise APIError (' User is anonymous' , 401 )
274+ raise APIError (" User is anonymous" , 401 )
265275 return self .__delete_sequence (request , schema , sequence , request .data )
266276
267277 @load_cursor ()
@@ -371,9 +381,9 @@ def post(self, request, schema, table):
371381 :return:
372382 """
373383 if schema not in PLAYGROUNDS and schema not in UNVERSIONED_SCHEMAS :
374- raise APIError (' Schema is not in allowed set of schemes for upload' )
384+ raise APIError (" Schema is not in allowed set of schemes for upload" )
375385 if schema .startswith ("_" ):
376- raise APIError (' Schema starts with _, which is not allowed' )
386+ raise APIError (" Schema starts with _, which is not allowed" )
377387 json_data = request .data
378388
379389 if "column" in json_data ["type" ]:
@@ -423,11 +433,11 @@ def put(self, request, schema, table):
423433 :return:
424434 """
425435 if schema not in PLAYGROUNDS and schema not in UNVERSIONED_SCHEMAS :
426- raise APIError (' Schema is not in allowed set of schemes for upload' )
436+ raise APIError (" Schema is not in allowed set of schemes for upload" )
427437 if schema .startswith ("_" ):
428- raise APIError (' Schema starts with _, which is not allowed' )
438+ raise APIError (" Schema starts with _, which is not allowed" )
429439 if request .user .is_anonymous :
430- raise APIError (' User is anonymous' , 401 )
440+ raise APIError (" User is anonymous" , 401 )
431441 if actions .has_table (dict (schema = schema , table = table ), {}):
432442 raise APIError ("Table already exists" , 409 )
433443 json_data = request .data .get ("query" , {})
@@ -967,10 +977,10 @@ def get(self, request, schema, table, row_id=None):
967977 content_type = "text/csv" ,
968978 session = session ,
969979 )
970- response ["Content-Disposition" ] = (
971- 'attachment; filename="{schema}__{table}.csv"' . format (
972- schema = schema , table = table
973- )
980+ response [
981+ "Content-Disposition"
982+ ] = 'attachment; filename="{ schema}__{ table}.csv"' . format (
983+ schema = schema , table = table
974984 )
975985 return response
976986 elif format == "datapackage" :
@@ -998,10 +1008,10 @@ def get(self, request, schema, table, row_id=None):
9981008 content_type = "application/zip" ,
9991009 session = session ,
10001010 )
1001- response ["Content-Disposition" ] = (
1002- 'attachment; filename="{schema}__{table}.zip"' . format (
1003- schema = schema , table = table
1004- )
1011+ response [
1012+ "Content-Disposition"
1013+ ] = 'attachment; filename="{ schema}__{ table}.zip"' . format (
1014+ schema = schema , table = table
10051015 )
10061016 return response
10071017 else :
@@ -1574,3 +1584,56 @@ class ScenarioDataTablesListAPIView(generics.ListAPIView):
15741584 topic = "scenario"
15751585 queryset = DBTable .objects .filter (schema__name = topic )
15761586 serializer_class = ScenarioDataTablesSerializer
1587+
1588+
1589+ class ManageOekgScenarioDatasets (APIView ):
1590+ permission_classes = [IsAuthenticated ] # Require authentication
1591+
1592+ @post_only_if_user_is_owner_of_scenario_bundle
1593+ def post (self , request ):
1594+ serializer = ScenarioBundleScenarioDatasetSerializer (data = request .data )
1595+ if not serializer .is_valid ():
1596+ return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
1597+
1598+ try :
1599+ dataset_configs = get_dataset_configs (serializer .validated_data )
1600+ response_data = process_datasets_sparql_query (dataset_configs )
1601+ except APIError as e :
1602+ return Response ({"error" : str (e )}, status = e .status )
1603+ except Exception :
1604+ return Response ({"error" : "An unexpected error occurred." }, status = 500 )
1605+
1606+ if "error" in response_data :
1607+ return Response (response_data , status = status .HTTP_400_BAD_REQUEST )
1608+
1609+ return Response (response_data , status = status .HTTP_200_OK )
1610+
1611+ # @post_only_if_user_is_owner_of_scenario_bundle
1612+ # def delete(self, request):
1613+ # serializer = ScenarioBundleScenarioDatasetSerializer(data=request.data)
1614+ # if serializer.is_valid():
1615+ # scenario_uuid = serializer.validated_data["scenario"]
1616+ # datasets = serializer.validated_data["datasets"]
1617+
1618+ # # Iterate over each dataset to process it properly
1619+ # for dataset in datasets:
1620+ # dataset_name = dataset["name"]
1621+ # dataset_type = dataset["type"]
1622+
1623+ # # Remove the dataset from the scenario in the bundle
1624+ # success = remove_datasets_from_scenario(
1625+ # scenario_uuid, dataset_name, dataset_type
1626+ # )
1627+
1628+ # if not success:
1629+ # return Response(
1630+ # {"error": f"Failed to remove dataset {dataset_name}"},
1631+ # status=status.HTTP_400_BAD_REQUEST,
1632+ # )
1633+
1634+ # return Response(
1635+ # {"message": "Datasets removed successfully"},
1636+ # status=status.HTTP_200_OK,
1637+ # )
1638+
1639+ # return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
0 commit comments