From eaf89ff64ce5c4e39239aec50a973d1c58a40bee Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Fri, 26 Nov 2021 18:44:38 +0100 Subject: add groups views --- 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 +++++++++++++++++++++++++++-- 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 chat_api/serializers.py (limited to 'chat_api') 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 -- cgit v1.2.3