from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def patch():
    with cmfutil.disable_acl(), cmfutil.disable_notify():
        # Подключаем Епики и подпроекты к Ганту проекта
        for project in tqdm(models.CmfProject.list(fields=['main_gantt_project'])):
            for task in models.CmfTask.list(filter=[
                        ['parent', '=', project],
                        ['parent_task', '=', None],
                        ['logic_prefix', 'IN', ['task.epic', 'task.subproject']],
                    ], fields=['parent_task', 'op_gantt_task']):
                task.parent_task = project.main_gantt_project
                task.save(only_data=True, notify=False, emit=False)
                task.op_gantt_task.parent_task = project.main_gantt_project
                task.op_gantt_task.save(only_data=True, notify=False, emit=False)


if __name__ == "__main__":
    patch()
