from patch.include import *


@app_context(commit=True)
def setting_current_approves():
    """
    Для тестирования патча: ( cd /opt/eva-app; python3 -m patch.202407082321_setting_current_approves )
    """
    print('Запуск патча setting_current_approves')

    filter = ["approves", "EXISTS", None]

    for model in (
        models.CmfTask,
        models.CmfDocument,
        models.CmfList,
    ):
        step = 1000
        current_step = 0

        objs_with_approves_count = model.count(filter=filter)

        while current_step < objs_with_approves_count:
            next_step = current_step + step
            
            objs_with_approves = model.list(filter=filter, slice=[current_step, next_step])
            for obj in objs_with_approves:
                cur_approve = models.CmfApprove.get(obj=obj, order_by=["-cmf_modified_at"])
                cur_approve.is_cur_approve = True
                cur_approve.save(only_data=True)

            cmf_commit()

            current_step = next_step


if __name__ == "__main__":
    setting_current_approves()
