From 054e4dd06b58a27b7ad4279f6c0679471d643803 Mon Sep 17 00:00:00 2001 From: Pranshu Aggarwal <70687348+Pranshu1902@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:13:24 +0530 Subject: [PATCH] Fix the endpoint to view project chats in order (#334) * endpoint to view project chats in order * send username along with chats to admin * refactor * show username in chats list to admin --- ayushma/serializers/chat.py | 7 ++++++- ayushma/views/chat.py | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ayushma/serializers/chat.py b/ayushma/serializers/chat.py index 6bb1ab14..061e04df 100644 --- a/ayushma/serializers/chat.py +++ b/ayushma/serializers/chat.py @@ -7,6 +7,7 @@ class ChatSerializer(serializers.ModelSerializer): project = ProjectSerializer(read_only=True) + username = serializers.CharField(source='user.username', read_only=True) class Meta: model = Chat @@ -15,12 +16,14 @@ class Meta: "title", "created_at", "modified_at", + "username", "project", ) read_only_fields = ( "external_id", "created_at", "modified_at", + "username", "project", ) @@ -109,12 +112,14 @@ class ConverseSerializer(serializers.Serializer): class ChatDetailSerializer(serializers.ModelSerializer): chats = serializers.SerializerMethodField() message = ConverseSerializer(required=False) + username = serializers.CharField(source='user.username', read_only=True) class Meta: model = Chat fields = ( "external_id", "title", + "username", "created_at", "modified_at", "chats", @@ -122,7 +127,7 @@ class Meta: "message", "model", ) - read_only_fields = ("external_id", "created_at", "modified_at", "chats") + read_only_fields = ("external_id", "created_at", "modified_at", "chats", "username") def get_chats(self, obj): chatmessages = ChatMessage.objects.filter(chat=obj).order_by("created_at") diff --git a/ayushma/views/chat.py b/ayushma/views/chat.py index 0006ed68..7b1c3f67 100644 --- a/ayushma/views/chat.py +++ b/ayushma/views/chat.py @@ -58,7 +58,9 @@ def get_parsers(self): def get_queryset(self): user = self.request.user project_id = self.kwargs["project_external_id"] - queryset = self.queryset.filter(project__external_id=project_id) + queryset = self.queryset.filter(project__external_id=project_id).order_by( + "-created_at" + ) if user.is_superuser and self.action == "list_all": return queryset