def add_task(self, task_fields: dict, files: dict, project_data: ProjectData = None): def split_name(file): _, name = file.split(files['data_root']) return name data_serializer = DataSerializer(data={ "server_files": files['media'], #TODO: followed fields whould be replaced with proper input values from request in future "use_cache": False, "use_zip_chunks": True, "image_quality": 70, }) data_serializer.is_valid(raise_exception=True) db_data = data_serializer.save() db_task = TaskSerializer.create(None, { **task_fields, 'data_id': db_data.id, 'project_id': self.db_project.id }) data = {k:v for k, v in data_serializer.data.items()} data['use_zip_chunks'] = data_serializer.validated_data['use_zip_chunks'] data['use_cache'] = data_serializer.validated_data['use_cache'] data['copy_data'] = data_serializer.validated_data['copy_data'] data['server_files_path'] = files['data_root'] data['stop_frame'] = None data['server_files'] = list(map(split_name, data['server_files'])) create_task(db_task, data, isDatasetImport=True) self.db_tasks = models.Task.objects.filter(project__id=self.db_project.id).order_by('id') self.init_from_db() if project_data is not None: project_data.new_tasks.add(db_task.id) project_data.init()
class ProjectViewSet(auth.ProjectGetQuerySetMixin, viewsets.ModelViewSet): queryset = models.Project.objects.all().order_by('-id') serializer_class = ProjectSerializer search_fields = ("name", "owner__username", "assignee__username", "status") filterset_class = ProjectFilter ordering_fields = ("id", "name", "owner", "status", "assignee") http_method_names = ['get', 'post', 'head', 'patch', 'delete'] def get_permissions(self): http_method = self.request.method permissions = [IsAuthenticated] if http_method in SAFE_METHODS: permissions.append(auth.ProjectAccessPermission) elif http_method in ["POST"]: permissions.append(auth.ProjectCreatePermission) elif http_method in ["PATCH"]: permissions.append(auth.ProjectChangePermission) elif http_method in ["DELETE"]: permissions.append(auth.ProjectDeletePermission) else: permissions.append(auth.AdminRolePermission) return [perm() for perm in permissions] def perform_create(self, serializer): if self.request.data.get('owner', None): serializer.save() else: serializer.save(owner=self.request.user) @swagger_auto_schema( method='get', operation_summary= 'Returns information of the tasks of the project with the selected id', responses={'200': TaskSerializer(many=True)}) @action(detail=True, methods=['GET'], serializer_class=TaskSerializer) def tasks(self, request, pk): self.get_object() # force to call check_object_permissions queryset = Task.objects.filter(project_id=pk).order_by('-id') queryset = auth.filter_task_queryset(queryset, request.user) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True, context={"request": request}) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True, context={"request": request}) return Response(serializer.data)
def serialize_task(): task_serializer = TaskSerializer(self._db_task) for field in ('url', 'owner', 'assignee', 'segments'): task_serializer.fields.pop(field) task = self._prepare_task_meta(task_serializer.data) task['labels'] = [ self._prepare_label_meta(l) for l in task['labels'] ] for label in task['labels']: label['attributes'] = [ self._prepare_attribute_meta(a) for a in label['attributes'] ] return task