diff options
author | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-29 10:50:24 +0100 |
---|---|---|
committer | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-29 10:50:24 +0100 |
commit | ff53fd490b02536733f6618908760b9e3d52eeb3 (patch) | |
tree | ecbc500ec8854bb693f0aa4341961a2fea74ecc4 /chat_api | |
parent | b77b24cfae3ccafd4bbbd50c87107ebfd88bf282 (diff) | |
download | lom_api-ff53fd490b02536733f6618908760b9e3d52eeb3.tar.gz lom_api-ff53fd490b02536733f6618908760b9e3d52eeb3.zip |
add group messages api
Diffstat (limited to 'chat_api')
-rw-r--r-- | chat_api/admin.py | 4 | ||||
-rw-r--r-- | chat_api/models.py | 12 | ||||
-rw-r--r-- | chat_api/serializers.py | 38 | ||||
-rw-r--r-- | chat_api/urls.py | 5 | ||||
-rw-r--r-- | chat_api/views.py | 38 |
5 files changed, 86 insertions, 11 deletions
diff --git a/chat_api/admin.py b/chat_api/admin.py index 50ad2b0..7aa8358 100644 --- a/chat_api/admin.py +++ b/chat_api/admin.py | |||
@@ -1,6 +1,6 @@ | |||
1 | from django.contrib import admin | 1 | from django.contrib import admin |
2 | from .models import * | 2 | from .models import * |
3 | 3 | ||
4 | admin.site.register(DmMessage) | 4 | admin.site.register(Dm) |
5 | admin.site.register(GmMessage) | 5 | admin.site.register(Gm) |
6 | admin.site.register(Group) \ No newline at end of file | 6 | admin.site.register(Group) \ No newline at end of file |
diff --git a/chat_api/models.py b/chat_api/models.py index 54fb0f3..4c370fc 100644 --- a/chat_api/models.py +++ b/chat_api/models.py | |||
@@ -2,9 +2,9 @@ from django.db import models | |||
2 | from django.contrib.auth.models import User | 2 | from django.contrib.auth.models import User |
3 | from django.db.models.base import Model | 3 | from django.db.models.base import Model |
4 | 4 | ||
5 | class DmMessage(models.Model): | 5 | class Dm(models.Model): |
6 | sender = models.ForeignKey(User, related_name="sender_dm_message", on_delete=models.CASCADE) | 6 | sender = models.ForeignKey(User, related_name="sender_dm", on_delete=models.CASCADE) |
7 | reciever = models.ForeignKey(User, related_name="receiver_dm_message", on_delete=models.CASCADE) | 7 | reciever = models.ForeignKey(User, related_name="receiver_dm", on_delete=models.CASCADE) |
8 | message = models.TextField(null=False) | 8 | message = models.TextField(null=False) |
9 | create_date = models.DateTimeField(auto_now=True) | 9 | create_date = models.DateTimeField(auto_now=True) |
10 | 10 | ||
@@ -17,9 +17,9 @@ class Group(models.Model): | |||
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) |
19 | 19 | ||
20 | class GmMessage(models.Model): | 20 | class Gm(models.Model): |
21 | sender = models.ForeignKey(User, related_name="sender_gm_message", on_delete=models.CASCADE) | 21 | sender = models.ForeignKey(User, related_name="sender_gm", on_delete=models.CASCADE) |
22 | reciever = models.ForeignKey(Group, related_name="receiver_gm_message", on_delete=models.CASCADE) | 22 | reciever = models.ForeignKey(Group, related_name="receiver_gm", on_delete=models.CASCADE) |
23 | message = models.TextField(null=False) | 23 | message = models.TextField(null=False) |
24 | create_date = models.DateTimeField(auto_now=True) | 24 | create_date = models.DateTimeField(auto_now=True) |
25 | 25 | ||
diff --git a/chat_api/serializers.py b/chat_api/serializers.py index 585bb36..15828be 100644 --- a/chat_api/serializers.py +++ b/chat_api/serializers.py | |||
@@ -15,3 +15,41 @@ class GroupSerializer(serializers.ModelSerializer): | |||
15 | model = Group | 15 | model = Group |
16 | fields = '__all__' | 16 | fields = '__all__' |
17 | extra_fields = ('is_mine', ) | 17 | extra_fields = ('is_mine', ) |
18 | |||
19 | class DmSerializer(serializers.ModelSerializer): | ||
20 | is_mine = serializers.SerializerMethodField('_is_mine') | ||
21 | |||
22 | def _is_mine(self, obj): | ||
23 | request = self.context.get('request') | ||
24 | if request: | ||
25 | return request.user == obj.sender | ||
26 | return False | ||
27 | class Meta: | ||
28 | model = Dm | ||
29 | fields = '__all__' | ||
30 | |||
31 | class GmSerializer(serializers.ModelSerializer): | ||
32 | is_mine = serializers.SerializerMethodField('_is_mine') | ||
33 | |||
34 | def _is_mine(self, obj): | ||
35 | request = self.context.get('request') | ||
36 | if request: | ||
37 | return request.user == obj.sender | ||
38 | return False | ||
39 | class Meta: | ||
40 | model = Gm | ||
41 | fields = '__all__' | ||
42 | |||
43 | class GroupMessagesSerializer(serializers.ModelSerializer): | ||
44 | messages = serializers.SerializerMethodField('_messages') | ||
45 | |||
46 | def _messages(self, obj): | ||
47 | request = self.context.get('request') | ||
48 | if request: | ||
49 | gms = GmSerializer(obj.receiver_gm.all(), many=True, read_only=True, context={"request": request}) | ||
50 | return gms.data | ||
51 | return [] | ||
52 | |||
53 | class Meta: | ||
54 | model=Group | ||
55 | fields=('messages', ) | ||
diff --git a/chat_api/urls.py b/chat_api/urls.py index 617a0f2..a860b5d 100644 --- a/chat_api/urls.py +++ b/chat_api/urls.py | |||
@@ -2,6 +2,7 @@ from django.urls import path | |||
2 | from .views import * | 2 | from .views import * |
3 | 3 | ||
4 | urlpatterns = [ | 4 | urlpatterns = [ |
5 | path('groups/', GroupView.as_view(), 'groups_list'), | 5 | path('groups/', GroupView.as_view(), name='groups_list'), |
6 | path('groups/detail/<int:pk>/', GroupDetailView.as_view(), 'groups_detail'), | 6 | path('groups/detail/<int:pk>/', GroupDetailView.as_view(), name='groups_detail'), |
7 | path('groups/detail/<int:pk>/messages/', GmsView.as_view(), name='group_messages'), | ||
7 | ] | 8 | ] |
diff --git a/chat_api/views.py b/chat_api/views.py index eca6d45..6d161c1 100644 --- a/chat_api/views.py +++ b/chat_api/views.py | |||
@@ -1,7 +1,6 @@ | |||
1 | from django.db.models.query import QuerySet | 1 | from django.db.models.query import QuerySet |
2 | from django.http.response import Http404 | 2 | from django.http.response import Http404 |
3 | from rest_framework.response import Response | 3 | from rest_framework.response import Response |
4 | from rest_framework.serializers import Serializer | ||
5 | from rest_framework.views import APIView | 4 | from rest_framework.views import APIView |
6 | from rest_framework import permissions | 5 | from rest_framework import permissions |
7 | from rest_framework import generics | 6 | from rest_framework import generics |
@@ -77,3 +76,40 @@ class GroupDetailView(generics.RetrieveUpdateDestroyAPIView): | |||
77 | 76 | ||
78 | serializer = GroupSerializer(group, context={"request": self.request}) | 77 | serializer = GroupSerializer(group, context={"request": self.request}) |
79 | return Response(serializer.data) | 78 | return Response(serializer.data) |
79 | |||
80 | class GmsView(APIView): | ||
81 | permission_classes = (permissions.IsAuthenticated, ) | ||
82 | authentication_classes = (TokenAuthentication, ) | ||
83 | |||
84 | def get(self, request, pk): | ||
85 | try: | ||
86 | group = Group.objects.filter( | ||
87 | Q(owner=request.user) | | ||
88 | Q(users__in=(request.user.pk, )) | ||
89 | ).distinct().get(Q(pk=pk)) | ||
90 | except Group.DoesNotExist: | ||
91 | raise Http404 | ||
92 | |||
93 | serialzier = GroupMessagesSerializer( | ||
94 | group, | ||
95 | context={"request": request} | ||
96 | ) | ||
97 | |||
98 | return Response(serialzier.data) | ||
99 | |||
100 | def post(self, request, pk): | ||
101 | try: # check is user in group TODO: Make it better | ||
102 | Group.objects.filter( | ||
103 | Q(owner=request.user) | | ||
104 | Q(users__in=(request.user.pk, )) | ||
105 | ).distinct().get(Q(pk=pk)) | ||
106 | except Group.DoesNotExist: | ||
107 | raise Http404 | ||
108 | |||
109 | request.data['sender'] = request.user.pk | ||
110 | request.data['reciever'] = pk | ||
111 | serializer = GmSerializer(data=request.data) | ||
112 | serializer.is_valid(raise_exception=True) | ||
113 | serializer.save() | ||
114 | |||
115 | return Response(serializer.data) \ No newline at end of file | ||