from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def create_create_trans():
    """
    Для тестирования патча: ( cd /opt/eva-app; python3 -m patch.202504060947_create_create_trans )
    Здесь можно работать с моделями через models.CmfTask и т.д.
    Для прогрессбара используйте:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    print('Запуск патча createcreatetrans')
    with cmfutil.disable_acl(), cmfutil.disable_notify():
        status_code = models.CmfStatusCode.get(code='open', fields=['code'])

        for wf in models.CmfWorkflow.list():
            status_to = wf.get_default_status(status_type='OPEN', raise_error=False)
            if not status_to:
                status_to = models.CmfStatus()
                status_to.workflow = wf
                status_to.name = "Открыто"
                status_to.text = "Открыто"
                status_to.status_type = "OPEN"
                status_to.code = "open"
                status_to.status_code = status_code
                status_to.color = "#60b1bb"
                status_to.next_alarm = None
                status_to.save()

            # Если что, метод идемпотентный
            wf.create_create_trans()
            

if __name__ == "__main__":
    create_create_trans()
