diff options
-rw-r--r-- | chat_api/models.py | 2 | ||||
-rw-r--r-- | chat_api/serializers.py | 9 | ||||
-rw-r--r-- | chat_api/urls.py | 5 | ||||
-rw-r--r-- | 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): | |||
12 | return str(self.sender) | 12 | return str(self.sender) |
13 | 13 | ||
14 | class Group(models.Model): | 14 | class 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 | |||
3 | from .models import * | 3 | from .models import * |
4 | 4 | ||
5 | class GroupSerializer(serializers.ModelSerializer): | 5 | class 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 @@ | |||
1 | from django.urls import path | 1 | from django.urls import path |
2 | from .views import GroupView | 2 | from .views import * |
3 | 3 | ||
4 | urlpatterns = [ | 4 | urlpatterns = [ |
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 | |||
5 | from rest_framework.views import APIView | 5 | from rest_framework.views import APIView |
6 | from rest_framework import permissions | 6 | from rest_framework import permissions |
7 | from rest_framework import generics | 7 | from rest_framework import generics |
8 | from rest_framework.views import Http404 | ||
9 | from rest_framework.generics import get_object_or_404 | ||
8 | from knox.auth import TokenAuthentication | 10 | from knox.auth import TokenAuthentication |
9 | 11 | ||
10 | from django.db.models.query import Q | 12 | from django.db.models.query import Q |
@@ -12,6 +14,17 @@ from django.db.models.query import Q | |||
12 | from .models import * | 14 | from .models import * |
13 | from .serializers import * | 15 | from .serializers import * |
14 | 16 | ||
17 | |||
18 | def 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 | |||
15 | class GroupView(generics.ListCreateAPIView): | 28 | class 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 | |||
49 | class 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) | ||