Exemplo n.º 1
0
    def responses(self, request, uuid=None):
        """
        Flow Results Responses endpoint.
        """
        status_code = status.HTTP_200_OK
        xform = self.get_object()
        uuid = str(UUID(uuid or xform.uuid, version=4))
        data = {"id": uuid, "type": "flow-results-data", "attributes": {}}
        headers = {
            'Content-Type': 'application/vnd.api+json',
            'Location': self.request.build_absolute_uri(
                reverse('flow-results-responses', kwargs={'uuid': uuid}))
        }  # yapf: disable
        if request.method == 'POST':
            serializer = FlowResultsResponseSerializer(
                data=request.data, context={'request': request})
            serializer.is_valid(raise_exception=True)
            serializer.save()
            data['response'] = serializer.data['responses']
            if serializer.data['duplicates']:
                status_code = status.HTTP_202_ACCEPTED
            else:
                status_code = status.HTTP_201_CREATED
        else:
            if xform.is_merged_dataset:
                pks = xform.mergedxform.xforms.filter(
                    deleted_at__isnull=True).values_list('pk', flat=True)
                queryset = Instance.objects.filter(
                    xform_id__in=pks,
                    deleted_at__isnull=True).values_list('json', flat=True)
            else:
                queryset = xform.instances.values_list('json', flat=True)

            paginate_queryset = self.paginate_queryset(queryset)
            if paginate_queryset:
                data['attributes']['responses'] = floip_list(paginate_queryset)
                response = self.get_paginated_response(data)
                for key, value in headers.items():
                    response[key] = value

                return response

            data['attributes']['responses'] = floip_list(queryset)

        return Response(data, headers=headers, status=status_code)
Exemplo n.º 2
0
    def responses(self, request, uuid=None):
        """
        Flow Results Responses endpoint.
        """
        status_code = status.HTTP_200_OK
        xform = self.get_object()
        data = {
            "id": uuid or xform.uuid,
            "type": "flow-results-data",
        }
        headers = {
            'Content-Type': 'application/vnd.api+json',
            'Location': self.request.build_absolute_uri(
                reverse('flow-results-responses', kwargs={'uuid': xform.uuid}))
        }  # yapf: disable
        if request.method == 'POST':
            serializer = FlowResultsResponseSerializer(
                data=request.data, context={'request': request})
            serializer.is_valid(raise_exception=True)
            serializer.save()
            data['response'] = serializer.data['responses']
            if serializer.data['duplicates']:
                status_code = status.HTTP_202_ACCEPTED
            else:
                status_code = status.HTTP_201_CREATED
        else:
            queryset = xform.instances.values_list('json', flat=True)
            paginate_queryset = self.paginate_queryset(queryset)
            if paginate_queryset:
                data['responses'] = floip_list(paginate_queryset)
                response = self.get_paginated_response(data)
                for key, value in headers.items():
                    response[key] = value

                return response

            data['responses'] = floip_list(queryset)

        return Response(data, headers=headers, status=status_code)
Exemplo n.º 3
0
    def responses(self, request, uuid=None):
        """
        Flow Results Responses endpoint.
        """
        status_code = status.HTTP_200_OK
        xform = self.get_object()
        data = {
            "id": uuid or xform.uuid,
            "type": "flow-results-data",
        }
        headers = {
            'Content-Type': 'application/vnd.api+json',
            'Location': self.request.build_absolute_uri(
                reverse('flow-results-responses', kwargs={'uuid': xform.uuid}))
        }  # yapf: disable
        if request.method == 'POST':
            serializer = FlowResultsResponseSerializer(
                data=request.data, context={'request': request})
            serializer.is_valid(raise_exception=True)
            serializer.save()
            data['response'] = serializer.data['responses']
            status_code = status.HTTP_201_CREATED
        else:
            queryset = xform.instances.values_list('json', flat=True)
            paginate_queryset = self.paginate_queryset(queryset)
            if paginate_queryset:
                data['responses'] = floip_list(paginate_queryset)
                response = self.get_paginated_response(data)
                for key, value in headers.items():
                    response[key] = value

                return response

            data['responses'] = floip_list(queryset)

        return Response(data, headers=headers, status=status_code)