summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chat_api/admin.py4
-rw-r--r--chat_api/models.py12
-rw-r--r--chat_api/serializers.py38
-rw-r--r--chat_api/urls.py5
-rw-r--r--chat_api/views.py38
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 @@
1from django.contrib import admin 1from django.contrib import admin
2from .models import * 2from .models import *
3 3
4admin.site.register(DmMessage) 4admin.site.register(Dm)
5admin.site.register(GmMessage) 5admin.site.register(Gm)
6admin.site.register(Group) \ No newline at end of file 6admin.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
2from django.contrib.auth.models import User 2from django.contrib.auth.models import User
3from django.db.models.base import Model 3from django.db.models.base import Model
4 4
5class DmMessage(models.Model): 5class 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
20class GmMessage(models.Model): 20class 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
19class 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
31class 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
43class 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
2from .views import * 2from .views import *
3 3
4urlpatterns = [ 4urlpatterns = [
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 @@
1from django.db.models.query import QuerySet 1from django.db.models.query import QuerySet
2from django.http.response import Http404 2from django.http.response import Http404
3from rest_framework.response import Response 3from rest_framework.response import Response
4from rest_framework.serializers import Serializer
5from rest_framework.views import APIView 4from rest_framework.views import APIView
6from rest_framework import permissions 5from rest_framework import permissions
7from rest_framework import generics 6from 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
80class 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