From 890f26266917baa82ae9b2582b1bba9a9c139383 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Sun, 28 Nov 2021 22:57:10 +0100 Subject: Add groups list and detail view --- chat_api/models.py | 2 +- chat_api/serializers.py | 9 ++++++++ chat_api/urls.py | 5 +++-- chat_api/views.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/chat_api/models.py b/chat_api/models.py index 5076d0b..54fb0f3 100644 --- a/chat_api/models.py +++ b/chat_api/models.py @@ -12,7 +12,7 @@ class DmMessage(models.Model): return str(self.sender) class Group(models.Model): - name = models.CharField(max_length=16, null=False) + name = models.CharField(max_length=16, null=False, unique=True) owner = models.ForeignKey(User, related_name="owner_group", on_delete=models.CASCADE) users = models.ManyToManyField(User, related_name="users_group") create_date = models.DateTimeField(auto_now=True) diff --git a/chat_api/serializers.py b/chat_api/serializers.py index 50d84e0..585bb36 100644 --- a/chat_api/serializers.py +++ b/chat_api/serializers.py @@ -3,6 +3,15 @@ from rest_framework import serializers from .models import * class GroupSerializer(serializers.ModelSerializer): + is_mine = serializers.SerializerMethodField('_is_mine') + + def _is_mine(self, obj): + request = self.context.get('request') + if request: + return request.user == obj.owner + return False + class Meta: model = Group fields = '__all__' + extra_fields = ('is_mine', ) diff --git a/chat_api/urls.py b/chat_api/urls.py index 9fa6016..617a0f2 100644 --- a/chat_api/urls.py +++ b/chat_api/urls.py @@ -1,6 +1,7 @@ from django.urls import path -from .views import GroupView +from .views import * urlpatterns = [ - path('groups/', GroupView.as_view(), '') + path('groups/', GroupView.as_view(), 'groups_list'), + path('groups/detail//', GroupDetailView.as_view(), 'groups_detail'), ] diff --git a/chat_api/views.py b/chat_api/views.py index f94f3a0..eca6d45 100644 --- a/chat_api/views.py +++ b/chat_api/views.py @@ -5,6 +5,8 @@ from rest_framework.serializers import Serializer from rest_framework.views import APIView from rest_framework import permissions from rest_framework import generics +from rest_framework.views import Http404 +from rest_framework.generics import get_object_or_404 from knox.auth import TokenAuthentication from django.db.models.query import Q @@ -12,6 +14,17 @@ from django.db.models.query import Q from .models import * from .serializers import * + +def set_users(request): + users_list = request.data.get('users', []) + user_pk = request.user.pk + + if isinstance(users_list, list): + if user_pk not in users_list: + users_list.append(user_pk) + + request.data['users'] = users_list + class GroupView(generics.ListCreateAPIView): permission_classes = (permissions.IsAuthenticated, ) authentication_classes = (TokenAuthentication, ) @@ -20,9 +33,47 @@ class GroupView(generics.ListCreateAPIView): def get_queryset(self): return Group.objects.filter( Q(owner=self.request.user) | - Q(users__in=(self.request.user, )) + Q(users__in=(self.request.user.pk, )) ).distinct() + + def get_serializer_context(self): + context = super(GroupView, self).get_serializer_context() + context.update({"request": self.request}) + return context def create(self, request, *args, **kwargs): request.data['owner'] = request.user.pk - return super().create(request, *args, **kwargs) \ No newline at end of file + set_users(request) + return super().create(request, *args, **kwargs) + +class GroupDetailView(generics.RetrieveUpdateDestroyAPIView): + permission_classes = (permissions.IsAuthenticated, ) + authentication_classes = (TokenAuthentication, ) + serializer_class = GroupSerializer + + def get_queryset(self): + return Group.objects.filter( + Q(owner=self.request.user) + ).distinct() + + def get_serializer_context(self): + context = super(GroupDetailView, self).get_serializer_context() + context.update({"request": self.request}) + return context + + def update(self, request, *args, **kwargs): + request.data['owner'] = request.user.pk + set_users(request) + return super().update(request, *args, **kwargs) + + def retrieve(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 + + serializer = GroupSerializer(group, context={"request": self.request}) + return Response(serializer.data) -- cgit v1.2.3