Esempio n. 1
0
    def expected_result_json(objects, images):
        objects_serialized = TreeObjectSerializer(objects, many=True).data
        images_serialized = TreeImageSerializer(images, many=True).data

        response = {
            "directories": sorted(objects_serialized, key=lambda x: x["name"]),
            "images": sorted(images_serialized, key=lambda x: x["name"]),
        }
        return json.dumps(
            response,
            cls=encoders.JSONEncoder,
            ensure_ascii=not api_settings.UNICODE_JSON,
            allow_nan=not api_settings.STRICT_JSON,
            separators=SHORT_SEPARATORS
            if api_settings.COMPACT_JSON
            else LONG_SEPARATORS,
        )
    def get(self, request, pk=None):
        image_prefetch_related = ("modality", "study__patient")
        objects = []
        images = []
        if pk is None:
            objects = get_objects_for_user(request.user,
                                           "archives.view_archive")
        else:
            if Archive.objects.filter(pk=pk).exists():
                if (get_objects_for_user(
                        request.user,
                        "archives.view_archive").filter(pk=pk).exists()):
                    objects = Patient.objects.filter(
                        study__image__componentinterfacevalue__archive_items__archive__pk
                        =pk).distinct()
                    images = Image.objects.filter(
                        componentinterfacevalue__archive_items__archive__pk=pk,
                        study=None,
                    ).prefetch_related(*image_prefetch_related)
            elif Patient.objects.filter(pk=pk).exists():
                objects = Study.objects.filter(
                    patient__pk=pk,
                    image__componentinterfacevalue__archive_items__archive__in=
                    get_objects_for_user(request.user,
                                         "archives.view_archive"),
                ).distinct()

            elif Study.objects.filter(pk=pk).exists():
                images = Image.objects.filter(
                    study__pk=pk,
                    componentinterfacevalue__archive_items__archive__in=
                    get_objects_for_user(request.user,
                                         "archives.view_archive"),
                ).prefetch_related(*image_prefetch_related)
            else:
                return HttpResponse(status=status.HTTP_404_NOT_FOUND)

        objects_serialized = TreeObjectSerializer(objects, many=True).data
        images_serialized = TreeImageSerializer(images, many=True).data

        response = {
            "directories": sorted(objects_serialized, key=lambda x: x["name"]),
            "images": sorted(images_serialized, key=lambda x: x["name"]),
        }
        return Response(response)