Coverage for backend/core/auxiliary/views/CompoundSuggestionViews.py: 58%

26 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-11-06 23:27 +0000

1from rest_framework.decorators import api_view 

2from rest_framework.response import Response 

3from rest_framework import serializers 

4from drf_spectacular.utils import extend_schema 

5from core.auxiliary.methods.CompoundSuggestionMethods import get_compound_suggestions 

6from core.auxiliary.models.Flowsheet import Flowsheet 

7from flowsheetInternals.unitops.models.SimulationObject import SimulationObject 

8from drf_spectacular.utils import OpenApiParameter, OpenApiTypes, extend_schema 

9from core.validation import api_view_validate 

10from flowsheetInternals.unitops.serializers.SimulationObjectSerializer import SimulationObjectSerializer 

11import json 

12 

13@api_view_validate 

14@extend_schema(parameters=[ 

15 OpenApiParameter(name="flowsheet", required=True, type=OpenApiTypes.INT), 

16 OpenApiParameter(name="stream_id", required=True,type=OpenApiTypes.INT) 

17]) 

18@api_view(['get']) 

19def get_suggestions(request): 

20 try: 

21 flowsheet_id=request.query_params.get('flowsheet') 

22 stream_id=request.query_params.get('stream_id') 

23 

24 suggestions:list = get_compound_suggestions(flowsheet_id=flowsheet_id, stream_id=stream_id) 

25 return Response({"status":"success", "data":suggestions}, status=200) 

26 

27 except ValueError as e: 

28 return Response({"status": "error", "message": "flowsheet_id and stream_id must be integers. " + json.dumps(SimulationObjectSerializer(SimulationObject.objects.get(id=stream_id)).data) + str(e)}, status=400) 

29 except SimulationObject.DoesNotExist: 

30 return Response({"status": "error", "message": "Stream or Flowsheet not found"}, status=404) 

31 except Exception as e: 

32 return Response({'status': 'error', 'message': str(e)}, status=400)