Corp/backend/test_announcement_notifications.py
Игорь Надеин 413c2c25b4 Исправлена отправка уведомлений для объявлений всем участникам
Проблема:
- get_participants для announcement возвращал none()
- notification_signals использовал participants вместо get_participants
- Уведомления не создавались для объявлений с include_all_employees

Решение:
- get_participants для announcement/channel теперь возвращает всех
  активных сотрудников если include_all_employees=True
- notification_signals использует get_participants для
  announcement/channel/department/global типов
- Добавлены тестовые скрипты для проверки

Тестирование:
✅ Уведомления создаются для всех участников (кроме автора)
✅ Тип уведомления: announcement_new_message
✅ Приоритет: high, каналы: web + telegram
2025-11-30 12:25:19 +03:00

85 lines
3.4 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.

#!/usr/bin/env python
"""
Тестирование уведомлений для объявлений
"""
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'eusrr_backend.settings')
django.setup()
from communications.models import Chat, Message
from employees.models import Employee
def test_announcement_notifications():
"""Проверяет, что уведомления для объявлений отправляются всем"""
# Находим чат-объявление
announcement = Chat.objects.filter(type='announcement').first()
if not announcement:
print("❌ Нет объявлений в системе")
return
print(f"✓ Найдено объявление: {announcement.name}")
print(f" Created by: {announcement.created_by}")
print(f" include_all_employees: {announcement.include_all_employees}")
# Получаем участников
participants = announcement.get_participants
print(f" Участников (get_participants): {participants.count()}")
# Список участников
for p in participants[:5]:
print(f" - {p.get_full_name() or p.username} ({p.email})")
if participants.count() > 5:
print(f" ... и ещё {participants.count() - 5}")
# Проверяем последнее сообщение
last_message = announcement.messages.order_by('-created_at').first()
if last_message:
print(f"\n✓ Последнее сообщение:")
print(f" ID: {last_message.id}")
print(f" Автор: {last_message.author}")
print(f" Текст: {last_message.content[:50]}...")
print(f" Создано: {last_message.created_at}")
# Проверяем уведомления
from notifications.models import Notification
notifications = Notification.objects.filter(
content_type__model='message',
object_id=last_message.id
)
print(f"\n✓ Уведомлений создано: {notifications.count()}")
# Проверяем тип уведомлений
announcement_notifs = notifications.filter(
notification_type__code='announcement_new_message'
)
print(f" Типа 'announcement_new_message': {announcement_notifs.count()}")
# Показываем первые 5
for n in notifications[:5]:
print(f" - Для: {n.recipient.get_full_name()}")
print(f" Тип: {n.notification_type.code}")
print(f" Прочитано: {n.is_read}")
else:
print("\n❌ Нет сообщений в объявлении")
# Итоги
print("\n" + "="*60)
all_employees = Employee.objects.filter(is_active=True).count()
print(f"Всего активных сотрудников: {all_employees}")
print(f"Участников объявления: {participants.count()}")
if participants.count() == all_employees:
print("✅ ПРАВИЛЬНО: Все сотрудники получат уведомления")
else:
print(f"⚠️ ВНИМАНИЕ: Уведомления получат только {participants.count()} из {all_employees}")
if __name__ == '__main__':
test_announcement_notifications()