hackbot/api/serializers.py

125 lines
3.8 KiB
Python
Raw Normal View History

2022-03-20 15:46:53 +03:00
from rest_framework.serializers import ModelSerializer, Serializer, CharField, HyperlinkedModelSerializer, Field, BooleanField
2022-03-18 18:13:36 +03:00
from chat_models.models import Chat, Message
2022-03-19 20:23:17 +03:00
from users.models import UserChat, User
from rest_framework import viewsets
2022-03-18 18:13:36 +03:00
2022-03-20 15:46:53 +03:00
class MessageSerializer(ModelSerializer):
2022-03-18 18:13:36 +03:00
class Meta:
2022-03-20 15:46:53 +03:00
model = Message
2022-03-18 18:13:36 +03:00
fields = "__all__"
2022-03-20 15:46:53 +03:00
depth = 1
2022-03-18 18:13:36 +03:00
2022-03-20 15:46:53 +03:00
class ChatSerializer(ModelSerializer):
message_set = MessageSerializer(many=True, read_only=True)
2022-03-18 18:13:36 +03:00
class Meta:
2022-03-20 15:46:53 +03:00
model = Chat
2022-03-18 18:13:36 +03:00
fields = "__all__"
2022-03-20 15:46:53 +03:00
depth = 1
2022-03-19 20:23:17 +03:00
class UserChatSerializer(ModelSerializer):
2022-03-20 15:46:53 +03:00
2022-03-19 20:23:17 +03:00
class Meta:
model = UserChat
fields = "__all__"
2022-03-20 15:46:53 +03:00
depth = 1
2022-03-19 20:23:17 +03:00
class UserSerializer(ModelSerializer):
2022-03-20 15:46:53 +03:00
admin_chats = ChatSerializer(many=True, required=False, read_only=True)
viewers_chats = ChatSerializer(many=True, required=False, read_only=True)
2022-03-19 20:23:17 +03:00
class Meta:
model = User
fields = "__all__"
lookup_field = 'user__tg_id'
class UserHyperlinkedSerializer(HyperlinkedModelSerializer):
user_tg = Field(source="user.tg_id")
class Meta:
model = User
fields = "__all__"
lookup_field = 'user_tg'
class MessageCreateWithUsername(Serializer):
author_tg_nickname = CharField(write_only=True)
message = CharField(write_only=True)
chat_id = CharField(write_only=True)
2022-03-20 15:46:53 +03:00
message_id = CharField(write_only=True)
2022-03-19 20:23:17 +03:00
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"]
2022-03-20 15:46:53 +03:00
message_id = validated_data["message_id"]
2022-03-19 20:23:17 +03:00
messageModel = Message.objects.create(
text=message,
author=User.objects.get(tg_id=author_tg_nickname),
chat=Chat.objects.get(tg_id=chat_id),
2022-03-20 15:46:53 +03:00
tg_id=message_id
2022-03-19 20:23:17 +03:00
)
return {
"created_message": messageModel
}
class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
lookup_field = 'tg_id'
queryset = User.objects.all()
class ChatCreateWithTg(Serializer):
name = CharField(write_only=True)
2022-03-20 15:46:53 +03:00
start_message = CharField(write_only=True)
2022-03-19 20:23:17 +03:00
api_key = CharField(write_only=True)
tg_id = CharField(write_only=True)
admin_tg_id = CharField(write_only=True)
chat = ChatSerializer(read_only=True)
def create(self, validated_data):
name = validated_data['name']
2022-03-20 15:46:53 +03:00
startMessage = validated_data['start_message']
2022-03-19 20:23:17 +03:00
api_key = validated_data['api_key']
tg_id = validated_data['tg_id']
admin_tg_id = validated_data['admin_tg_id']
2022-03-20 15:46:53 +03:00
print(name, startMessage, api_key, tg_id, admin_tg_id)
2022-03-19 20:23:17 +03:00
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)
2022-03-20 15:46:53 +03:00
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}