diff --git a/api/__pycache__/serializers.cpython-39.pyc b/api/__pycache__/serializers.cpython-39.pyc index 5ec4e14..a50cceb 100644 Binary files a/api/__pycache__/serializers.cpython-39.pyc and b/api/__pycache__/serializers.cpython-39.pyc differ diff --git a/api/__pycache__/urls.cpython-39.pyc b/api/__pycache__/urls.cpython-39.pyc index a8592fb..24b991e 100644 Binary files a/api/__pycache__/urls.cpython-39.pyc and b/api/__pycache__/urls.cpython-39.pyc differ diff --git a/api/__pycache__/viewsets.cpython-39.pyc b/api/__pycache__/viewsets.cpython-39.pyc index fd01042..ecd8f49 100644 Binary files a/api/__pycache__/viewsets.cpython-39.pyc and b/api/__pycache__/viewsets.cpython-39.pyc differ diff --git a/api/serializers.py b/api/serializers.py index 899c364..22192d4 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,28 +1,37 @@ -from rest_framework.serializers import ModelSerializer, Serializer, CharField, HyperlinkedModelSerializer, Field +from rest_framework.serializers import ModelSerializer, Serializer, CharField, HyperlinkedModelSerializer, Field, BooleanField from chat_models.models import Chat, Message from users.models import UserChat, User from rest_framework import viewsets -class ChatSerializer(ModelSerializer): - class Meta: - model = Chat - fields = "__all__" - - class MessageSerializer(ModelSerializer): class Meta: model = Message fields = "__all__" + depth = 1 + + +class ChatSerializer(ModelSerializer): + message_set = MessageSerializer(many=True, read_only=True) + + class Meta: + model = Chat + fields = "__all__" + depth = 1 class UserChatSerializer(ModelSerializer): + class Meta: model = UserChat fields = "__all__" + depth = 1 class UserSerializer(ModelSerializer): + admin_chats = ChatSerializer(many=True, required=False, read_only=True) + viewers_chats = ChatSerializer(many=True, required=False, read_only=True) + class Meta: model = User fields = "__all__" @@ -42,16 +51,19 @@ class MessageCreateWithUsername(Serializer): author_tg_nickname = CharField(write_only=True) message = CharField(write_only=True) chat_id = CharField(write_only=True) + message_id = CharField(write_only=True) created_message = MessageSerializer(read_only=True) def create(self, validated_data): message = validated_data["message"] author_tg_nickname = validated_data["author_tg_nickname"] chat_id = validated_data["chat_id"] + message_id = validated_data["message_id"] messageModel = Message.objects.create( text=message, author=User.objects.get(tg_id=author_tg_nickname), chat=Chat.objects.get(tg_id=chat_id), + tg_id=message_id ) return { "created_message": messageModel @@ -66,7 +78,7 @@ class UserViewSet(viewsets.ModelViewSet): class ChatCreateWithTg(Serializer): name = CharField(write_only=True) - startMessage = CharField(write_only=True) + start_message = CharField(write_only=True) api_key = CharField(write_only=True) tg_id = CharField(write_only=True) admin_tg_id = CharField(write_only=True) @@ -74,13 +86,39 @@ class ChatCreateWithTg(Serializer): def create(self, validated_data): name = validated_data['name'] - startMessage = validated_data['startMessage'] + startMessage = validated_data['start_message'] api_key = validated_data['api_key'] tg_id = validated_data['tg_id'] admin_tg_id = validated_data['admin_tg_id'] + print(name, startMessage, api_key, tg_id, admin_tg_id) chat = Chat.objects.create(name=name, start_message=startMessage, api_key=api_key, admin=User.objects.get(tg_id=admin_tg_id), tg_id=tg_id) - return {'chat': chat} \ No newline at end of file + return {'chat': chat} + + +class AddUserToChatSerializer(Serializer): + chat_tg_id = CharField(write_only=True) + user_tg_id = CharField(write_only=True) + + added = BooleanField(read_only=True) + + def create(self, validated_data): + chat_tg_id = validated_data['chat_tg_id'] + user_tg_id = validated_data['user_tg_id'] + + Chat.objects.get(tg_id=chat_tg_id).viewers.add(User.objects.get(tg_id=user_tg_id)) + print(Chat.objects.get(tg_id=chat_tg_id), User.objects.get(tg_id=user_tg_id)) + return {"added": True} + + +class FromUsernameToTgSerializer(Serializer): + username = CharField(write_only=True) + user = UserSerializer(read_only=True) + + def create(self, validated_data): + username = validated_data["username"] + user = User.objects.get(username=username) + return {"user": user} diff --git a/api/urls.py b/api/urls.py index cf6704a..70ed35a 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,18 +1,17 @@ from django.urls import path -from .views import ListCreateMessage, ListCreateChat, RetrieveUpdateMessage, RetrieveUpdateDestroyChat, \ - ListCreateUserChat, ListCreateUser, RetrieveUpdateUserChat, RetrieveUpdateUser, CreateMessageFromUsername, CreateChatWithUsername -from .serializers import UserViewSet from rest_framework.routers import DefaultRouter -from .viewsets import UserViewSet, MessageViewSet, ChatViewSet +from .viewsets import UserViewSet, MessageViewSet, ChatViewSet, CreateChatFromTgViewSet, \ + MessageCreateWithUsernameViewSet, AddUserToChatViewSet, FromUsernameToUserViewset, FromTgIdToChats router = DefaultRouter() router.register('user', UserViewSet, basename='user') router.register('message', MessageViewSet, basename='message') router.register('chat', ChatViewSet, basename='chat') - -urlpatterns = [ - path("message-from-username", CreateMessageFromUsername.as_view()), - path("chat-from-username", CreateChatWithUsername.as_view()) -] +router.register('chat-from-username', CreateChatFromTgViewSet, basename='chat-from-username') +router.register('message-from-username', MessageCreateWithUsernameViewSet, basename='message-from-username') +router.register('add-viewer-to-chat', AddUserToChatViewSet, basename='add-user-to-chat') +router.register('from-username-to-user', FromUsernameToUserViewset, basename='from-username-to-user') +router.register('from-tg-id-to-admin-chats', FromTgIdToChats, basename='from-tg-id-to-admin-chats') +urlpatterns = [] urlpatterns.extend(router.urls) diff --git a/api/viewsets.py b/api/viewsets.py index 70b92f8..8acb423 100644 --- a/api/viewsets.py +++ b/api/viewsets.py @@ -1,7 +1,9 @@ -from rest_framework import viewsets +from rest_framework import viewsets, mixins from users.models import User from chat_models.models import Message, Chat -from .serializers import UserSerializer, MessageSerializer, ChatSerializer +from .serializers import UserSerializer, MessageSerializer, ChatSerializer, ChatCreateWithTg, MessageCreateWithUsername, \ + AddUserToChatSerializer +from rest_framework.response import Response class UserViewSet(viewsets.ModelViewSet): @@ -20,3 +22,33 @@ class ChatViewSet(viewsets.ModelViewSet): serializer_class = ChatSerializer lookup_field = 'tg_id' queryset = Chat.objects.all() + + +class CreateChatFromTgViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet): + serializer_class = ChatCreateWithTg + + +class MessageCreateWithUsernameViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet): + serializer_class = MessageCreateWithUsername + + +class AddUserToChatViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet): + serializer_class = AddUserToChatSerializer + + +class FromUsernameToUserViewset(mixins.RetrieveModelMixin, viewsets.GenericViewSet): + serializer_class = AddUserToChatSerializer + + def retrieve(self, request, *args, **kwargs): + pk = kwargs['pk'] + user = User.objects.get(username=pk) + return Response({"tg_id": user.tg_id}) + + +class FromTgIdToChats(mixins.RetrieveModelMixin, viewsets.GenericViewSet): + serializer_class = AddUserToChatSerializer + + def retrieve(self, request, *args, **kwargs): + pk = kwargs['pk'] + user = User.objects.get(tg_id=pk) + return Response({"chats": [{"api_key": i.api_key, "name": i.name} for i in user.admin_chats.all()]}) \ No newline at end of file diff --git a/chat_models/__pycache__/models.cpython-39.pyc b/chat_models/__pycache__/models.cpython-39.pyc index c406710..982dc84 100644 Binary files a/chat_models/__pycache__/models.cpython-39.pyc and b/chat_models/__pycache__/models.cpython-39.pyc differ diff --git a/chat_models/migrations/0007_auto_20220319_2234.py b/chat_models/migrations/0007_auto_20220319_2234.py new file mode 100644 index 0000000..67fc6e6 --- /dev/null +++ b/chat_models/migrations/0007_auto_20220319_2234.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.7 on 2022-03-19 19:34 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0005_auto_20220319_1853'), + ('chat_models', '0006_chat_tg_id'), + ] + + operations = [ + migrations.AddField( + model_name='chat', + name='viewers', + field=models.ManyToManyField(null=True, related_name='viewers_chats', to='users.User'), + ), + migrations.AlterField( + model_name='chat', + name='admin', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='admin_chats', to='users.user'), + ), + ] diff --git a/chat_models/migrations/0008_auto_20220320_0005.py b/chat_models/migrations/0008_auto_20220320_0005.py new file mode 100644 index 0000000..b3f7edd --- /dev/null +++ b/chat_models/migrations/0008_auto_20220320_0005.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 21:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0007_auto_20220319_2234'), + ] + + operations = [ + migrations.AlterField( + model_name='chat', + name='tg_id', + field=models.CharField(max_length=100, null=True, unique=True), + ), + ] diff --git a/chat_models/migrations/0009_auto_20220320_0008.py b/chat_models/migrations/0009_auto_20220320_0008.py new file mode 100644 index 0000000..e53a960 --- /dev/null +++ b/chat_models/migrations/0009_auto_20220320_0008.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 21:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0008_auto_20220320_0005'), + ] + + operations = [ + migrations.AlterField( + model_name='chat', + name='tg_id', + field=models.CharField(default='1', max_length=100, unique=True), + ), + ] diff --git a/chat_models/migrations/0010_auto_20220320_0009.py b/chat_models/migrations/0010_auto_20220320_0009.py new file mode 100644 index 0000000..001f545 --- /dev/null +++ b/chat_models/migrations/0010_auto_20220320_0009.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1.7 on 2022-03-19 21:09 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0009_auto_20220320_0008'), + ] + + operations = [ + migrations.AlterField( + model_name='chat', + name='tg_id', + field=models.CharField(default=uuid.uuid1, max_length=100, unique=True), + ), + ] diff --git a/chat_models/migrations/0011_alter_chat_id_alter_message_id.py b/chat_models/migrations/0011_alter_chat_id_alter_message_id.py new file mode 100644 index 0000000..9159951 --- /dev/null +++ b/chat_models/migrations/0011_alter_chat_id_alter_message_id.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.3 on 2022-03-19 21:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0010_auto_20220320_0009'), + ] + + operations = [ + migrations.AlterField( + model_name='chat', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.AlterField( + model_name='message', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + ] diff --git a/chat_models/migrations/__pycache__/0007_auto_20220319_2234.cpython-39.pyc b/chat_models/migrations/__pycache__/0007_auto_20220319_2234.cpython-39.pyc new file mode 100644 index 0000000..b422dd2 Binary files /dev/null and b/chat_models/migrations/__pycache__/0007_auto_20220319_2234.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0008_auto_20220320_0005.cpython-39.pyc b/chat_models/migrations/__pycache__/0008_auto_20220320_0005.cpython-39.pyc new file mode 100644 index 0000000..b95a8f6 Binary files /dev/null and b/chat_models/migrations/__pycache__/0008_auto_20220320_0005.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0009_auto_20220320_0008.cpython-39.pyc b/chat_models/migrations/__pycache__/0009_auto_20220320_0008.cpython-39.pyc new file mode 100644 index 0000000..6bc28b0 Binary files /dev/null and b/chat_models/migrations/__pycache__/0009_auto_20220320_0008.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0010_auto_20220320_0009.cpython-39.pyc b/chat_models/migrations/__pycache__/0010_auto_20220320_0009.cpython-39.pyc new file mode 100644 index 0000000..6d083bb Binary files /dev/null and b/chat_models/migrations/__pycache__/0010_auto_20220320_0009.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0011_alter_chat_id_alter_message_id.cpython-39.pyc b/chat_models/migrations/__pycache__/0011_alter_chat_id_alter_message_id.cpython-39.pyc new file mode 100644 index 0000000..ef7e2e3 Binary files /dev/null and b/chat_models/migrations/__pycache__/0011_alter_chat_id_alter_message_id.cpython-39.pyc differ diff --git a/chat_models/models.py b/chat_models/models.py index 0ed853b..5235750 100644 --- a/chat_models/models.py +++ b/chat_models/models.py @@ -1,12 +1,14 @@ from django.db import models +from uuid import uuid1 class Chat(models.Model): name = models.TextField() start_message = models.TextField(blank=True) api_key = models.TextField() - admin = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True) - tg_id = models.CharField(max_length=100, null=True) + admin = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True, related_name='admin_chats') + tg_id = models.CharField(max_length=100, unique=True, default=uuid1) + viewers = models.ManyToManyField("users.User", null=True, related_name='viewers_chats') class Message(models.Model): diff --git a/users/migrations/0006_alter_user_id_alter_userchat_id.py b/users/migrations/0006_alter_user_id_alter_userchat_id.py new file mode 100644 index 0000000..afbcddd --- /dev/null +++ b/users/migrations/0006_alter_user_id_alter_userchat_id.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.3 on 2022-03-19 21:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0005_auto_20220319_1853'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.AlterField( + model_name='userchat', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + ] diff --git a/users/migrations/__pycache__/0006_alter_user_id_alter_userchat_id.cpython-39.pyc b/users/migrations/__pycache__/0006_alter_user_id_alter_userchat_id.cpython-39.pyc new file mode 100644 index 0000000..bedc42c Binary files /dev/null and b/users/migrations/__pycache__/0006_alter_user_id_alter_userchat_id.cpython-39.pyc differ