summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2021-11-26 18:44:38 +0100
committerjdlugosz963 <jdlugosz963@gmail.com>2021-11-26 18:44:38 +0100
commiteaf89ff64ce5c4e39239aec50a973d1c58a40bee (patch)
tree161c53033c08f9f0bb499dd3c1f394a0f508cf4e
parent45daed0c4fd60febf9458c889f7a89c0e5ab70bd (diff)
downloadlom_api-eaf89ff64ce5c4e39239aec50a973d1c58a40bee.tar.gz
lom_api-eaf89ff64ce5c4e39239aec50a973d1c58a40bee.zip
add groups views
-rw-r--r--.gitignore3
-rw-r--r--auth_api/views.py4
-rw-r--r--chat_api/admin.py5
-rw-r--r--chat_api/models.py26
-rw-r--r--chat_api/serializers.py8
-rw-r--r--chat_api/urls.py3
-rw-r--r--chat_api/views.py29
-rw-r--r--lom_api/urls.py3
8 files changed, 72 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index 7de1644..3283d7c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
1db.sqlite3 1db.sqlite3
2venv/ 2venv/
3__pycache__/ \ No newline at end of file 3__pycache__/
4migrations/ \ No newline at end of file
diff --git a/auth_api/views.py b/auth_api/views.py
index efd98b9..cd32914 100644
--- a/auth_api/views.py
+++ b/auth_api/views.py
@@ -3,7 +3,7 @@ from django.contrib.auth.models import User
3from django.http.response import Http404 3from django.http.response import Http404
4from django.shortcuts import get_object_or_404 4from django.shortcuts import get_object_or_404
5 5
6from rest_framework import permissions, serializers 6from rest_framework import permissions
7from rest_framework.authtoken.serializers import AuthTokenSerializer 7from rest_framework.authtoken.serializers import AuthTokenSerializer
8from rest_framework.response import Response 8from rest_framework.response import Response
9from rest_framework.views import APIView 9from rest_framework.views import APIView
@@ -16,7 +16,7 @@ from .serializers import UserSerializer, RegisterUserSerializer
16class LoginView(KnoxLoginView): 16class LoginView(KnoxLoginView):
17 permission_classes = (permissions.AllowAny, ) 17 permission_classes = (permissions.AllowAny, )
18 18
19 def post(self, request, format=None): 19 def post(self, request):
20 token_serializer = AuthTokenSerializer(data=request.data) 20 token_serializer = AuthTokenSerializer(data=request.data)
21 token_serializer.is_valid(raise_exception=True) 21 token_serializer.is_valid(raise_exception=True)
22 user = token_serializer.validated_data['user'] 22 user = token_serializer.validated_data['user']
diff --git a/chat_api/admin.py b/chat_api/admin.py
index 8c38f3f..50ad2b0 100644
--- a/chat_api/admin.py
+++ b/chat_api/admin.py
@@ -1,3 +1,6 @@
1from django.contrib import admin 1from django.contrib import admin
2from .models import *
2 3
3# Register your models here. 4admin.site.register(DmMessage)
5admin.site.register(GmMessage)
6admin.site.register(Group) \ No newline at end of file
diff --git a/chat_api/models.py b/chat_api/models.py
index 71a8362..5076d0b 100644
--- a/chat_api/models.py
+++ b/chat_api/models.py
@@ -1,3 +1,27 @@
1from django.db import models 1from django.db import models
2from django.contrib.auth.models import User
3from django.db.models.base import Model
2 4
3# Create your models here. 5class DmMessage(models.Model):
6 sender = models.ForeignKey(User, related_name="sender_dm_message", on_delete=models.CASCADE)
7 reciever = models.ForeignKey(User, related_name="receiver_dm_message", on_delete=models.CASCADE)
8 message = models.TextField(null=False)
9 create_date = models.DateTimeField(auto_now=True)
10
11 def __str__(self):
12 return str(self.sender)
13
14class Group(models.Model):
15 name = models.CharField(max_length=16, null=False)
16 owner = models.ForeignKey(User, related_name="owner_group", on_delete=models.CASCADE)
17 users = models.ManyToManyField(User, related_name="users_group")
18 create_date = models.DateTimeField(auto_now=True)
19
20class GmMessage(models.Model):
21 sender = models.ForeignKey(User, related_name="sender_gm_message", on_delete=models.CASCADE)
22 reciever = models.ForeignKey(Group, related_name="receiver_gm_message", on_delete=models.CASCADE)
23 message = models.TextField(null=False)
24 create_date = models.DateTimeField(auto_now=True)
25
26 def __str__(self):
27 return str(self.sender) \ No newline at end of file
diff --git a/chat_api/serializers.py b/chat_api/serializers.py
new file mode 100644
index 0000000..50d84e0
--- /dev/null
+++ b/chat_api/serializers.py
@@ -0,0 +1,8 @@
1from django.db.models import fields
2from rest_framework import serializers
3from .models import *
4
5class GroupSerializer(serializers.ModelSerializer):
6 class Meta:
7 model = Group
8 fields = '__all__'
diff --git a/chat_api/urls.py b/chat_api/urls.py
index d2d839f..9fa6016 100644
--- a/chat_api/urls.py
+++ b/chat_api/urls.py
@@ -1,5 +1,6 @@
1from django.urls import path 1from django.urls import path
2from .views import GroupView
2 3
3urlpatterns = [ 4urlpatterns = [
4 5 path('groups/', GroupView.as_view(), '')
5] 6]
diff --git a/chat_api/views.py b/chat_api/views.py
index 91ea44a..f94f3a0 100644
--- a/chat_api/views.py
+++ b/chat_api/views.py
@@ -1,3 +1,28 @@
1from django.shortcuts import render 1from django.db.models.query import QuerySet
2from django.http.response import Http404
3from rest_framework.response import Response
4from rest_framework.serializers import Serializer
5from rest_framework.views import APIView
6from rest_framework import permissions
7from rest_framework import generics
8from knox.auth import TokenAuthentication
2 9
3# Create your views here. 10from django.db.models.query import Q
11
12from .models import *
13from .serializers import *
14
15class GroupView(generics.ListCreateAPIView):
16 permission_classes = (permissions.IsAuthenticated, )
17 authentication_classes = (TokenAuthentication, )
18 serializer_class = GroupSerializer
19
20 def get_queryset(self):
21 return Group.objects.filter(
22 Q(owner=self.request.user) |
23 Q(users__in=(self.request.user, ))
24 ).distinct()
25
26 def create(self, request, *args, **kwargs):
27 request.data['owner'] = request.user.pk
28 return super().create(request, *args, **kwargs) \ No newline at end of file
diff --git a/lom_api/urls.py b/lom_api/urls.py
index 0bc8e32..64b6235 100644
--- a/lom_api/urls.py
+++ b/lom_api/urls.py
@@ -18,5 +18,6 @@ from django.urls import path, include
18 18
19urlpatterns = [ 19urlpatterns = [
20 path('admin/', admin.site.urls), 20 path('admin/', admin.site.urls),
21 path('api/auth/', include('auth_api.urls')) 21 path('api/auth/', include('auth_api.urls')),
22 path('api/chat/', include('chat_api.urls')),
22] 23]