diff --git a/api/__pycache__/serializers.cpython-39.pyc b/api/__pycache__/serializers.cpython-39.pyc index 9f61493..5ec4e14 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 3b413c3..a8592fb 100644 Binary files a/api/__pycache__/urls.cpython-39.pyc and b/api/__pycache__/urls.cpython-39.pyc differ diff --git a/api/__pycache__/views.cpython-39.pyc b/api/__pycache__/views.cpython-39.pyc index 08740a0..6fac161 100644 Binary files a/api/__pycache__/views.cpython-39.pyc and b/api/__pycache__/views.cpython-39.pyc differ diff --git a/api/__pycache__/viewsets.cpython-39.pyc b/api/__pycache__/viewsets.cpython-39.pyc new file mode 100644 index 0000000..fd01042 Binary files /dev/null and b/api/__pycache__/viewsets.cpython-39.pyc differ diff --git a/api/serializers.py b/api/serializers.py index a6d0943..899c364 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,5 +1,7 @@ -from rest_framework.serializers import ModelSerializer +from rest_framework.serializers import ModelSerializer, Serializer, CharField, HyperlinkedModelSerializer, Field from chat_models.models import Chat, Message +from users.models import UserChat, User +from rest_framework import viewsets class ChatSerializer(ModelSerializer): @@ -12,3 +14,73 @@ class MessageSerializer(ModelSerializer): class Meta: model = Message fields = "__all__" + + +class UserChatSerializer(ModelSerializer): + class Meta: + model = UserChat + fields = "__all__" + + +class UserSerializer(ModelSerializer): + 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) + 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"] + messageModel = Message.objects.create( + text=message, + author=User.objects.get(tg_id=author_tg_nickname), + chat=Chat.objects.get(tg_id=chat_id), + ) + 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) + startMessage = CharField(write_only=True) + 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'] + startMessage = validated_data['startMessage'] + api_key = validated_data['api_key'] + tg_id = validated_data['tg_id'] + admin_tg_id = validated_data['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 diff --git a/api/urls.py b/api/urls.py index 3e0f62f..cf6704a 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,10 +1,18 @@ from django.urls import path -from .views import ListCreateMessage, ListCreateChat, RetrieveUpdateMessage, RetrieveUpdateDestroyChat +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 +router = DefaultRouter() + +router.register('user', UserViewSet, basename='user') +router.register('message', MessageViewSet, basename='message') +router.register('chat', ChatViewSet, basename='chat') urlpatterns = [ - path("chat", ListCreateChat.as_view()), - path("message", ListCreateMessage.as_view()), - path("chat/", RetrieveUpdateDestroyChat.as_view()), - path("message/", RetrieveUpdateMessage.as_view()) + path("message-from-username", CreateMessageFromUsername.as_view()), + path("chat-from-username", CreateChatWithUsername.as_view()) ] +urlpatterns.extend(router.urls) diff --git a/api/views.py b/api/views.py index f6a877a..a516b0b 100644 --- a/api/views.py +++ b/api/views.py @@ -1,6 +1,8 @@ -from .serializers import ChatSerializer, MessageSerializer +from .serializers import ChatSerializer, MessageSerializer, UserSerializer, UserChatSerializer, \ + MessageCreateWithUsername, UserHyperlinkedSerializer, ChatCreateWithTg from chat_models.models import Chat, Message -from rest_framework.generics import ListCreateAPIView, RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView +from rest_framework.generics import ListCreateAPIView, RetrieveUpdateAPIView, CreateAPIView +from users.models import User, UserChat class ListCreateMessage(ListCreateAPIView): @@ -21,3 +23,31 @@ class RetrieveUpdateMessage(RetrieveUpdateAPIView): class RetrieveUpdateDestroyChat(RetrieveUpdateAPIView): queryset = Chat.objects.all() serializer_class = ChatSerializer + + +class ListCreateUser(ListCreateAPIView): + queryset = User.objects.all() + serializer_class = UserHyperlinkedSerializer + + +class ListCreateUserChat(ListCreateAPIView): + queryset = UserChat.objects.all() + serializer_class = UserHyperlinkedSerializer + + +class RetrieveUpdateUserChat(RetrieveUpdateAPIView): + queryset = UserChat.objects.all() + serializer_class = UserHyperlinkedSerializer + + +class RetrieveUpdateUser(RetrieveUpdateAPIView): + queryset = User.objects.all() + serializer_class = UserSerializer + + +class CreateMessageFromUsername(CreateAPIView): + serializer_class = MessageCreateWithUsername + + +class CreateChatWithUsername(CreateAPIView): + serializer_class = ChatCreateWithTg \ No newline at end of file diff --git a/api/viewsets.py b/api/viewsets.py new file mode 100644 index 0000000..70b92f8 --- /dev/null +++ b/api/viewsets.py @@ -0,0 +1,22 @@ +from rest_framework import viewsets +from users.models import User +from chat_models.models import Message, Chat +from .serializers import UserSerializer, MessageSerializer, ChatSerializer + + +class UserViewSet(viewsets.ModelViewSet): + serializer_class = UserSerializer + lookup_field = 'tg_id' + queryset = User.objects.all() + + +class MessageViewSet(viewsets.ModelViewSet): + serializer_class = MessageSerializer + lookup_field = 'tg_id' + queryset = Message.objects.all() + + +class ChatViewSet(viewsets.ModelViewSet): + serializer_class = ChatSerializer + lookup_field = 'tg_id' + queryset = Chat.objects.all() diff --git a/chat_models/__pycache__/models.cpython-39.pyc b/chat_models/__pycache__/models.cpython-39.pyc index 0a5465f..c406710 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/0002_message_time_created.py b/chat_models/migrations/0002_message_time_created.py new file mode 100644 index 0000000..5542ec8 --- /dev/null +++ b/chat_models/migrations/0002_message_time_created.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-03-19 12:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='message', + name='time_created', + field=models.DateTimeField(auto_now_add=True, null=True), + ), + ] diff --git a/chat_models/migrations/0003_alter_message_author.py b/chat_models/migrations/0003_alter_message_author.py new file mode 100644 index 0000000..6c878f7 --- /dev/null +++ b/chat_models/migrations/0003_alter_message_author.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0.3 on 2022-03-19 12:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0002_alter_user_joined'), + ('chat_models', '0002_message_time_created'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='author', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='users.user'), + ), + ] diff --git a/chat_models/migrations/0004_auto_20220319_1617.py b/chat_models/migrations/0004_auto_20220319_1617.py new file mode 100644 index 0000000..3937fec --- /dev/null +++ b/chat_models/migrations/0004_auto_20220319_1617.py @@ -0,0 +1,30 @@ +# Generated by Django 3.1.7 on 2022-03-19 13:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0003_auto_20220319_1617'), + ('chat_models', '0003_alter_message_author'), + ] + + operations = [ + migrations.AddField( + model_name='chat', + name='admin', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='users.user'), + ), + migrations.AlterField( + model_name='chat', + name='id', + field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.AlterField( + model_name='message', + name='id', + field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + ] diff --git a/chat_models/migrations/0005_message_tg_id.py b/chat_models/migrations/0005_message_tg_id.py new file mode 100644 index 0000000..a7a8838 --- /dev/null +++ b/chat_models/migrations/0005_message_tg_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 15:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0004_auto_20220319_1617'), + ] + + operations = [ + migrations.AddField( + model_name='message', + name='tg_id', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/chat_models/migrations/0006_chat_tg_id.py b/chat_models/migrations/0006_chat_tg_id.py new file mode 100644 index 0000000..b6c0672 --- /dev/null +++ b/chat_models/migrations/0006_chat_tg_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 15:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('chat_models', '0005_message_tg_id'), + ] + + operations = [ + migrations.AddField( + model_name='chat', + name='tg_id', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/chat_models/migrations/__pycache__/0002_message_time_created.cpython-39.pyc b/chat_models/migrations/__pycache__/0002_message_time_created.cpython-39.pyc new file mode 100644 index 0000000..ed5885f Binary files /dev/null and b/chat_models/migrations/__pycache__/0002_message_time_created.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0003_alter_message_author.cpython-39.pyc b/chat_models/migrations/__pycache__/0003_alter_message_author.cpython-39.pyc new file mode 100644 index 0000000..783363e Binary files /dev/null and b/chat_models/migrations/__pycache__/0003_alter_message_author.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0004_auto_20220319_1617.cpython-39.pyc b/chat_models/migrations/__pycache__/0004_auto_20220319_1617.cpython-39.pyc new file mode 100644 index 0000000..4a8fdcc Binary files /dev/null and b/chat_models/migrations/__pycache__/0004_auto_20220319_1617.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0005_message_tg_id.cpython-39.pyc b/chat_models/migrations/__pycache__/0005_message_tg_id.cpython-39.pyc new file mode 100644 index 0000000..0784446 Binary files /dev/null and b/chat_models/migrations/__pycache__/0005_message_tg_id.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/0006_chat_tg_id.cpython-39.pyc b/chat_models/migrations/__pycache__/0006_chat_tg_id.cpython-39.pyc new file mode 100644 index 0000000..a3b2507 Binary files /dev/null and b/chat_models/migrations/__pycache__/0006_chat_tg_id.cpython-39.pyc differ diff --git a/chat_models/models.py b/chat_models/models.py index 2788974..0ed853b 100644 --- a/chat_models/models.py +++ b/chat_models/models.py @@ -5,9 +5,13 @@ 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) class Message(models.Model): text = models.TextField() - author = models.TextField() + author = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True) chat = models.ForeignKey(Chat, on_delete=models.CASCADE) + time_created = models.DateTimeField(auto_now_add=True, null=True) + tg_id = models.CharField(null=True, max_length=100) diff --git a/hackbackend/__pycache__/settings.cpython-39.pyc b/hackbackend/__pycache__/settings.cpython-39.pyc index 84971fc..08243c7 100644 Binary files a/hackbackend/__pycache__/settings.cpython-39.pyc and b/hackbackend/__pycache__/settings.cpython-39.pyc differ diff --git a/hackbackend/settings.py b/hackbackend/settings.py index c8079df..0837de5 100644 --- a/hackbackend/settings.py +++ b/hackbackend/settings.py @@ -29,7 +29,6 @@ ALLOWED_HOSTS = ['*'] # Application definition - INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', @@ -37,15 +36,19 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'corsheaders', 'rest_framework', 'chat_models', - 'api' + 'api', + 'users', + ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', + 'corsheaders.middleware.CorsMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', @@ -82,6 +85,7 @@ DATABASES = { 'NAME': BASE_DIR / 'db.sqlite3', } } +CORS_ORIGIN_ALLOW_ALL = True # Password validation diff --git a/requirements.txt b/requirements.txt index d122001..7b17995 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ asgiref==3.5.0 Django==4.0.3 +django-cors-headers==3.11.0 djangorestframework==3.13.1 pytz==2021.3 sqlparse==0.4.2 diff --git a/users/__init__.py b/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/users/__pycache__/__init__.cpython-39.pyc b/users/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..2b49a3e Binary files /dev/null and b/users/__pycache__/__init__.cpython-39.pyc differ diff --git a/users/__pycache__/admin.cpython-39.pyc b/users/__pycache__/admin.cpython-39.pyc new file mode 100644 index 0000000..6bbafa6 Binary files /dev/null and b/users/__pycache__/admin.cpython-39.pyc differ diff --git a/users/__pycache__/apps.cpython-39.pyc b/users/__pycache__/apps.cpython-39.pyc new file mode 100644 index 0000000..384a9fa Binary files /dev/null and b/users/__pycache__/apps.cpython-39.pyc differ diff --git a/users/__pycache__/models.cpython-39.pyc b/users/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..ac52b5f Binary files /dev/null and b/users/__pycache__/models.cpython-39.pyc differ diff --git a/users/admin.py b/users/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/users/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/users/apps.py b/users/apps.py new file mode 100644 index 0000000..72b1401 --- /dev/null +++ b/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users' diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py new file mode 100644 index 0000000..ca0827a --- /dev/null +++ b/users/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.0.3 on 2022-03-19 12:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('chat_models', '0002_message_time_created'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('username', models.TextField()), + ('joined', models.DateTimeField()), + ], + ), + migrations.CreateModel( + name='UserChat', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('chat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='chat_models.chat')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.user')), + ], + ), + ] diff --git a/users/migrations/0002_alter_user_joined.py b/users/migrations/0002_alter_user_joined.py new file mode 100644 index 0000000..1bc4ce5 --- /dev/null +++ b/users/migrations/0002_alter_user_joined.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-03-19 12:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='joined', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/users/migrations/0003_auto_20220319_1617.py b/users/migrations/0003_auto_20220319_1617.py new file mode 100644 index 0000000..4e12c3e --- /dev/null +++ b/users/migrations/0003_auto_20220319_1617.py @@ -0,0 +1,23 @@ +# Generated by Django 3.1.7 on 2022-03-19 13:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0002_alter_user_joined'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='id', + field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.AlterField( + model_name='userchat', + name='id', + field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + ] diff --git a/users/migrations/0004_user_tg_id.py b/users/migrations/0004_user_tg_id.py new file mode 100644 index 0000000..22bb437 --- /dev/null +++ b/users/migrations/0004_user_tg_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 13:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0003_auto_20220319_1617'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='tg_id', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/users/migrations/0005_auto_20220319_1853.py b/users/migrations/0005_auto_20220319_1853.py new file mode 100644 index 0000000..cfd40e2 --- /dev/null +++ b/users/migrations/0005_auto_20220319_1853.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2022-03-19 15:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0004_user_tg_id'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='tg_id', + field=models.CharField(max_length=100, null=True, unique=True), + ), + ] diff --git a/users/migrations/__init__.py b/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/users/migrations/__pycache__/0001_initial.cpython-39.pyc b/users/migrations/__pycache__/0001_initial.cpython-39.pyc new file mode 100644 index 0000000..ad10d20 Binary files /dev/null and b/users/migrations/__pycache__/0001_initial.cpython-39.pyc differ diff --git a/users/migrations/__pycache__/0002_alter_user_joined.cpython-39.pyc b/users/migrations/__pycache__/0002_alter_user_joined.cpython-39.pyc new file mode 100644 index 0000000..dabd9f8 Binary files /dev/null and b/users/migrations/__pycache__/0002_alter_user_joined.cpython-39.pyc differ diff --git a/users/migrations/__pycache__/0003_auto_20220319_1617.cpython-39.pyc b/users/migrations/__pycache__/0003_auto_20220319_1617.cpython-39.pyc new file mode 100644 index 0000000..c7303d9 Binary files /dev/null and b/users/migrations/__pycache__/0003_auto_20220319_1617.cpython-39.pyc differ diff --git a/users/migrations/__pycache__/0004_user_tg_id.cpython-39.pyc b/users/migrations/__pycache__/0004_user_tg_id.cpython-39.pyc new file mode 100644 index 0000000..ac4a018 Binary files /dev/null and b/users/migrations/__pycache__/0004_user_tg_id.cpython-39.pyc differ diff --git a/users/migrations/__pycache__/0005_auto_20220319_1853.cpython-39.pyc b/users/migrations/__pycache__/0005_auto_20220319_1853.cpython-39.pyc new file mode 100644 index 0000000..5f2d215 Binary files /dev/null and b/users/migrations/__pycache__/0005_auto_20220319_1853.cpython-39.pyc differ diff --git a/users/migrations/__pycache__/__init__.cpython-39.pyc b/users/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..ebb5e49 Binary files /dev/null and b/users/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/users/models.py b/users/models.py new file mode 100644 index 0000000..c10a448 --- /dev/null +++ b/users/models.py @@ -0,0 +1,20 @@ +from django.db import models +from chat_models.models import Chat + + +class User(models.Model): + username = models.TextField() + joined = models.DateTimeField(auto_now_add=True) + tg_id = models.CharField(max_length=100, null=True, unique=True) + + def __str__(self): + return "User({}, {})".format(self.username, self.tg_id) + + +class UserChat(models.Model): + chat = models.ForeignKey(Chat, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) + + def __str__(self): + return "Chat rel with {} chat and {} user".format(self.chat, self.user) + diff --git a/users/tests.py b/users/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/users/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/users/views.py b/users/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/users/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.