from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def patch():
    """
    Здесь можно работать с моделями через models.CmfTask и т.д.
    Для прогрессбара используйте:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    
    for task in models.CmfTask.list(
        filter=[['approved', '==', False], ['cache_status_type', '!=', 'closed'], ['parent_id', '!=', None]],
        fields=['cmf_owner', 'parent.cmf_owner']):
        if not task.parent:
            continue
        task.approve_for = task.parent.cmf_owner
        task.approve_for_place = 'approve2'
        task.save(only_data=True)

    for task in models.CmfTask.list(
        filter=[['responsible_id', '==', None], ['cache_status_type', '!=', 'closed'], ['parent_id', '!=', None], ['alarm_date', '<', 'NOW()']],
        fields=['cmf_owner', 'parent.cmf_owner']):
        if not task.parent:
            continue
        task.approve_for = task.parent.cmf_owner
        task.approve_for_place = 'approve3_notassigned'
        task.save(only_data=True)

    # approve_review
    for task in models.CmfTask.list(filter=[['cache_status_type', '=', 'in_review']], fields=['cmf_owner']):
        task.approve_for = task.cmf_owner
        task.approve_for_place = 'approve1_review'
        task.save(only_data=True)


if __name__ == "__main__":
    patch()
