Skip to content

Commit

Permalink
fix: messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Shavkatjon-O committed Aug 30, 2024
1 parent f4d17f7 commit 13a7628
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 80 deletions.
11 changes: 5 additions & 6 deletions apps/chats/consumers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# consumers.py
from channels.generic.websocket import AsyncWebsocketConsumer
import json
from channels.generic.websocket import AsyncWebsocketConsumer


class ChatConsumer(AsyncWebsocketConsumer):
Expand All @@ -18,15 +17,15 @@ async def disconnect(self, close_code):
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
user = self.scope["user"].username
sender = text_data_json["sender"]

await self.channel_layer.group_send(
self.room_group_name,
{"type": "chat_message", "message": message, "user": user},
{"type": "chat_message", "message": message, "sender": sender},
)

async def chat_message(self, event):
message = event["message"]
user = event["user"]
sender = event["sender"]

await self.send(text_data=json.dumps({"message": message, "user": user}))
await self.send(text_data=json.dumps({"message": message, "sender": sender}))
5 changes: 2 additions & 3 deletions apps/chats/routing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# routing.py
from django.urls import re_path
from django.urls import path
from . import consumers

websocket_urlpatterns = [
re_path(r"ws/chats/(?P<chat_id>\d+)/$", consumers.ChatConsumer.as_asgi()),
path("ws/chat/<int:chat_id>/", consumers.ChatConsumer.as_asgi()),
]
4 changes: 2 additions & 2 deletions apps/chats/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Meta:


class MessageSerializer(serializers.ModelSerializer):
user = serializers.StringRelatedField()
user = UserSerializer()

class Meta:
model = Message
Expand All @@ -32,8 +32,8 @@ class Meta:


class ChatSerializer(serializers.ModelSerializer):
users = UserSerializer(many=True)
messages = MessageSerializer(many=True, read_only=True)
users = UserSerializer(many=True)

class Meta:
model = Chat
Expand Down
42 changes: 29 additions & 13 deletions apps/chats/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,39 @@
# ]

# urls.py
# from django.urls import path
# from .views import (
# ChatListCreateView,
# ChatDetailView,
# MessageListCreateView,
# MessageDetailView,
# UserListView,
# )

# urlpatterns = [
# path("", ChatListCreateView.as_view(), name="chat-list-create"),
# path("<int:pk>/", ChatDetailView.as_view(), name="chat-detail"),
# path(
# "<int:chat_id>/messages/",
# MessageListCreateView.as_view(),
# name="message-list-create",
# ),
# path("messages/<int:pk>/", MessageDetailView.as_view(), name="message-detail"),
# path("users/", UserListView.as_view(), name="user-list"),
# ]

from django.urls import path
from .views import (

from apps.chats.views import (
ChatListCreateView,
ChatDetailView,
ChatRetrieveUpdateDestroyView,
MessageListCreateView,
MessageDetailView,
UserListView,
MessageRetrieveUpdateDestroyView,
)

urlpatterns = [
path("", ChatListCreateView.as_view(), name="chat-list-create"),
path("<int:pk>/", ChatDetailView.as_view(), name="chat-detail"),
path(
"<int:chat_id>/messages/",
MessageListCreateView.as_view(),
name="message-list-create",
),
path("messages/<int:pk>/", MessageDetailView.as_view(), name="message-detail"),
path("users/", UserListView.as_view(), name="user-list"),
path("chats/", ChatListCreateView.as_view()),
path("chats/<int:pk>/", ChatRetrieveUpdateDestroyView.as_view()),
path("messages/", MessageListCreateView.as_view()),
path("messages/<int:pk>/", MessageRetrieveUpdateDestroyView.as_view()),
]
45 changes: 38 additions & 7 deletions apps/chats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,61 @@
from apps.chats.serializers import ChatSerializer, MessageSerializer, UserSerializer


class UserListView(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
# class UserListView(generics.ListAPIView):
# queryset = User.objects.all()
# serializer_class = UserSerializer


# class ChatListCreateView(generics.ListCreateAPIView):
# queryset = Chat.objects.all()
# serializer_class = ChatSerializer


# class ChatDetailView(generics.RetrieveUpdateDestroyAPIView):
# queryset = Chat.objects.all()
# serializer_class = ChatSerializer


# class MessageListCreateView(generics.ListCreateAPIView):
# queryset = Message.objects.all()
# serializer_class = MessageSerializer

# def get_queryset(self):
# chat_id = self.kwargs.get("chat_id")
# return Message.objects.filter(chat_id=chat_id)


# class MessageDetailView(generics.RetrieveUpdateDestroyAPIView):
# queryset = Message.objects.all()
# serializer_class = MessageSerializer


class ChatListCreateView(generics.ListCreateAPIView):
queryset = Chat.objects.all()
serializer_class = ChatSerializer

def get_queryset(self):
return Chat.objects.filter(users=self.request.user)


class ChatDetailView(generics.RetrieveUpdateDestroyAPIView):
class ChatRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Chat.objects.all()
serializer_class = ChatSerializer


class MessageListCreateView(generics.ListCreateAPIView):
queryset = Message.objects.all()
serializer_class = MessageSerializer

def get_queryset(self):
chat_id = self.kwargs.get("chat_id")
return Message.objects.filter(chat_id=chat_id)
if chat_id:
return Message.objects.filter(chat_id=chat_id)
return Message.objects.all()

def perform_create(self, serializer):
serializer.save(user=self.request.user)


class MessageDetailView(generics.RetrieveUpdateDestroyAPIView):
class MessageRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Message.objects.all()
serializer_class = MessageSerializer
53 changes: 4 additions & 49 deletions core/asgi.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,22 @@
# # """
# # ASGI config for core project.

# # It exposes the ASGI callable as a module-level variable named ``application``.

# # For more information on this file, see
# # https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
# # """

# # import os

# # from django.core.asgi import get_asgi_application

# # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

# # application = get_asgi_application()


# import os
# import django

# django.setup()
# from django.core.asgi import get_asgi_application
# from channels.routing import ProtocolTypeRouter, URLRouter
# from channels.auth import AuthMiddlewareStack
# from apps.chats.consumers import ChatConsumer
# from django.urls import re_path

# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")

# application = ProtocolTypeRouter(
# {
# "http": get_asgi_application(),
# "websocket": AuthMiddlewareStack(
# URLRouter(
# [
# re_path(r"ws/chat/(?P<chat_id>\d+)/$", ChatConsumer.as_asgi()),
# ]
# )
# ),
# }
# )


# asgi.py
import django

django.setup()

import os
import environ

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from apps.chats import routing

import environ

env = environ.Env()
env.read_env(".env")

# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(URLRouter(routing.websocket_urlpatterns)),
"websocket": URLRouter(routing.websocket_urlpatterns),
}
)

0 comments on commit 13a7628

Please sign in to comment.