コード例 #1
0
def load_set_annotations(request) -> Response:
    try:
        imageset_id = int(request.query_params['imageset_id'])
        include_deleted = bool(
            request.query_params.get('include_deleted', False))
    except (KeyError, TypeError, ValueError):
        raise ParseError

    imageset = get_object_or_404(ImageSet, pk=imageset_id)
    images = Image.objects.filter(image_set=imageset)
    annotations = Annotation.objects.filter(image__in=images,
                                            annotation_type__active=True,
                                            deleted=include_deleted)

    if not imageset.has_perm('read', request.user):
        return Response(
            {
                'detail': 'permission for reading this image set missing.',
            },
            status=HTTP_403_FORBIDDEN)

    serializer = AnnotationSerializer(
        annotations.select_related().order_by('image__name',
                                              'annotation_type__sort_order'),
        many=True,
        context={'request': request},
    )
    return Response({
        'annotations': serializer.data,
    }, status=HTTP_200_OK)
コード例 #2
0
def load_filtered_set_annotations(request) -> Response:
    try:
        imageset_id = int(request.query_params['imageset_id'])
        verified = request.query_params['verified'] == 'true'
        annotation_type_id = int(request.query_params['annotation_type'])
        include_deleted = bool(request.query_params.get('include_deleted', False))
    except (KeyError, TypeError, ValueError):
        raise ParseError

    imageset = get_object_or_404(ImageSet, pk=imageset_id)
    images = Image.objects.filter(image_set=imageset)
    annotations = Annotation.objects.filter(image__in=images,
                                            annotation_type__active=True,
                                            deleted=include_deleted).select_related()
    user_verifications = Verification.objects.filter(user=request.user, annotation__in=annotations)
    if annotation_type_id > -1:
        annotations = annotations.filter(annotation_type__id=annotation_type_id)
    if verified:
        annotations = [annotation for annotation in annotations if not user_verifications.filter(annotation=annotation).exists()]

    if not imageset.has_perm('read', request.user):
        return Response({
            'detail': 'permission for reading this image set missing.',
        }, status=HTTP_403_FORBIDDEN)

    serializer = AnnotationSerializer(
        sorted(list(annotations), key=lambda annotation: annotation.image.id),
        many=True,
        context={'request': request},
    )
    return Response({
        'annotations': serializer.data,
    }, status=HTTP_200_OK)
コード例 #3
0
def api_delete_annotation(request) -> Response:
    try:
        annotation_id = int(request.query_params['annotation_id'])
        keep_deleted_element = bool(request.query_params.get('keep_deleted_element', False))
    except (KeyError, TypeError, ValueError):
        raise ParseError

    annotation = get_object_or_404(
        Annotation.objects.select_related(), pk=annotation_id)

    if not annotation.image.image_set.has_perm('delete_annotation', request.user):
        return Response({
            'detail': 'permission for deleting annotations in this image set missing.',
        }, status=HTTP_403_FORBIDDEN)

    if keep_deleted_element:
        with transaction.atomic():
            annotation.last_editor = request.user
            annotation.deleted = True
            annotation.save()
    else:
        annotation.delete()

    annotation.id = annotation_id

    serializer = AnnotationSerializer(
        annotation,
        context={'request': request, },
        many=False)
    return Response({
        'annotations': serializer.data,
    }, status=HTTP_200_OK)
コード例 #4
0
def load_annotation(request) -> Response:
    try:
        annotation_id = int(request.query_params['annotation_id'])
    except (KeyError, TypeError, ValueError):
        raise ParseError

    annotation = get_object_or_404(Annotation, pk=annotation_id)

    if not annotation.image.image_set.has_perm('read', request.user):
        return Response({
            'detail': 'permission for reading this image set missing.',
        }, status=HTTP_403_FORBIDDEN)

    serializer = AnnotationSerializer(annotation,
                                      context={
                                          'request': request,
                                      },
                                      many=False)
    return Response({
        'annotation': serializer.data,
    }, status=HTTP_200_OK)