From eaf89ff64ce5c4e39239aec50a973d1c58a40bee Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Fri, 26 Nov 2021 18:44:38 +0100 Subject: add groups views --- .gitignore | 3 ++- auth_api/views.py | 4 ++-- chat_api/admin.py | 5 ++++- chat_api/models.py | 26 +++++++++++++++++++++++++- chat_api/serializers.py | 8 ++++++++ chat_api/urls.py | 3 ++- chat_api/views.py | 29 +++++++++++++++++++++++++++-- lom_api/urls.py | 3 ++- 8 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 chat_api/serializers.py diff --git a/.gitignore b/.gitignore index 7de1644..3283d7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ db.sqlite3 venv/ -__pycache__/ \ No newline at end of file +__pycache__/ +migrations/ \ 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 from django.http.response import Http404 from django.shortcuts import get_object_or_404 -from rest_framework import permissions, serializers +from rest_framework import permissions from rest_framework.authtoken.serializers import AuthTokenSerializer from rest_framework.response import Response from rest_framework.views import APIView @@ -16,7 +16,7 @@ from .serializers import UserSerializer, RegisterUserSerializer class LoginView(KnoxLoginView): permission_classes = (permissions.AllowAny, ) - def post(self, request, format=None): + def post(self, request): token_serializer = AuthTokenSerializer(data=request.data) token_serializer.is_valid(raise_exception=True) 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 @@ from django.contrib import admin +from .models import * -# Register your models here. +admin.site.register(DmMessage) +admin.site.register(GmMessage) +admin.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 @@ from django.db import models +from django.contrib.auth.models import User +from django.db.models.base import Model -# Create your models here. +class DmMessage(models.Model): + sender = models.ForeignKey(User, related_name="sender_dm_message", on_delete=models.CASCADE) + reciever = models.ForeignKey(User, related_name="receiver_dm_message", on_delete=models.CASCADE) + message = models.TextField(null=False) + create_date = models.DateTimeField(auto_now=True) + + def __str__(self): + return str(self.sender) + +class Group(models.Model): + name = models.CharField(max_length=16, null=False) + owner = models.ForeignKey(User, related_name="owner_group", on_delete=models.CASCADE) + users = models.ManyToManyField(User, related_name="users_group") + create_date = models.DateTimeField(auto_now=True) + +class GmMessage(models.Model): + sender = models.ForeignKey(User, related_name="sender_gm_message", on_delete=models.CASCADE) + reciever = models.ForeignKey(Group, related_name="receiver_gm_message", on_delete=models.CASCADE) + message = models.TextField(null=False) + create_date = models.DateTimeField(auto_now=True) + + def __str__(self): + 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 @@ +from django.db.models import fields +from rest_framework import serializers +from .models import * + +class GroupSerializer(serializers.ModelSerializer): + class Meta: + model = Group + 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 @@ from django.urls import path +from .views import GroupView urlpatterns = [ - + path('groups/', GroupView.as_view(), '') ] 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 @@ -from django.shortcuts import render +from django.db.models.query import QuerySet +from django.http.response import Http404 +from rest_framework.response import Response +from rest_framework.serializers import Serializer +from rest_framework.views import APIView +from rest_framework import permissions +from rest_framework import generics +from knox.auth import TokenAuthentication -# Create your views here. +from django.db.models.query import Q + +from .models import * +from .serializers import * + +class GroupView(generics.ListCreateAPIView): + permission_classes = (permissions.IsAuthenticated, ) + authentication_classes = (TokenAuthentication, ) + serializer_class = GroupSerializer + + def get_queryset(self): + return Group.objects.filter( + Q(owner=self.request.user) | + Q(users__in=(self.request.user, )) + ).distinct() + + def create(self, request, *args, **kwargs): + request.data['owner'] = request.user.pk + 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 urlpatterns = [ path('admin/', admin.site.urls), - path('api/auth/', include('auth_api.urls')) + path('api/auth/', include('auth_api.urls')), + path('api/chat/', include('chat_api.urls')), ] -- cgit v1.2.3