<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import zipfile

from modules.logs.log_config import get_logger
from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def patch():
    """
    Р”Р»СЏ С‚РµСЃС‚РёСЂРѕРІР°РЅРёСЏ РїР°С‚С‡Р°: ( cd /opt/crm; python3 -m patch.20220XXXXXXX_PATCHNAME )
    Р—РґРµСЃСЊ РјРѕР¶РЅРѕ СЂР°Р±РѕС‚Р°С‚СЊ СЃ РјРѕРґРµР»СЏРјРё С‡РµСЂРµР· models.CmfTask Рё С‚.Рґ.
    Р”Р»СЏ РїСЂРѕРіСЂРµСЃСЃР±Р°СЂР° РёСЃРїРѕР»СЊР·СѓР№С‚Рµ:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    logger = get_logger('kn_client_delete_attach')
    start = 0
    step = 1000
    total = models.CmfAttachment.count(include_deleted=True)
    logger.info(f'total={total}')
    while True:
        atts = models.CmfAttachment.list(slice=[start, start + step], include_deleted=True,
                                         fields=['name', 'cmf_deleted'])
        for attach in tqdm(atts):
            try:
                if attach.cmf_deleted:
                    continue
                rdisk = models.CmfRFile.dp.data_driver.get_rd()
                rfile = rdisk.get_rfile(attach._file_name)
                if not rfile.exists or not rfile.st_size:
                    logger.info(f'РЈРґР°Р»СЏРµРј {rfile.abspath}')
                    attach.delete()
            except Exception as e:
                logger.exception(f'{attach._file_name}: {e}')
        if not atts:
            break
        start += step
        models.CmfAttachment.dp.commit()


if __name__ == "__main__":
    patch()
</pre></body></html>