| 371 | return Response({'error': _("Cannot duplicate task")}, status=status.HTTP_200_OK) |
| 372 | |
| 373 | def create(self, request, project_pk=None): |
| 374 | project = get_and_check_project(request, project_pk, ('change_project', )) |
| 375 | |
| 376 | # Check if user has permissions to set processing node |
| 377 | check_processing_node_perms(request) |
| 378 | |
| 379 | # Check if an alignment field is set to a valid task |
| 380 | # this means a user wants to align this task with another |
| 381 | align_to = request.data.get('align_to') |
| 382 | align_task = None |
| 383 | if align_to is not None and align_to != "auto" and align_to != "": |
| 384 | try: |
| 385 | align_task = models.Task.objects.select_related('project').get(pk=align_to) |
| 386 | check_project_perms(request, align_task.project, ('view_project', )) |
| 387 | except ObjectDoesNotExist: |
| 388 | raise exceptions.ValidationError(detail=_("Cannot create task, alignment task is not valid")) |
| 389 | |
| 390 | # If this is a partial task, we're going to upload images later |
| 391 | # for now we just create a placeholder task. |
| 392 | if request.data.get('partial'): |
| 393 | task = models.Task.objects.create(project=project, |
| 394 | pending_action=pending_actions.RESIZE if 'resize_to' in request.data else None) |
| 395 | if align_task is not None: |
| 396 | task.set_alignment_file_from(align_task) |
| 397 | |
| 398 | serializer = TaskSerializer(task, data=request.data, partial=True) |
| 399 | serializer.is_valid(raise_exception=True) |
| 400 | serializer.save() |
| 401 | else: |
| 402 | files = flatten_files(request.FILES) |
| 403 | |
| 404 | if len(files) <= 1: |
| 405 | raise exceptions.ValidationError(detail=_("Cannot create task, you need at least 2 images")) |
| 406 | |
| 407 | with transaction.atomic(): |
| 408 | task = models.Task.objects.create(project=project, |
| 409 | pending_action=pending_actions.RESIZE if 'resize_to' in request.data else None) |
| 410 | if align_task is not None: |
| 411 | task.set_alignment_file_from(align_task) |
| 412 | task.handle_images_upload(files) |
| 413 | task.images_count = len(task.scan_images()) |
| 414 | |
| 415 | # Update other parameters such as processing node, task name, etc. |
| 416 | serializer = TaskSerializer(task, data=request.data, partial=True) |
| 417 | serializer.is_valid(raise_exception=True) |
| 418 | serializer.save() |
| 419 | |
| 420 | worker_tasks.process_task.delay(task.id) |
| 421 | |
| 422 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 423 | |
| 424 | |
| 425 | def update(self, request, pk=None, project_pk=None, partial=False): |