From ff53fd490b02536733f6618908760b9e3d52eeb3 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Mon, 29 Nov 2021 10:50:24 +0100 Subject: add group messages api --- chat_api/admin.py | 4 ++-- chat_api/models.py | 12 ++++++------ chat_api/serializers.py | 38 ++++++++++++++++++++++++++++++++++++++ chat_api/urls.py | 5 +++-- chat_api/views.py | 38 +++++++++++++++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 11 deletions(-) (limited to 'chat_api') diff --git a/chat_api/admin.py b/chat_api/admin.py index 50ad2b0..7aa8358 100644 --- a/chat_api/admin.py +++ b/chat_api/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin from .models import * -admin.site.register(DmMessage) -admin.site.register(GmMessage) +admin.site.register(Dm) +admin.site.register(Gm) admin.site.register(Group) \ No newline at end of file diff --git a/chat_api/models.py b/chat_api/models.py index 54fb0f3..4c370fc 100644 --- a/chat_api/models.py +++ b/chat_api/models.py @@ -2,9 +2,9 @@ from django.db import models from django.contrib.auth.models import User from django.db.models.base import Model -class DmMessage(models.Model): - sender = models.ForeignKey(User, related_name="sender_dm_message", on_delete=models.CASCADE) - reciever = models.ForeignKey(User, related_name="receiver_dm_message", on_delete=models.CASCADE) +class Dm(models.Model): + sender = models.ForeignKey(User, related_name="sender_dm", on_delete=models.CASCADE) + reciever = models.ForeignKey(User, related_name="receiver_dm", on_delete=models.CASCADE) message = models.TextField(null=False) create_date = models.DateTimeField(auto_now=True) @@ -17,9 +17,9 @@ class Group(models.Model): users = models.ManyToManyField(User, related_name="users_group") create_date = models.DateTimeField(auto_now=True) -class GmMessage(models.Model): - sender = models.ForeignKey(User, related_name="sender_gm_message", on_delete=models.CASCADE) - reciever = models.ForeignKey(Group, related_name="receiver_gm_message", on_delete=models.CASCADE) +class Gm(models.Model): + sender = models.ForeignKey(User, related_name="sender_gm", on_delete=models.CASCADE) + reciever = models.ForeignKey(Group, related_name="receiver_gm", on_delete=models.CASCADE) message = models.TextField(null=False) create_date = models.DateTimeField(auto_now=True) diff --git a/chat_api/serializers.py b/chat_api/serializers.py index 585bb36..15828be 100644 --- a/chat_api/serializers.py +++ b/chat_api/serializers.py @@ -15,3 +15,41 @@ class GroupSerializer(serializers.ModelSerializer): model = Group fields = '__all__' extra_fields = ('is_mine', ) + +class DmSerializer(serializers.ModelSerializer): + is_mine = serializers.SerializerMethodField('_is_mine') + + def _is_mine(self, obj): + request = self.context.get('request') + if request: + return request.user == obj.sender + return False + class Meta: + model = Dm + fields = '__all__' + +class GmSerializer(serializers.ModelSerializer): + is_mine = serializers.SerializerMethodField('_is_mine') + + def _is_mine(self, obj): + request = self.context.get('request') + if request: + return request.user == obj.sender + return False + class Meta: + model = Gm + fields = '__all__' + +class GroupMessagesSerializer(serializers.ModelSerializer): + messages = serializers.SerializerMethodField('_messages') + + def _messages(self, obj): + request = self.context.get('request') + if request: + gms = GmSerializer(obj.receiver_gm.all(), many=True, read_only=True, context={"request": request}) + return gms.data + return [] + + class Meta: + model=Group + fields=('messages', ) diff --git a/chat_api/urls.py b/chat_api/urls.py index 617a0f2..a860b5d 100644 --- a/chat_api/urls.py +++ b/chat_api/urls.py @@ -2,6 +2,7 @@ from django.urls import path from .views import * urlpatterns = [ - path('groups/', GroupView.as_view(), 'groups_list'), - path('groups/detail//', GroupDetailView.as_view(), 'groups_detail'), + path('groups/', GroupView.as_view(), name='groups_list'), + path('groups/detail//', GroupDetailView.as_view(), name='groups_detail'), + path('groups/detail//messages/', GmsView.as_view(), name='group_messages'), ] diff --git a/chat_api/views.py b/chat_api/views.py index eca6d45..6d161c1 100644 --- a/chat_api/views.py +++ b/chat_api/views.py @@ -1,7 +1,6 @@ from django.db.models.query import QuerySet from django.http.response import Http404 from rest_framework.response import Response -from rest_framework.serializers import Serializer from rest_framework.views import APIView from rest_framework import permissions from rest_framework import generics @@ -77,3 +76,40 @@ class GroupDetailView(generics.RetrieveUpdateDestroyAPIView): serializer = GroupSerializer(group, context={"request": self.request}) return Response(serializer.data) + +class GmsView(APIView): + permission_classes = (permissions.IsAuthenticated, ) + authentication_classes = (TokenAuthentication, ) + + def get(self, request, pk): + try: + group = Group.objects.filter( + Q(owner=request.user) | + Q(users__in=(request.user.pk, )) + ).distinct().get(Q(pk=pk)) + except Group.DoesNotExist: + raise Http404 + + serialzier = GroupMessagesSerializer( + group, + context={"request": request} + ) + + return Response(serialzier.data) + + def post(self, request, pk): + try: # check is user in group TODO: Make it better + Group.objects.filter( + Q(owner=request.user) | + Q(users__in=(request.user.pk, )) + ).distinct().get(Q(pk=pk)) + except Group.DoesNotExist: + raise Http404 + + request.data['sender'] = request.user.pk + request.data['reciever'] = pk + serializer = GmSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + serializer.save() + + return Response(serializer.data) \ No newline at end of file -- cgit v1.2.3