from patch.include import *
from tqdm import tqdm
import mimetypes


@app_context(commit=True)
def fill_field_embedded_cmf_attachment():
    """
    Для тестирования патча: ( cd /opt/eva-app; python3 -m patch.202409131629_fill_field_embedded_cmf_attachment )
    Здесь можно работать с моделями через models.CmfTask и т.д.
    Для прогрессбара используйте:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    print('Запуск патча fill_field_embedded_cmf_attachment')

    while True:
        attachments = models.CmfAttachment.list(slice=[0, 200],
                                                fields='embedded',
                                                filter=[
                                                        ['embedded', '!=', True],
                                                        ['OR',
                                                        ['name', 'LIKE', 'image-mce%'],
                                                        ['name', 'LIKE', 'comm-image-mce%']
                                                        ]])
        if not attachments:
             break
        for attachment in attachments:
                attachment.embedded = True
                attachment.save(only_data=True)

if __name__ == "__main__":
    fill_field_embedded_cmf_attachment()
