Игорь Надеин fa0948c159 feat(communications): добавлена поддержка редактирования сообщений с изменением attachments
Изменения:
- Новый endpoint /api/v1/communications/messages/upload-temp/ для загрузки файлов без создания сообщения
- Поле message в MessageAttachment теперь nullable для временных вложений
- Логика перемещения attachments между сообщениями при редактировании
- Frontend использует upload-temp вместо создания временных сообщений
- Celery задача cleanup_orphaned_attachments для очистки забытых файлов (каждый час)
- Исправлена проблема с WebSocket обновлениями при редактировании
- Удалены debug логи после успешного тестирования

Технические детали:
- communications/models.py: добавлено null=True, blank=True к MessageAttachment.message
- communications/migrations/0022: миграция для изменения поля
- api/v1/communications/viewsets.py: новый action upload_temp() + логика перемещения в update()
- static/js/components/chatComposer.js: использование upload-temp вместо upload
- communications/tasks.py: новая Celery задача для очистки старых orphaned attachments
- eusrr_backend/settings.py: добавлен CELERY_BEAT_SCHEDULE с задачей очистки
2026-01-15 11:13:54 +03:00

44 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Celery задачи для модуля communications
"""
from celery import shared_task
from django.utils import timezone
from datetime import timedelta
import logging
logger = logging.getLogger(__name__)
@shared_task
def cleanup_orphaned_attachments():
"""
Очистка временных вложений без привязки к сообщению.
Удаляет MessageAttachment с message=null старше 1 часа.
"""
from communications.models import MessageAttachment
cutoff_time = timezone.now() - timedelta(hours=1)
orphaned = MessageAttachment.objects.filter(
message__isnull=True,
created_at__lt=cutoff_time
)
count = orphaned.count()
if count > 0:
logger.info(f'[cleanup_orphaned_attachments] Found {count} orphaned attachments')
# Удаляем файлы и записи
for attachment in orphaned:
try:
if attachment.file:
attachment.file.delete()
attachment.delete()
except Exception as e:
logger.error(f'[cleanup_orphaned_attachments] Error deleting attachment {attachment.id}: {e}')
logger.info(f'[cleanup_orphaned_attachments] Cleaned up {count} orphaned attachments')
return count