def _sync_task(self, task, project, models, skip_subtasks=False):
        """Sync this task and its parent, dependencies, and subtasks

        For parents and subtasks, this method is called recursively, so skip_subtasks True is
        passed when syncing a parent task from a subtask.
        """
        task_id = task['gid']
        try:
            task_dict = self.client.tasks.find_by_id(task_id)
        except (ForbiddenError, NotFoundError):
            try:
                Task.objects.get(remote_id=task_id).delete()
            except Task.DoesNotExist:
                pass
            return
        logger.debug('Sync task %s', task_dict['name'])
        logger.debug(task_dict)

        if Task in models and self.commit:
            remote_id = task_dict['gid']
            parent = task_dict.pop('parent', None)
            dependencies = task_dict.pop('dependencies', None) or []
            if parent:
                # If this is a task we already know about, assume it was just synced.
                parent_id = parent['gid']
                if parent_id not in self.synced_ids and \
                        not Task.objects.filter(remote_id=parent_id).exists():
                    self._sync_task(parent,
                                    project,
                                    models,
                                    skip_subtasks=True)
                task_dict['parent_id'] = parent_id
            task_ = sync_task(remote_id,
                              task_dict,
                              project,
                              sync_tags=Tag in models)
            self.synced_ids.append(remote_id)
            if not skip_subtasks:
                for subtask in self.client.tasks.subtasks(task_id):
                    if subtask['gid'] not in self.synced_ids:
                        self._sync_task(subtask, project, models)
                if dependencies:
                    for subtask in dependencies:
                        if subtask['gid'] not in self.synced_ids:
                            self._sync_task(subtask, project, models)
                    task_.dependencies.set(
                        Task.objects.filter(
                            remote_id__in=[dep['gid']
                                           for dep in dependencies]))
        if Attachment in models and self.commit:
            for attachment in self.client.attachments.find_by_task(task_id):
                sync_attachment(self.client, task_, attachment['gid'])
        if Story in models and self.commit:
            for story in self.client.stories.find_by_task(task_id):
                self._sync_story(story)
        return
Esempio n. 2
0
    def _sync_task(self, task, project, models, skip_subtasks=False):
        """Sync this task and parent its subtasks

        For parents and subtasks, this method is called recursively, so skip_subtasks True is
        passed when syncing a parent task from a subtask.
        """
        synced_ids = []
        try:
            task_dict = self.client.tasks.find_by_id(task['id'])
        except (ForbiddenError, NotFoundError):
            try:
                Task.objects.get(remote_id=task['id']).delete()
            except Task.DoesNotExist:
                pass
            return
        logger.debug('Sync task %s', task_dict['name'])
        logger.debug(task_dict)

        if Task in models and self.commit:
            remote_id = task_dict.pop('id')
            parent = task_dict.pop('parent', None)
            if parent:
                # If this is a task we already know about, assume it was just synced.
                parent_id = parent['id']
                if not Task.objects.filter(remote_id=parent_id).exists():
                    ids = self._sync_task(parent,
                                          project,
                                          models,
                                          skip_subtasks=True)
                    synced_ids.extend(ids)
                task_dict['parent_id'] = parent_id
            task_ = sync_task(remote_id,
                              task_dict,
                              project,
                              sync_tags=Tag in models)
            synced_ids.append(remote_id)
            if not skip_subtasks:
                subtasks = self.client.tasks.subtasks(task['id'])
                for subtask in subtasks:
                    ids = self._sync_task(subtask, project, models)
                    synced_ids.extend(ids)
        if Attachment in models and self.commit:
            for attachment in self.client.attachments.find_by_task(task['id']):
                attachment_dict = self.client.attachments.find_by_id(
                    attachment['id'])
                logger.debug(attachment_dict)
                remote_id = attachment_dict.pop('id')
                if attachment_dict['parent']:
                    attachment_dict['parent'] = task_
                Attachment.objects.get_or_create(remote_id=remote_id,
                                                 defaults=attachment_dict)
        if Story in models and self.commit:
            for story in self.client.stories.find_by_task(task['id']):
                self._sync_story(story)
        return synced_ids