From 27ef98a2008e17b683afbf98b3d263d11d7d9992 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Sun, 28 Nov 2021 23:36:53 +0100 Subject: Revert "change login api" This reverts commit 2634ae627659b71790147581880529dad741f425. --- chat_api/models.py | 2 +- chat_api/serializers.py | 9 -------- chat_api/urls.py | 5 ++--- chat_api/views.py | 55 ++----------------------------------------------- 4 files changed, 5 insertions(+), 66 deletions(-) (limited to 'chat_api') diff --git a/chat_api/models.py b/chat_api/models.py index 54fb0f3..5076d0b 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, unique=True) + name = models.CharField(max_length=16, null=False) 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 585bb36..50d84e0 100644 --- a/chat_api/serializers.py +++ b/chat_api/serializers.py @@ -3,15 +3,6 @@ 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 617a0f2..9fa6016 100644 --- a/chat_api/urls.py +++ b/chat_api/urls.py @@ -1,7 +1,6 @@ from django.urls import path -from .views import * +from .views import GroupView urlpatterns = [ - path('groups/', GroupView.as_view(), 'groups_list'), - path('groups/detail//', GroupDetailView.as_view(), 'groups_detail'), + path('groups/', GroupView.as_view(), '') ] diff --git a/chat_api/views.py b/chat_api/views.py index eca6d45..f94f3a0 100644 --- a/chat_api/views.py +++ b/chat_api/views.py @@ -5,8 +5,6 @@ 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 @@ -14,17 +12,6 @@ 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, ) @@ -33,47 +20,9 @@ class GroupView(generics.ListCreateAPIView): def get_queryset(self): return Group.objects.filter( Q(owner=self.request.user) | - Q(users__in=(self.request.user.pk, )) + Q(users__in=(self.request.user, )) ).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 - 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) + return super().create(request, *args, **kwargs) \ No newline at end of file -- cgit v1.2.3