From e141a4e0f5144b4c20f25cd7bb93ef12508549e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=9D=D0=B0=D0=B4=D0=B5?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Wed, 3 Dec 2025 15:25:58 +0300 Subject: [PATCH] =?UTF-8?q?communications:=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=20API=20endpoint=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=87?= =?UTF-8?q?=D0=B0=D1=82=D0=B0,=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20JS=20=D0=B4=D0=BB=D1=8F=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20URL,=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B3=D0=BB=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D1=85/=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=BF=D0=BF=D0=BE=D0=B2=D1=8B=D1=85/=D0=BA=D0=B0=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D1=87=D0=B0=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D0=BC=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api/v1/communications/views.py | 85 +++++++++++++++++++ backend/api/v1/urls.py | 6 ++ .../templates/communications/chat_list.html | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/backend/api/v1/communications/views.py b/backend/api/v1/communications/views.py index d686658d..015a0228 100644 --- a/backend/api/v1/communications/views.py +++ b/backend/api/v1/communications/views.py @@ -981,3 +981,88 @@ def delete_message(request, message_id): }) +@csrf_protect +@login_required +@require_POST +def create_chat(request): + """Создание нового чата (группового, канала, объявлений и т.д.)""" + user = request.user + + # Получаем параметры + chat_type = request.POST.get('type', 'group') + name = request.POST.get('name', '').strip() + description = request.POST.get('description', '').strip() + department_id = request.POST.get('department_id') + include_all = request.POST.get('include_all_employees') == 'true' + is_main = request.POST.get('is_main') == 'true' + participant_ids = request.POST.getlist('participant_ids') + avatar_file = request.FILES.get('avatar') + + # Валидация + if not name: + return JsonResponse({'error': 'Название чата обязательно'}, status=400) + + # Проверка на существующий чат объявлений + if chat_type == 'announcement': + existing = Chat.objects.filter( + type='announcement', + created_by=user + ).first() + + if existing: + return JsonResponse({ + 'error': 'У вас уже есть чат объявлений', + 'existing_chat_id': existing.id + }, status=400) + + try: + with transaction.atomic(): + # Создаём чат + chat = Chat.objects.create( + type=chat_type, + name=name, + description=description, + created_by=user, + include_all_employees=include_all, + is_main=is_main + ) + + # Аватар + if avatar_file: + chat.avatar = avatar_file + chat.save() + + # Привязка к отделу + if chat_type == 'department' and department_id: + from employees.models import Department + try: + dept = Department.objects.get(id=department_id) + chat.department = dept + chat.save() + except Department.DoesNotExist: + pass + + # Добавляем участников для группового чата + if chat_type == 'group' and participant_ids: + from employees.models import Employee + participants = Employee.objects.filter(id__in=participant_ids) + chat.participants.add(*participants) + # Добавляем создателя + chat.participants.add(user) + + # Для канала/объявлений добавляем создателя как участника + if chat_type in ('channel', 'announcement'): + chat.participants.add(user) + + return JsonResponse({ + 'ok': True, + 'chat_id': chat.id, + 'message': 'Чат успешно создан' + }) + + except Exception as e: + return JsonResponse({ + 'error': f'Ошибка при создании чата: {str(e)}' + }, status=500) + + diff --git a/backend/api/v1/urls.py b/backend/api/v1/urls.py index 27db644a..d9aeefd2 100644 --- a/backend/api/v1/urls.py +++ b/backend/api/v1/urls.py @@ -13,6 +13,7 @@ from .requests_app.views import RequestViewSet from .communications.views import ( add_reaction, bulk_delete_messages, + create_chat, delete_message, edit_message, forward_messages, @@ -77,6 +78,11 @@ urlpatterns = [ get_user_chats, name="user_chats" ), + path( + "communications/chats/create/", + create_chat, + name="create_chat" + ), path( "communications/upload-message/", upload_message_with_attachments, diff --git a/backend/templates/communications/chat_list.html b/backend/templates/communications/chat_list.html index ea688181..b5e2db79 100644 --- a/backend/templates/communications/chat_list.html +++ b/backend/templates/communications/chat_list.html @@ -533,7 +533,7 @@ }); } - const res = await fetch('/communications/api/chat/create/', { + const res = await fetch('/api/v1/communications/chats/create/', { method: 'POST', headers: { 'X-CSRFToken': csrftoken