def api_verify_annotation(request) -> Response: try: annotation_id = int(request.data['annotation_id']) if request.data['state'] == 'accept': state = True elif request.data['state'] == 'reject': state = False else: raise ParseError except (KeyError, TypeError, ValueError): raise ParseError annotation = get_object_or_404(Annotation, pk=annotation_id) if not annotation.image.image_set.has_perm('verify', request.user): return Response({ 'detail': 'permission for verifying annotations in this image set missing.', }, status=HTTP_403_FORBIDDEN) if state: annotation.verify(request.user, True) serializer = serialize_annotation(annotation) if Verification.objects.filter( user=request.user, verified=state, annotation=annotation).exists(): return Response({ 'annotation': serializer, 'detail': 'the user already verified this annotation and verified it now', }, status=HTTP_200_OK) return Response({ 'annotation': serializer, 'detail': 'you verified the last annotation', }, status=HTTP_200_OK) else: annotation.verify(request.user, False) serializer = serialize_annotation(annotation) if Verification.objects.filter( user=request.user, verified=state, annotation=annotation).exists(): return Response({ 'annotation': serializer, 'detail': 'the user already verified this annotation and rejected it now', }, status=HTTP_200_OK) return Response({ 'annotation': serializer, 'detail': 'you rejected the last annotation', }, 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.get('blurred', False) concealed = request.data.get('concealed', False) tempid = request.data.get('tempid', False) description = request.data.get('description', "") meta_data = request.data.get('meta_data', None) 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 annotation_type.validate_vector(vector) == False: return Response({ 'detail': 'the vector is not valid', }, status=HTTP_400_BAD_REQUEST) with transaction.atomic(): annotation = Annotation.objects.create(vector=vector, image=image, annotation_type=annotation_type, user=request.user, last_editor=request.user, _blurred=blurred, _concealed=concealed, description=description, meta_data=meta_data) if "unique_identifier" in request.data: annotation.unique_identifier = request.data["unique_identifier"] annotation.save() # Automatically verify for owner annotation.verify(request.user, True) # SlideRunner sync requires the option to set the last edit time to have it in sync with the database if "last_edit_time" in request.data: image.annotations.filter(id=annotation.id).update( last_edit_time=datetime.datetime.strptime( request.data["last_edit_time"], "%Y-%m-%dT%H:%M:%S.%f")) return Response( { 'annotations': serialize_annotation(annotation), 'tempid': tempid }, status=HTTP_201_CREATED)
def api_copy_annotation(request, source_annotation_id, target_image_id) -> Response: source_annotation = get_object_or_404(Annotation, pk=source_annotation_id) target_image = get_object_or_404(Image, pk=target_image_id) target_annotation_type = AnnotationType.objects.filter( product__in=target_image.image_set.product_set.all(), vector_type=source_annotation.annotation_type.vector_type, name=source_annotation.annotation_type.name).first() if target_annotation_type: source_annotation.image_id = target_image.id source_annotation.annotation_type = target_annotation_type source_annotation.id = None source_annotation.save() else: return Response( { 'detail': 'No target annotation type match the source annotation type {0}' .format(source_annotation.annotation_type.name), }, status=HTTP_403_FORBIDDEN) return Response({'annotations': serialize_annotation(source_annotation)}, status=HTTP_201_CREATED)
def update_annotation(request) -> Response: #TODO Depricated 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'] blurred = request.data.get('blurred', False) concealed = request.data.get('concealed', False) deleted = bool(request.data.get('deleted', False)) description = request.data.get('description', "") meta_data = request.data.get('meta_data', None) 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: return Response({ 'detail': 'the image id does not match the annotation id.', }, status=HTTP_400_BAD_REQUEST) 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 annotation_type.validate_vector(vector) == False: return Response({ 'detail': 'the vector is not valid', }, status=HTTP_400_BAD_REQUEST) with transaction.atomic(): annotation.annotation_type = annotation_type annotation.vector = vector annotation._concealed = concealed annotation._blurred = blurred annotation.last_editor = request.user annotation.annotation_type = annotation_type annotation.deleted = deleted annotation.description = description annotation.meta_data = meta_data annotation.save() # Automatically verify for owner annotation.verify(request.user, True) # SlideRunner sync requires the option to set the last edit time to have it in sync with the database if "last_edit_time" in request.data: annotation.image.annotations.filter(id=annotation.id).update(last_edit_time=datetime.datetime.strptime(request.data["last_edit_time"], "%Y-%m-%dT%H:%M:%S.%f")) return Response({ 'annotations': serialize_annotation(annotation), }, status=HTTP_200_OK)
def load_annotations(request) -> Response: try: image_id = int(request.query_params['image_id']) since = request.query_params.get('since', None) min_x = request.query_params.get('min_x', None) max_x = request.query_params.get('max_x', None) min_y = request.query_params.get('min_y', None) max_y = request.query_params.get('max_y', None) include_deleted = bool( request.query_params.get('include_deleted', False)) 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) annotations = image.annotations.select_related().filter( annotation_type__active=True) if image.image_set.collaboration_type == ImageSet.CollaborationTypes.COMPETITIVE: annotations = annotations.filter(user=request.user) if include_deleted is False: annotations = annotations.filter(deleted=include_deleted) if since is not None: annotations = annotations.filter( last_edit_time__gte=datetime.datetime.fromtimestamp(int(since))) annotations = annotations.filter(~Q( last_editor__username=request.user.username)) if min_x is not None and min_y is not None: annotations = annotations.filter(vector__x1__gte=int(min_x), vector__y1__gte=int(min_y)) if max_x is not None and max_y is not None: annotations = annotations.filter(vector__x1__lt=int(max_x), vector__y1__lt=int(max_y)) data = [serialize_annotation(a) for a in annotations] respond = Response({ 'annotations': data, }, status=HTTP_200_OK) return respond