lizerk f56e256a2a feat(communications): Phase 2 - Update views, signals, rules for GenericFK
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
2026-03-11 14:30:23 +03:00

38 lines
1.6 KiB
Python

# backend\communications\apps.py
from django.apps import AppConfig
class CommunicationsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "communications"
def ready(self):
import communications.signals
import communications.notifications # Модуль уведомлений (signals регистрируются автоматически)
import communications.rules # django-rules: регистрация предикатов и правил доступа
def create_main_global_chat(sender, **kwargs):
"""
Создает главный глобальный чат при первом запуске.
Использует flags['is_primary'] вместо is_main.
Для обратной совместимости также устанавливает is_main=True.
"""
from communications.models import Chat
from django.db.models import Q
# Проверяем по обоим полям (старому и новому)
if not Chat.objects.filter(
Q(type="global") & (
Q(is_main=True) | Q(flags__is_primary=True)
)
).exists():
Chat.objects.create(
type="global",
# NEW: flags
flags={'is_primary': True},
# DEPRECATED: для обратной совместимости
is_main=True
)