mirror of
https://github.com/evgen-app/hackbot.git
synced 2024-12-04 22:03:46 +03:00
last commit
This commit is contained in:
parent
8e60b9cd01
commit
1329114286
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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 chat_models.models import Chat, Message
|
||||||
from users.models import UserChat, User
|
from users.models import UserChat, User
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
|
|
||||||
class ChatSerializer(ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = Chat
|
|
||||||
fields = "__all__"
|
|
||||||
|
|
||||||
|
|
||||||
class MessageSerializer(ModelSerializer):
|
class MessageSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Message
|
model = Message
|
||||||
fields = "__all__"
|
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 UserChatSerializer(ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UserChat
|
model = UserChat
|
||||||
fields = "__all__"
|
fields = "__all__"
|
||||||
|
depth = 1
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(ModelSerializer):
|
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:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = "__all__"
|
fields = "__all__"
|
||||||
|
@ -42,16 +51,19 @@ class MessageCreateWithUsername(Serializer):
|
||||||
author_tg_nickname = CharField(write_only=True)
|
author_tg_nickname = CharField(write_only=True)
|
||||||
message = CharField(write_only=True)
|
message = CharField(write_only=True)
|
||||||
chat_id = CharField(write_only=True)
|
chat_id = CharField(write_only=True)
|
||||||
|
message_id = CharField(write_only=True)
|
||||||
created_message = MessageSerializer(read_only=True)
|
created_message = MessageSerializer(read_only=True)
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
message = validated_data["message"]
|
message = validated_data["message"]
|
||||||
author_tg_nickname = validated_data["author_tg_nickname"]
|
author_tg_nickname = validated_data["author_tg_nickname"]
|
||||||
chat_id = validated_data["chat_id"]
|
chat_id = validated_data["chat_id"]
|
||||||
|
message_id = validated_data["message_id"]
|
||||||
messageModel = Message.objects.create(
|
messageModel = Message.objects.create(
|
||||||
text=message,
|
text=message,
|
||||||
author=User.objects.get(tg_id=author_tg_nickname),
|
author=User.objects.get(tg_id=author_tg_nickname),
|
||||||
chat=Chat.objects.get(tg_id=chat_id),
|
chat=Chat.objects.get(tg_id=chat_id),
|
||||||
|
tg_id=message_id
|
||||||
)
|
)
|
||||||
return {
|
return {
|
||||||
"created_message": messageModel
|
"created_message": messageModel
|
||||||
|
@ -66,7 +78,7 @@ class UserViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
class ChatCreateWithTg(Serializer):
|
class ChatCreateWithTg(Serializer):
|
||||||
name = CharField(write_only=True)
|
name = CharField(write_only=True)
|
||||||
startMessage = CharField(write_only=True)
|
start_message = CharField(write_only=True)
|
||||||
api_key = CharField(write_only=True)
|
api_key = CharField(write_only=True)
|
||||||
tg_id = CharField(write_only=True)
|
tg_id = CharField(write_only=True)
|
||||||
admin_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):
|
def create(self, validated_data):
|
||||||
name = validated_data['name']
|
name = validated_data['name']
|
||||||
startMessage = validated_data['startMessage']
|
startMessage = validated_data['start_message']
|
||||||
api_key = validated_data['api_key']
|
api_key = validated_data['api_key']
|
||||||
tg_id = validated_data['tg_id']
|
tg_id = validated_data['tg_id']
|
||||||
admin_tg_id = validated_data['admin_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,
|
chat = Chat.objects.create(name=name,
|
||||||
start_message=startMessage,
|
start_message=startMessage,
|
||||||
api_key=api_key,
|
api_key=api_key,
|
||||||
admin=User.objects.get(tg_id=admin_tg_id),
|
admin=User.objects.get(tg_id=admin_tg_id),
|
||||||
tg_id=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}
|
||||||
|
|
17
api/urls.py
17
api/urls.py
|
@ -1,18 +1,17 @@
|
||||||
from django.urls import path
|
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 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 = DefaultRouter()
|
||||||
|
|
||||||
router.register('user', UserViewSet, basename='user')
|
router.register('user', UserViewSet, basename='user')
|
||||||
router.register('message', MessageViewSet, basename='message')
|
router.register('message', MessageViewSet, basename='message')
|
||||||
router.register('chat', ChatViewSet, basename='chat')
|
router.register('chat', ChatViewSet, basename='chat')
|
||||||
|
router.register('chat-from-username', CreateChatFromTgViewSet, basename='chat-from-username')
|
||||||
urlpatterns = [
|
router.register('message-from-username', MessageCreateWithUsernameViewSet, basename='message-from-username')
|
||||||
path("message-from-username", CreateMessageFromUsername.as_view()),
|
router.register('add-viewer-to-chat', AddUserToChatViewSet, basename='add-user-to-chat')
|
||||||
path("chat-from-username", CreateChatWithUsername.as_view())
|
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)
|
urlpatterns.extend(router.urls)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets, mixins
|
||||||
from users.models import User
|
from users.models import User
|
||||||
from chat_models.models import Message, Chat
|
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):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -20,3 +22,33 @@ class ChatViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = ChatSerializer
|
serializer_class = ChatSerializer
|
||||||
lookup_field = 'tg_id'
|
lookup_field = 'tg_id'
|
||||||
queryset = Chat.objects.all()
|
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()]})
|
Binary file not shown.
25
chat_models/migrations/0007_auto_20220319_2234.py
Normal file
25
chat_models/migrations/0007_auto_20220319_2234.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
18
chat_models/migrations/0008_auto_20220320_0005.py
Normal file
18
chat_models/migrations/0008_auto_20220320_0005.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
18
chat_models/migrations/0009_auto_20220320_0008.py
Normal file
18
chat_models/migrations/0009_auto_20220320_0008.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
19
chat_models/migrations/0010_auto_20220320_0009.py
Normal file
19
chat_models/migrations/0010_auto_20220320_0009.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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'),
|
||||||
|
),
|
||||||
|
]
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,12 +1,14 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from uuid import uuid1
|
||||||
|
|
||||||
|
|
||||||
class Chat(models.Model):
|
class Chat(models.Model):
|
||||||
name = models.TextField()
|
name = models.TextField()
|
||||||
start_message = models.TextField(blank=True)
|
start_message = models.TextField(blank=True)
|
||||||
api_key = models.TextField()
|
api_key = models.TextField()
|
||||||
admin = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True)
|
admin = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True, related_name='admin_chats')
|
||||||
tg_id = models.CharField(max_length=100, null=True)
|
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):
|
class Message(models.Model):
|
||||||
|
|
23
users/migrations/0006_alter_user_id_alter_userchat_id.py
Normal file
23
users/migrations/0006_alter_user_id_alter_userchat_id.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user