summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2021-11-28 22:57:10 +0100
committerjdlugosz963 <jdlugosz963@gmail.com>2021-11-28 22:57:10 +0100
commit2634ae627659b71790147581880529dad741f425 (patch)
tree1efff96174ef42491f8823a29655d4e275208b17
parenteaf89ff64ce5c4e39239aec50a973d1c58a40bee (diff)
downloadlom_api-2634ae627659b71790147581880529dad741f425.tar.gz
lom_api-2634ae627659b71790147581880529dad741f425.zip
change login api
-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, 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):
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) 15 name = models.CharField(max_length=16, null=False, unique=True)
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 50d84e0..585bb36 100644
--- a/chat_api/serializers.py
+++ b/chat_api/serializers.py
@@ -3,6 +3,15 @@ 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
6 class Meta: 14 class Meta:
7 model = Group 15 model = Group
8 fields = '__all__' 16 fields = '__all__'
17 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 @@
1from django.urls import path 1from django.urls import path
2from .views import GroupView 2from .views import *
3 3
4urlpatterns = [ 4urlpatterns = [
5 path('groups/', GroupView.as_view(), '') 5 path('groups/', GroupView.as_view(), 'groups_list'),
6 path('groups/detail/<int:pk>/', GroupDetailView.as_view(), 'groups_detail'),
6] 7]
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
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
8from knox.auth import TokenAuthentication 10from knox.auth import TokenAuthentication
9 11
10from django.db.models.query import Q 12from django.db.models.query import Q
@@ -12,6 +14,17 @@ from django.db.models.query import Q
12from .models import * 14from .models import *
13from .serializers import * 15from .serializers import *
14 16
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
15class GroupView(generics.ListCreateAPIView): 28class GroupView(generics.ListCreateAPIView):
16 permission_classes = (permissions.IsAuthenticated, ) 29 permission_classes = (permissions.IsAuthenticated, )
17 authentication_classes = (TokenAuthentication, ) 30 authentication_classes = (TokenAuthentication, )
@@ -20,9 +33,47 @@ class GroupView(generics.ListCreateAPIView):
20 def get_queryset(self): 33 def get_queryset(self):
21 return Group.objects.filter( 34 return Group.objects.filter(
22 Q(owner=self.request.user) | 35 Q(owner=self.request.user) |
23 Q(users__in=(self.request.user, )) 36 Q(users__in=(self.request.user.pk, ))
24 ).distinct() 37 ).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
25 43
26 def create(self, request, *args, **kwargs): 44 def create(self, request, *args, **kwargs):
27 request.data['owner'] = request.user.pk 45 request.data['owner'] = request.user.pk
28 return super().create(request, *args, **kwargs) \ No newline at end of file 46 set_users(request)
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)