Skip to content
Snippets Groups Projects
Select Git revision
  • f70416ca4cba77778316c61a214d369d5593f014
  • main default protected
  • renovate/django-5.x
  • koma/feature/preference-polling-form
4 results

api.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    api.py 1.68 KiB
    from rest_framework import permissions, viewsets
    from rest_framework.response import Response
    
    from AKModel.models import Event
    from AKSolverInterface.serializers import ExportEventSerializer
    
    
    class ExportEventForSolverViewSet(viewsets.GenericViewSet):
        """
        API View: Current event, formatted to be consumed by a solver.
        Read-only
    
        Follows the format of the KoMa solver:
        https://github.com/Die-KoMa/ak-plan-optimierung/wiki/Input-&-output-format#input--output-format
        """
    
        permission_classes = (permissions.DjangoModelPermissions,)
        serializer_class = ExportEventSerializer
        lookup_url_kwarg = "event_slug"
        lookup_field = "slug"
        # only allow exporting of active events
        queryset = Event.objects.filter(active=True)
    
        # rename view name to avoid 'List' suffix
        def get_view_name(self):
            return "JSON-Export for scheduling solver"
    
        # somewhat hacky solution: TODO: FIXME
        def list(self, request, *args, **kwargs):
            """Construct HTTP response showing serialized event export."""
            #   Below is the code of mixins.RetrieveModelMixin::retrieve
            #   which would usually be used to serve this detail API page.
            #   However, we do not specify the event to serve at the end of the URL
            #   but instead prepend the URL with it, i.e.
            #   `<slug>/api/<export_endpoint>` instead of the usual `api/<export_endpoint>/<slug>`.
            #   To make this work with the DefaultRouter, we serve this page as a list instead
            #   of a detail page. Hence, we use the method `list` instead of `retrieve`.
            instance = self.get_object()
            serializer = self.get_serializer(instance)
            return Response(serializer.data)