from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def patch():
    """
    Здесь можно работать с моделями через models.CmfTask и т.д.
    Для прогрессбара используйте:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    local_persons = models.CmfPerson.slist(filter=['user_local', '==', True])
    for comment in tqdm(models.CmfComment.list(filter=[['parent_id', 'LIKE', 'CmfChatTopic%'],
                                                       ['cmf_created_at', '>', datetime.datetime.now() - datetime.timedelta(days=60)]],
                                               fields=['views', 'chat_topic_unread_comments'])):
        if comment.chat_topic_unread_comments:
            continue
        for person in local_persons:
            if person.code not in comment.views.json:
                comment.views.value.append({'datetime': datetime.datetime.now(tz=datetime.timezone.utc).isoformat(),
                                            'code': person.code,
                                            'name': person.name
                                            })
                comment.save(only_data=True)


if __name__ == "__main__":
    patch()
