BREAKING CHANGES (backward compatible): - All code updated to support both old and new fields - context_object (GenericFK) and flags now primary - department and is_main still work (DEPRECATED) FILES UPDATED: 1. signals.py: - create_main_department_chat() uses context_object + flags - Creates both old and new fields for compatibility - Checks existence using Q() for both fields 2. views.py (4 critical updates): - has_user_access_to_chat(): uses get_participants() (supports both) - ChatListView.get_queryset(): checks department OR context_object - ChatDetailView.get_queryset(): checks department OR context_object - mark_as_read_ajax(): uses get_participants() and include_all_users 3. rules.py: - is_department_chat(): checks both department and context_object - Supports GenericFK for Department context 4. apps.py: - create_main_global_chat(): checks and sets flags['is_primary'] - Backward compatible with is_main 5. admin.py: - get_readonly_fields(): checks flags['is_primary'] OR is_main - Prevents editing primary chats 6. notifications/config.py: - get_chat_display_name(): uses context_object OR department 7. models.py: - Chat.__str__(): uses context_object OR department TESTED: ✅ Signal creates chat with context_object + flags ✅ get_participants() works with both fields ✅ __str__ displays correctly ✅ is_department_chat rule works ✅ Backward compatibility preserved
161 lines
5.2 KiB
Python
161 lines
5.2 KiB
Python
"""
|
||
Конфигурация уведомлений для модуля Communications.
|
||
|
||
Содержит:
|
||
- NotificationVerbs - глаголы для уведомлений
|
||
- MessageTemplates - шаблоны сообщений
|
||
- ActionURLs - URL-адреса действий
|
||
- Вспомогательные функции для обработки сообщений
|
||
"""
|
||
|
||
import re
|
||
|
||
|
||
class NotificationVerbs:
|
||
"""Глаголы (verbs) для уведомлений communications."""
|
||
|
||
NEW_MESSAGE = 'chat_new_message'
|
||
ANNOUNCEMENT = 'announcement_new_message'
|
||
MENTION = 'chat_mention'
|
||
REPLY = 'chat_reply'
|
||
ADDED_TO_CHAT = 'chat_added_to_chat'
|
||
|
||
|
||
class MessageTemplates:
|
||
"""Шаблоны сообщений для уведомлений communications."""
|
||
|
||
@staticmethod
|
||
def mention(author_name: str) -> str:
|
||
"""Шаблон для упоминания пользователя."""
|
||
return f'Вас упомянул {author_name}'
|
||
|
||
@staticmethod
|
||
def reply(author_name: str) -> str:
|
||
"""Шаблон для ответа на сообщение."""
|
||
return f'{author_name} ответил на ваше сообщение'
|
||
|
||
@staticmethod
|
||
def announcement(author_name: str) -> str:
|
||
"""Шаблон для нового объявления."""
|
||
return f'Новое объявление от {author_name}'
|
||
|
||
@staticmethod
|
||
def private_message(author_name: str) -> str:
|
||
"""Шаблон для приватного сообщения."""
|
||
return f'Новое сообщение от {author_name}'
|
||
|
||
@staticmethod
|
||
def group_message(author_name: str, chat_name: str) -> str:
|
||
"""Шаблон для сообщения в группе."""
|
||
return f'{author_name} в {chat_name}'
|
||
|
||
@staticmethod
|
||
def added_to_chat(chat_name: str) -> str:
|
||
"""Шаблон для добавления в чат."""
|
||
return f'Вы были добавлены в чат "{chat_name}"'
|
||
|
||
@staticmethod
|
||
def added_to_chat_title() -> str:
|
||
"""Заголовок для уведомления о добавлении в чат."""
|
||
return 'Вас добавили в чат'
|
||
|
||
|
||
class ActionURLs:
|
||
"""URL-адреса для действий с сообщениями."""
|
||
|
||
MESSAGES = '/messages'
|
||
|
||
@staticmethod
|
||
def chat_detail(chat_id: int) -> str:
|
||
"""
|
||
Возвращает URL-адрес чата.
|
||
"""
|
||
return f'/messages/{chat_id}'
|
||
|
||
@staticmethod
|
||
def message_detail(chat_id: int, message_id: int) -> str:
|
||
"""
|
||
Возвращает URL-адрес сообщения.
|
||
|
||
В будущем можно добавить прокрутку к конкретному сообщению.
|
||
"""
|
||
return f'/messages/{chat_id}?message={message_id}'
|
||
|
||
|
||
# ===== Вспомогательные функции =====
|
||
|
||
def extract_mentions(text: str) -> list[str]:
|
||
"""
|
||
Извлекает email'ы из упоминаний вида @email.
|
||
|
||
Args:
|
||
text: Текст сообщения
|
||
|
||
Returns:
|
||
list: Список email'ов без символа @
|
||
|
||
Examples:
|
||
>>> extract_mentions("Привет @user@example.com и @test@mail.com")
|
||
['user@example.com', 'test@mail.com']
|
||
"""
|
||
if not text:
|
||
return []
|
||
|
||
# Паттерн: @ + email формат
|
||
pattern = r'@([\w.+-]+@[\w.-]+\.[\w]+)'
|
||
mentions = re.findall(pattern, text)
|
||
|
||
return list(set(mentions)) # Уникальные значения
|
||
|
||
|
||
def truncate_message(text: str, max_length: int = 100) -> str:
|
||
"""
|
||
Обрезает текст сообщения до указанной длины.
|
||
|
||
Args:
|
||
text: Исходный текст
|
||
max_length: Максимальная длина (по умолчанию 100)
|
||
|
||
Returns:
|
||
Обрезанный текст с многоточием если нужно
|
||
"""
|
||
if not text:
|
||
return ''
|
||
|
||
if len(text) <= max_length:
|
||
return text
|
||
|
||
return text[:max_length - 3] + '...'
|
||
|
||
|
||
def get_chat_name(chat) -> str:
|
||
"""
|
||
Возвращает название чата в зависимости от типа.
|
||
|
||
Args:
|
||
chat: Объект Chat
|
||
|
||
Returns:
|
||
str: Название чата
|
||
"""
|
||
if chat.name:
|
||
return chat.name
|
||
|
||
if chat.type == 'global':
|
||
return 'Глобальный чат'
|
||
|
||
if chat.type == 'department':
|
||
# Проверяем context_object (GenericFK) или старое поле department
|
||
dept = chat.context_object if hasattr(chat, 'context_object') and chat.context_object else getattr(chat, 'department', None)
|
||
if dept:
|
||
return f'Чат отдела: {dept.name}'
|
||
|
||
if chat.type == 'private':
|
||
# Для приватного чата можно вернуть имена участников
|
||
participants = chat.participants.all()[:2]
|
||
if participants:
|
||
names = [p.get_full_name() or p.username for p in participants]
|
||
return ', '.join(names)
|
||
|
||
return 'Чат'
|