from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def remove_gantt_project_from_parent_task():
    """
    Для тестирования патча: ( cd /opt/eva-app; python3 -m patch.202404251141_remove_gantt_project_from_parent_task )
    На базе BCRM ~60 сек
    """
    print('Запуск патча remove_gantt_project_from_parent_task')

    filter = ['parent_task.logic_prefix', '==', 'task.gantt_project']

    while True:
        tasks = models.CmfTask.list(filter=filter, slice=[0, 1000], fields=['op_gantt_task'], include_deleted=True)

        if not tasks:
            break

        for task in tasks:
            task.parent_task = None
            task.save(only_data=True, notify=False, emit=False)

            task.op_gantt_task.parent_task = None
            task.op_gantt_task.save(only_data=True, notify=False, emit=False)

        commit_all_ds()


if __name__ == "__main__":
    remove_gantt_project_from_parent_task()
