def load_multiple_annotations(request) -> Response: try: image_id = int(request.query_params['image_id']) annotation_type_id = int(request.query_params['annotation_type_id']) except (KeyError, TypeError, ValueError): raise ParseError annotations = Annotation.objects.filter( image_id=image_id, annotation_type_id=annotation_type_id) if not Image.objects.get(id=image_id).image_set.has_perm( 'read', request.user): return Response( { 'detail': 'permission for reading this image set missing.', }, status=HTTP_403_FORBIDDEN) serializer = AnnotationSerializer(annotations, context={ 'request': request, }, many=True) return Response({ 'annotation': serializer.data, }, status=HTTP_200_OK)
def api_delete_annotation(request) -> Response: try: annotation_id = int(request.query_params['annotation_id']) 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) image = annotation.image annotation.delete() serializer = AnnotationSerializer( image.annotations.select_related().filter( annotation_type__active=True).order_by('annotation_type__name'), context={ 'request': request, }, many=True) return Response({ 'annotations': serializer.data, }, status=HTTP_200_OK)
def load_set_annotations(request) -> Response: try: imageset_id = int(request.query_params['imageset_id']) 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) 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__name'), many=True, context={'request': request}, ) return Response({ 'annotations': serializer.data, }, status=HTTP_200_OK)
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']) 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).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)
def create_annotation(request) -> Response: try: image_id = int(request.data['image_id']) annotation_type_id = int(request.data['annotation_type_id']) vector = request.data['vector'] blurred = request.data['blurred'] concealed = request.data['concealed'] except (KeyError, TypeError, ValueError): raise ParseError image = get_object_or_404(Image, pk=image_id) annotation_type = get_object_or_404(AnnotationType, pk=annotation_type_id) if not image.image_set.has_perm('annotate', request.user): return Response( { 'detail': 'permission for annotating in this image set missing.', }, status=HTTP_403_FORBIDDEN) if not annotation_type.validate_vector(vector): print(vector) return Response({ 'detail': 'the vector is invalid.', }, status=HTTP_400_BAD_REQUEST) if Annotation.similar_annotations(vector, image, annotation_type): return Response({ 'detail': 'similar annotation exists.', }) with transaction.atomic(): annotation = Annotation.objects.create(vector=vector, image=image, annotation_type=annotation_type, user=request.user, _blurred=blurred, _concealed=concealed) # Automatically verify for owner annotation.verify(request.user, True) serializer = AnnotationSerializer(annotation.image.annotations.filter( annotation_type__active=True).select_related().order_by( 'annotation_type__name'), context={ 'request': request, }, many=True) return Response({ 'annotations': serializer.data, }, status=HTTP_201_CREATED)
def update_annotation(request) -> Response: try: annotation_id = int(request.data['annotation_id']) image_id = int(request.data['image_id']) annotation_type_id = int(request.data['annotation_type_id']) vector = request.data['vector'] except (KeyError, TypeError, ValueError): raise ParseError annotation = get_object_or_404(Annotation, pk=annotation_id) annotation_type = get_object_or_404(AnnotationType, pk=annotation_type_id) if annotation.image_id != image_id: raise ParseError('the image id does not match the annotation id.') if not annotation.image.image_set.has_perm('edit_annotation', request.user): return Response( { 'detail': 'permission for updating annotations in this image set missing.', }, status=HTTP_403_FORBIDDEN) if not Annotation.validate_vector(vector, Annotation.VECTOR_TYPE.BOUNDING_BOX): return Response({'detail': 'the vector is invalid.'}, status=HTTP_400_BAD_REQUEST) if Annotation.similar_annotations(vector, annotation.image, annotation_type, exclude={annotation.id}): annotation.delete() return Response({ 'detail': 'similar annotation exists.', }) with transaction.atomic(): annotation.annotation_type = annotation_type annotation.vector = vector annotation.last_editor = request.user annotation.save() annotation.annotation_type = annotation_type # Automatically verify for owner annotation.verify(request.user, True) serializer = AnnotationSerializer( annotation.image.annotations.select_related() \ .order_by('annotation_type__name'), many=True) return Response({ 'annotations': serializer.data, }, status=HTTP_200_OK)
def load_annotations(request) -> Response: try: image_id = int(request.query_params['image_id']) except (KeyError, TypeError, ValueError): raise ParseError image = get_object_or_404(Image, pk=image_id) if not image.image_set.has_perm('read', request.user): return Response({ 'detail': 'permission for reading this image set missing.', }, status=HTTP_403_FORBIDDEN) serializer = AnnotationSerializer( image.annotations.select_related().order_by('annotation_type__name'), many=True) return Response({ 'annotations': serializer.data, }, status=HTTP_200_OK)
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)