From e7f4bd9cc60aa3ad230b31c3918df1f2043d6942 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Sun, 12 Dec 2021 23:22:13 +0100 Subject: add grups views and modify info auth view --- auth_api/views.py | 12 +++++++++++- chat_api/models.py | 2 +- chat_api/views.py | 3 ++- index.http | 34 ++++++++++++++++++++++++++++++++++ rest.http | 12 ++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 index.http create mode 100644 rest.http diff --git a/auth_api/views.py b/auth_api/views.py index cd32914..36fc7e0 100644 --- a/auth_api/views.py +++ b/auth_api/views.py @@ -2,6 +2,7 @@ from django.contrib.auth import login from django.contrib.auth.models import User from django.http.response import Http404 from django.shortcuts import get_object_or_404 +from django.db.models import Q from rest_framework import permissions from rest_framework.authtoken.serializers import AuthTokenSerializer @@ -47,6 +48,15 @@ class UserInfo(APIView): def get(self, request): user = request.GET.get("pk", request.user) username = request.GET.get("username", None) + all_users = request.GET.get("all", None) + + if all_users: + users = User.objects.filter(~Q(username=request.user.username)) + serializer = UserSerializer(users, many=True) + + return Response(data={ + "users": serializer.data + }) if username: users = User.objects.filter(username__startswith = username)[:5] @@ -66,4 +76,4 @@ class UserInfo(APIView): return Response({ "user": serializer.data - }) \ No newline at end of file + }) diff --git a/chat_api/models.py b/chat_api/models.py index 4c370fc..4b354f4 100644 --- a/chat_api/models.py +++ b/chat_api/models.py @@ -24,4 +24,4 @@ class Gm(models.Model): create_date = models.DateTimeField(auto_now=True) def __str__(self): - return str(self.sender) \ No newline at end of file + return str(self.sender) diff --git a/chat_api/views.py b/chat_api/views.py index 6d161c1..3ea0e54 100644 --- a/chat_api/views.py +++ b/chat_api/views.py @@ -63,6 +63,7 @@ class GroupDetailView(generics.RetrieveUpdateDestroyAPIView): def update(self, request, *args, **kwargs): request.data['owner'] = request.user.pk set_users(request) + return super().update(request, *args, **kwargs) def retrieve(self, request, pk): @@ -112,4 +113,4 @@ class GmsView(APIView): serializer.is_valid(raise_exception=True) serializer.save() - return Response(serializer.data) \ No newline at end of file + return Response(serializer.data) diff --git a/index.http b/index.http new file mode 100644 index 0000000..6349a84 --- /dev/null +++ b/index.http @@ -0,0 +1,34 @@ +POST http://localhost:8000/api/auth/login/ +content-type: application/json + +{ + "username": "jakub", + "password": "jakub" +} + +### + +PUT http://localhost:8000/api/chat/groups/detail/1/ +Authorization: Token bd057a90565d85b2efa23f73deb9e5afdb4e4d56b24ba405a1cf33b66b49f4ed +content-type: application/json + +{ + "name": "Bar", + "users": [2] +} + +### + +POST http://localhost:8000/api/chat/groups/ +Authorization: Token bd057a90565d85b2efa23f73deb9e5afdb4e4d56b24ba405a1cf33b66b49f4ed +content-type: application/json + +{ + "name": "Foo" +} + + +### + +GET http://localhost:8000/api/auth/info/? +Authorization: Token bd057a90565d85b2efa23f73deb9e5afdb4e4d56b24ba405a1cf33b66b49f4ed diff --git a/rest.http b/rest.http new file mode 100644 index 0000000..b0c6198 --- /dev/null +++ b/rest.http @@ -0,0 +1,12 @@ +POST http://localhost:8000/api/auth/login/ +content-type: application/json + +{ + "username": "admin", + "password": "admin" +} + +### + +GET http://localhost:8000/api/auth/info?username=a +authorization: TOKEN 92844a78767178dfc15b0814373839bc80b97028f433440fcb4223088a9b8b69 -- cgit v1.2.3