-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8cfed8
commit 6439b92
Showing
4 changed files
with
87 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<int:pk>/", views.ChatViewSet.as_view(), name="chat"), | ||
path("messages/<int:pk>/", 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/<int:pk>/", views.ChatDetailView.as_view(), name="chat-detail"), | ||
path( | ||
"chats/<int:chat_id>/messages/", | ||
views.MessageListView.as_view(), | ||
name="messages-list", | ||
), | ||
path( | ||
"chats/<int:chat_id>/messages/create/", | ||
views.MessageCreateView.as_view(), | ||
name="message-create", | ||
), | ||
path( | ||
"chats/<int:chat_id>/messages/<int:pk>/", | ||
views.MessageDetailView.as_view(), | ||
name="message-detail", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]) |