summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2021-11-28 23:36:53 +0100
committerjdlugosz963 <jdlugosz963@gmail.com>2021-11-28 23:36:53 +0100
commit27ef98a2008e17b683afbf98b3d263d11d7d9992 (patch)
tree161c53033c08f9f0bb499dd3c1f394a0f508cf4e
parent9e237564e417b7915aa47d95bfb9e3b23391114d (diff)
downloadlom_api-27ef98a2008e17b683afbf98b3d263d11d7d9992.tar.gz
lom_api-27ef98a2008e17b683afbf98b3d263d11d7d9992.zip
Revert "change login api"
This reverts commit 2634ae627659b71790147581880529dad741f425.
-rw-r--r--chat_api/models.py2
-rw-r--r--chat_api/serializers.py9
-rw-r--r--chat_api/urls.py5
-rw-r--r--chat_api/views.py55
4 files changed, 5 insertions, 66 deletions
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):
12 return str(self.sender) 12 return str(self.sender)
13 13
14class Group(models.Model): 14class Group(models.Model):
15 name = models.CharField(max_length=16, null=False, unique=True) 15 name = models.CharField(max_length=16, null=False)
16 owner = models.ForeignKey(User, related_name="owner_group", on_delete=models.CASCADE) 16 owner = models.ForeignKey(User, related_name="owner_group", on_delete=models.CASCADE)
17 users = models.ManyToManyField(User, related_name="users_group") 17 users = models.ManyToManyField(User, related_name="users_group")
18 create_date = models.DateTimeField(auto_now=True) 18 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
3from .models import * 3from .models import *
4 4
5class GroupSerializer(serializers.ModelSerializer): 5class GroupSerializer(serializers.ModelSerializer):
6 is_mine = serializers.SerializerMethodField('_is_mine')
7
8 def _is_mine(self, obj):
9 request = self.context.get('request')
10 if request:
11 return request.user == obj.owner
12 return False
13
14 class Meta: 6 class Meta:
15 model = Group 7 model = Group
16 fields = '__all__' 8 fields = '__all__'
17 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 @@
1from django.urls import path 1from django.urls import path
2from .views import * 2from .views import GroupView
3 3
4urlpatterns = [ 4urlpatterns = [
5 path('groups/', GroupView.as_view(), 'groups_list'), 5 path('groups/', GroupView.as_view(), '')
6 path('groups/detail/<int:pk>/', GroupDetailView.as_view(), 'groups_detail'),
7] 6]
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
5from rest_framework.views import APIView 5from rest_framework.views import APIView
6from rest_framework import permissions 6from rest_framework import permissions
7from rest_framework import generics 7from rest_framework import generics
8from rest_framework.views import Http404
9from rest_framework.generics import get_object_or_404
10from knox.auth import TokenAuthentication 8from knox.auth import TokenAuthentication
11 9
12from django.db.models.query import Q 10from django.db.models.query import Q
@@ -14,17 +12,6 @@ from django.db.models.query import Q
14from .models import * 12from .models import *
15from .serializers import * 13from .serializers import *
16 14
17
18def set_users(request):
19 users_list = request.data.get('users', [])
20 user_pk = request.user.pk
21
22 if isinstance(users_list, list):
23 if user_pk not in users_list:
24 users_list.append(user_pk)
25
26 request.data['users'] = users_list
27
28class GroupView(generics.ListCreateAPIView): 15class GroupView(generics.ListCreateAPIView):
29 permission_classes = (permissions.IsAuthenticated, ) 16 permission_classes = (permissions.IsAuthenticated, )
30 authentication_classes = (TokenAuthentication, ) 17 authentication_classes = (TokenAuthentication, )
@@ -33,47 +20,9 @@ class GroupView(generics.ListCreateAPIView):
33 def get_queryset(self): 20 def get_queryset(self):
34 return Group.objects.filter( 21 return Group.objects.filter(
35 Q(owner=self.request.user) | 22 Q(owner=self.request.user) |
36 Q(users__in=(self.request.user.pk, )) 23 Q(users__in=(self.request.user, ))
37 ).distinct() 24 ).distinct()
38
39 def get_serializer_context(self):
40 context = super(GroupView, self).get_serializer_context()
41 context.update({"request": self.request})
42 return context
43 25
44 def create(self, request, *args, **kwargs): 26 def create(self, request, *args, **kwargs):
45 request.data['owner'] = request.user.pk 27 request.data['owner'] = request.user.pk
46 set_users(request) 28 return super().create(request, *args, **kwargs) \ No newline at end of file
47 return super().create(request, *args, **kwargs)
48
49class GroupDetailView(generics.RetrieveUpdateDestroyAPIView):
50 permission_classes = (permissions.IsAuthenticated, )
51 authentication_classes = (TokenAuthentication, )
52 serializer_class = GroupSerializer
53
54 def get_queryset(self):
55 return Group.objects.filter(
56 Q(owner=self.request.user)
57 ).distinct()
58
59 def get_serializer_context(self):
60 context = super(GroupDetailView, self).get_serializer_context()
61 context.update({"request": self.request})
62 return context
63
64 def update(self, request, *args, **kwargs):
65 request.data['owner'] = request.user.pk
66 set_users(request)
67 return super().update(request, *args, **kwargs)
68
69 def retrieve(self, request, pk):
70 try:
71 group = Group.objects.filter(
72 Q(owner=request.user) |
73 Q(users__in=(request.user.pk, ))
74 ).distinct().get(Q(pk=pk))
75 except Group.DoesNotExist:
76 raise Http404
77
78 serializer = GroupSerializer(group, context={"request": self.request})
79 return Response(serializer.data)