From 6439b9236f97a8af4c4260d4ea63cee9d126ae71 Mon Sep 17 00:00:00 2001 From: Shavkatjon Odamboyev <153182519+Shavkatjon-O@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:11:41 +0500 Subject: [PATCH] fix: backend --- apps/chats/models.py | 2 -- apps/chats/serializers.py | 24 ++++++++++++--- apps/chats/urls.py | 35 +++++++++++++--------- apps/chats/views.py | 62 +++++++++++++++++++++++++++++---------- 4 files changed, 87 insertions(+), 36 deletions(-) diff --git a/apps/chats/models.py b/apps/chats/models.py index c2695c3..72c6d9c 100644 --- a/apps/chats/models.py +++ b/apps/chats/models.py @@ -1,6 +1,5 @@ from django.db import models from django.contrib.auth import get_user_model - from apps.common.models import BaseModel User = get_user_model() @@ -18,7 +17,6 @@ def __str__(self): class Message(BaseModel): chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name="messages") user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="messages") - content = models.TextField() def __str__(self): diff --git a/apps/chats/serializers.py b/apps/chats/serializers.py index 37b6b07..1b739c5 100644 --- a/apps/chats/serializers.py +++ b/apps/chats/serializers.py @@ -1,24 +1,40 @@ from rest_framework import serializers from .models import Chat, Message +from django.contrib.auth import get_user_model + +User = get_user_model() class MessageSerializer(serializers.ModelSerializer): + user = serializers.StringRelatedField() + class Meta: model = Message - fields = [ + fields = ( "id", "chat", "user", "content", - ] + ) class ChatSerializer(serializers.ModelSerializer): + users = serializers.StringRelatedField(many=True) + class Meta: model = Chat - fields = [ + fields = ( "id", "title", "is_group", "users", - ] + ) + + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ( + "id", + "email", + ) diff --git a/apps/chats/urls.py b/apps/chats/urls.py index 63afae0..43c0cf5 100644 --- a/apps/chats/urls.py +++ b/apps/chats/urls.py @@ -1,17 +1,24 @@ -from django.urls import path, include -from rest_framework.routers import DefaultRouter -from . import views - -# router = DefaultRouter() -# router.register(r"chats", views.ChatViewSet) -# router.register(r"messages", views.MessageViewSet) - -# urlpatterns = [ -# path("", include(router.urls)), -# ] - +from django.urls import path +from apps.chats import views urlpatterns = [ - path("chats//", views.ChatViewSet.as_view(), name="chat"), - path("messages//", views.MessageViewSet.as_view(), name="message"), + path("users/", views.UserListView.as_view(), name="users-list"), + path("chats/", views.ChatListView.as_view(), name="chats-list"), + path("chats/create/", views.ChatCreateView.as_view(), name="chat-create"), + path("chats//", views.ChatDetailView.as_view(), name="chat-detail"), + path( + "chats//messages/", + views.MessageListView.as_view(), + name="messages-list", + ), + path( + "chats//messages/create/", + views.MessageCreateView.as_view(), + name="message-create", + ), + path( + "chats//messages//", + views.MessageDetailView.as_view(), + name="message-detail", + ), ] diff --git a/apps/chats/views.py b/apps/chats/views.py index 940de11..9143960 100644 --- a/apps/chats/views.py +++ b/apps/chats/views.py @@ -1,25 +1,55 @@ -from rest_framework import viewsets -from .models import Chat, Message -from .serializers import ChatSerializer, MessageSerializer -from rest_framework.generics import ListAPIView, RetrieveAPIView +from rest_framework import generics +from apps.chats import models, serializers -# class ChatViewSet(RetrieveAPIView): -# queryset = Chat.objects.all() -# serializer_class = ChatSerializer +class UserListView(generics.ListAPIView): + queryset = models.User.objects.all() + serializer_class = serializers.UserSerializer -class ChatViewSet(RetrieveAPIView): - queryset = Chat.objects.all() - serializer_class = ChatSerializer +class ChatListView(generics.ListAPIView): + queryset = models.Chat.objects.all() + serializer_class = serializers.ChatSerializer + def get_queryset(self): + return self.queryset.filter(users=self.request.user) + + +class ChatCreateView(generics.CreateAPIView): + queryset = models.Chat.objects.all() + serializer_class = serializers.ChatSerializer + + def perform_create(self, serializer): + serializer.save(users=[self.request.user]) -class MessageViewSet(ListAPIView): - queryset = Message.objects.all() - serializer_class = MessageSerializer + +class ChatDetailView(generics.RetrieveUpdateDestroyAPIView): + queryset = models.Chat.objects.all() + serializer_class = serializers.ChatSerializer def get_queryset(self): - chat_id = self.kwargs["chat_id"] + return self.queryset.filter(users=self.request.user) + - print(self.queryset.filter(chat__id=chat_id)) - return self.queryset.filter(chat__id=chat_id) +class MessageListView(generics.ListAPIView): + queryset = models.Message.objects.all() + serializer_class = serializers.MessageSerializer + + def get_queryset(self): + return self.queryset.filter(chat=self.kwargs["chat_id"]) + + +class MessageCreateView(generics.CreateAPIView): + queryset = models.Message.objects.all() + serializer_class = serializers.MessageSerializer + + def perform_create(self, serializer): + serializer.save(user=self.request.user) + + +class MessageDetailView(generics.RetrieveUpdateDestroyAPIView): + queryset = models.Message.objects.all() + serializer_class = serializers.MessageSerializer + + def get_queryset(self): + return self.queryset.filter(chat=self.kwargs["chat_id"])