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, 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 | ||
14 | class Group(models.Model): | 14 | class 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 | |||
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 | |||
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 @@ | |||
1 | from django.urls import path | 1 | from django.urls import path |
2 | from .views import * | 2 | from .views import GroupView |
3 | 3 | ||
4 | urlpatterns = [ | 4 | urlpatterns = [ |
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 | |||
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 | ||
10 | from knox.auth import TokenAuthentication | 8 | from knox.auth import TokenAuthentication |
11 | 9 | ||
12 | from django.db.models.query import Q | 10 | from django.db.models.query import Q |
@@ -14,17 +12,6 @@ from django.db.models.query import Q | |||
14 | from .models import * | 12 | from .models import * |
15 | from .serializers import * | 13 | from .serializers import * |
16 | 14 | ||
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 | |||
28 | class GroupView(generics.ListCreateAPIView): | 15 | class 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 | |||
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) | ||