|
18 | 18 | from imagetagger.annotations.forms import ExportFormatCreationForm, ExportFormatEditForm
|
19 | 19 | from imagetagger.annotations.models import Annotation, AnnotationType, Export, \
|
20 | 20 | Verification, ExportFormat
|
21 |
| -from imagetagger.annotations.serializers import AnnotationSerializer, AnnotationTypeSerializer |
| 21 | +from imagetagger.annotations.serializers import AnnotationSerializer, AnnotationTypeSerializer, ExportFormatInfoSerializer |
22 | 22 | from imagetagger.images.models import Image, ImageSet
|
23 | 23 | from imagetagger.users.models import Team
|
24 | 24 |
|
@@ -921,3 +921,54 @@ def api_blurred_concealed_annotation(request) -> Response:
|
921 | 921 | return Response({
|
922 | 922 | 'detail': 'you updated the last annotation',
|
923 | 923 | }, status=HTTP_200_OK)
|
| 924 | + |
| 925 | + |
| 926 | +@login_required |
| 927 | +@api_view(['POST']) |
| 928 | +def api_create_export(request) -> Response: |
| 929 | + try: |
| 930 | + image_set_id = int(request.data['imageset_id']) |
| 931 | + export_format_id = int(request.data['export_format_id']) |
| 932 | + except (KeyError, TypeError, ValueError): |
| 933 | + raise ParseError |
| 934 | + |
| 935 | + imageset = get_object_or_404(ImageSet, id=image_set_id) |
| 936 | + if imageset.has_perm('create_export', request.user): |
| 937 | + format = get_object_or_404(ExportFormat, id=export_format_id) |
| 938 | + export_text, annotation_count, export_filename = export_format(format, imageset) |
| 939 | + |
| 940 | + export = Export(image_set=imageset, |
| 941 | + user=request.user, |
| 942 | + annotation_count=annotation_count, |
| 943 | + export_text=export_text, |
| 944 | + format=format) |
| 945 | + export.save() |
| 946 | + export.filename = export_filename.replace('%%exportid', str(export.id)) |
| 947 | + export.save() |
| 948 | + |
| 949 | + return Response({ |
| 950 | + 'detail': 'export created successfully.', |
| 951 | + 'export_id': export.id, |
| 952 | + }, status=HTTP_201_CREATED) |
| 953 | + return Response({ |
| 954 | + 'detail': 'permission for exporting annotations in this image set missing.', |
| 955 | + }, status=HTTP_403_FORBIDDEN) |
| 956 | + |
| 957 | + |
| 958 | +@login_required |
| 959 | +@api_view(['GET']) |
| 960 | +def api_get_export_formats(request) -> Response: |
| 961 | + user_teams = Team.objects.filter(members=request.user) |
| 962 | + export_formats = ExportFormat.objects.filter(Q(public=True) | Q(team__in=user_teams)) |
| 963 | + serializer = ExportFormatInfoSerializer( |
| 964 | + export_formats, |
| 965 | + many=True, |
| 966 | + context={ |
| 967 | + 'request': request, |
| 968 | + } |
| 969 | + ) |
| 970 | + return Response({ |
| 971 | + 'detail': 'your user has access to the following export formats', |
| 972 | + 'export_formats': serializer.data, |
| 973 | + }, status=HTTP_200_OK) |
| 974 | + |
0 commit comments