last commit

This commit is contained in:
ilia 2022-03-20 15:46:53 +03:00
parent 8e60b9cd01
commit 1329114286
20 changed files with 220 additions and 23 deletions

Binary file not shown.

View File

@ -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}
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}

View File

@ -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)

View File

@ -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()]})

View File

@ -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'),
),
]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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'),
),
]

View File

@ -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):

View File

@ -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'),
),
]