Add images to a task
(self, request, pk=None, project_pk=None)
| 301 | |
| 302 | @action(detail=True, methods=['post']) |
| 303 | def upload(self, request, pk=None, project_pk=None): |
| 304 | """ |
| 305 | Add images to a task |
| 306 | """ |
| 307 | try: |
| 308 | task = self.queryset.get(pk=pk, project=project_pk) |
| 309 | check_project_perms(request, task.project, ('change_project', )) |
| 310 | except (ObjectDoesNotExist, ValidationError): |
| 311 | raise exceptions.NotFound() |
| 312 | |
| 313 | files = flatten_files(request.FILES) |
| 314 | if len(files) == 0: |
| 315 | raise exceptions.ValidationError(detail=_("No files uploaded")) |
| 316 | |
| 317 | chunk_info = None |
| 318 | chunk_index = request.data.get('dzchunkindex') |
| 319 | uuid = request.data.get('dzuuid') |
| 320 | total_chunk_count = request.data.get('dztotalchunkcount', None) |
| 321 | if len(files) == 1 and chunk_index is not None and uuid is not None and total_chunk_count is not None: |
| 322 | byte_offset = request.data.get('dzchunkbyteoffset', 0) |
| 323 | try: |
| 324 | chunk_index = int(chunk_index) |
| 325 | byte_offset = int(byte_offset) |
| 326 | total_chunk_count = int(total_chunk_count) |
| 327 | except ValueError: |
| 328 | raise exceptions.ValidationError(detail="chunkIndex is not an int") |
| 329 | |
| 330 | chunk_info = { |
| 331 | 'uuid': re.sub('[^0-9a-zA-Z-]+', "", uuid), |
| 332 | 'chunk_index': chunk_index, |
| 333 | 'byte_offset': byte_offset, |
| 334 | 'total_chunk_count': total_chunk_count, |
| 335 | 'tmp_upload_file': os.path.join(settings.FILE_UPLOAD_TEMP_DIR, f"{uuid}.upload") |
| 336 | } |
| 337 | |
| 338 | # 50% of the time, raise an exception |
| 339 | # import random |
| 340 | # if random.random() < 0.5: |
| 341 | # import time |
| 342 | # time.sleep(10) |
| 343 | # return Response('', status=524) |
| 344 | # raise exceptions.ValidationError(detail=_("Random upload failure for testing")) |
| 345 | |
| 346 | uploaded = task.handle_images_upload(files, chunk_info) |
| 347 | if len(uploaded) > 0: |
| 348 | task.images_count = len(task.scan_images()) |
| 349 | # Update other parameters such as processing node, task name, etc. |
| 350 | serializer = TaskSerializer(task, data=request.data, partial=True) |
| 351 | serializer.is_valid(raise_exception=True) |
| 352 | serializer.save() |
| 353 | |
| 354 | return Response({'success': True, 'uploaded': uploaded}, status=status.HTTP_200_OK) |
| 355 | |
| 356 | @action(detail=True, methods=['post']) |
| 357 | def duplicate(self, request, pk=None, project_pk=None): |
nothing calls this directly
no test coverage detected