commit 5856b27dcd4dc8a999ec01f1daa57ef1fc4a6506 Author: ilia Date: Fri Mar 18 18:13:36 2022 +0300 add api diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6324398 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +hack +db.sqlite3 +.idea +.code \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/hackbackend.iml b/.idea/hackbackend.iml new file mode 100644 index 0000000..4f2c9af --- /dev/null +++ b/.idea/hackbackend.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..19b5037 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,170 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f6104af --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..78a355a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/__pycache__/__init__.cpython-39.pyc b/api/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..472a3a6 Binary files /dev/null and b/api/__pycache__/__init__.cpython-39.pyc differ diff --git a/api/__pycache__/admin.cpython-39.pyc b/api/__pycache__/admin.cpython-39.pyc new file mode 100644 index 0000000..26d24db Binary files /dev/null and b/api/__pycache__/admin.cpython-39.pyc differ diff --git a/api/__pycache__/apps.cpython-39.pyc b/api/__pycache__/apps.cpython-39.pyc new file mode 100644 index 0000000..3b1d73f Binary files /dev/null and b/api/__pycache__/apps.cpython-39.pyc differ diff --git a/api/__pycache__/models.cpython-39.pyc b/api/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..ccec8f2 Binary files /dev/null and b/api/__pycache__/models.cpython-39.pyc differ diff --git a/api/__pycache__/serializers.cpython-39.pyc b/api/__pycache__/serializers.cpython-39.pyc new file mode 100644 index 0000000..9f61493 Binary files /dev/null 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 new file mode 100644 index 0000000..3b413c3 Binary files /dev/null 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 new file mode 100644 index 0000000..08740a0 Binary files /dev/null and b/api/__pycache__/views.cpython-39.pyc differ diff --git a/api/admin.py b/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/api/apps.py b/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'api' diff --git a/api/migrations/__init__.py b/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/migrations/__pycache__/__init__.cpython-39.pyc b/api/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..839aad7 Binary files /dev/null and b/api/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/api/models.py b/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 0000000..a6d0943 --- /dev/null +++ b/api/serializers.py @@ -0,0 +1,14 @@ +from rest_framework.serializers import ModelSerializer +from chat_models.models import Chat, Message + + +class ChatSerializer(ModelSerializer): + class Meta: + model = Chat + fields = "__all__" + + +class MessageSerializer(ModelSerializer): + class Meta: + model = Message + fields = "__all__" diff --git a/api/tests.py b/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..3e0f62f --- /dev/null +++ b/api/urls.py @@ -0,0 +1,10 @@ +from django.urls import path +from .views import ListCreateMessage, ListCreateChat, RetrieveUpdateMessage, RetrieveUpdateDestroyChat + + +urlpatterns = [ + path("chat", ListCreateChat.as_view()), + path("message", ListCreateMessage.as_view()), + path("chat/", RetrieveUpdateDestroyChat.as_view()), + path("message/", RetrieveUpdateMessage.as_view()) +] diff --git a/api/views.py b/api/views.py new file mode 100644 index 0000000..f6a877a --- /dev/null +++ b/api/views.py @@ -0,0 +1,23 @@ +from .serializers import ChatSerializer, MessageSerializer +from chat_models.models import Chat, Message +from rest_framework.generics import ListCreateAPIView, RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView + + +class ListCreateMessage(ListCreateAPIView): + serializer_class = MessageSerializer + queryset = Message.objects.all() + + +class ListCreateChat(ListCreateAPIView): + serializer_class = ChatSerializer + queryset = Chat.objects.all() + + +class RetrieveUpdateMessage(RetrieveUpdateAPIView): + queryset = Message.objects.all() + serializer_class = MessageSerializer + + +class RetrieveUpdateDestroyChat(RetrieveUpdateAPIView): + queryset = Chat.objects.all() + serializer_class = ChatSerializer diff --git a/chat_models/__init__.py b/chat_models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chat_models/__pycache__/__init__.cpython-39.pyc b/chat_models/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..eb6258e Binary files /dev/null and b/chat_models/__pycache__/__init__.cpython-39.pyc differ diff --git a/chat_models/__pycache__/admin.cpython-39.pyc b/chat_models/__pycache__/admin.cpython-39.pyc new file mode 100644 index 0000000..7e12676 Binary files /dev/null and b/chat_models/__pycache__/admin.cpython-39.pyc differ diff --git a/chat_models/__pycache__/apps.cpython-39.pyc b/chat_models/__pycache__/apps.cpython-39.pyc new file mode 100644 index 0000000..d455d84 Binary files /dev/null and b/chat_models/__pycache__/apps.cpython-39.pyc differ diff --git a/chat_models/__pycache__/models.cpython-39.pyc b/chat_models/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..0a5465f Binary files /dev/null and b/chat_models/__pycache__/models.cpython-39.pyc differ diff --git a/chat_models/admin.py b/chat_models/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/chat_models/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/chat_models/apps.py b/chat_models/apps.py new file mode 100644 index 0000000..98c427b --- /dev/null +++ b/chat_models/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChatModelsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'chat_models' diff --git a/chat_models/migrations/0001_initial.py b/chat_models/migrations/0001_initial.py new file mode 100644 index 0000000..4a40084 --- /dev/null +++ b/chat_models/migrations/0001_initial.py @@ -0,0 +1,33 @@ +# Generated by Django 4.0.3 on 2022-03-18 15:07 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Chat', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.TextField()), + ('start_message', models.TextField(blank=True)), + ('api_key', models.TextField()), + ], + ), + migrations.CreateModel( + name='Message', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.TextField()), + ('author', models.TextField()), + ('chat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='chat_models.chat')), + ], + ), + ] diff --git a/chat_models/migrations/__init__.py b/chat_models/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chat_models/migrations/__pycache__/0001_initial.cpython-39.pyc b/chat_models/migrations/__pycache__/0001_initial.cpython-39.pyc new file mode 100644 index 0000000..59963ba Binary files /dev/null and b/chat_models/migrations/__pycache__/0001_initial.cpython-39.pyc differ diff --git a/chat_models/migrations/__pycache__/__init__.cpython-39.pyc b/chat_models/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..28cc9f5 Binary files /dev/null and b/chat_models/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/chat_models/models.py b/chat_models/models.py new file mode 100644 index 0000000..2788974 --- /dev/null +++ b/chat_models/models.py @@ -0,0 +1,13 @@ +from django.db import models + + +class Chat(models.Model): + name = models.TextField() + start_message = models.TextField(blank=True) + api_key = models.TextField() + + +class Message(models.Model): + text = models.TextField() + author = models.TextField() + chat = models.ForeignKey(Chat, on_delete=models.CASCADE) diff --git a/chat_models/tests.py b/chat_models/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/chat_models/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/chat_models/views.py b/chat_models/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/chat_models/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/hackbackend/__init__.py b/hackbackend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hackbackend/__pycache__/__init__.cpython-39.pyc b/hackbackend/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..32a8217 Binary files /dev/null and b/hackbackend/__pycache__/__init__.cpython-39.pyc differ diff --git a/hackbackend/__pycache__/settings.cpython-39.pyc b/hackbackend/__pycache__/settings.cpython-39.pyc new file mode 100644 index 0000000..84971fc Binary files /dev/null and b/hackbackend/__pycache__/settings.cpython-39.pyc differ diff --git a/hackbackend/__pycache__/urls.cpython-39.pyc b/hackbackend/__pycache__/urls.cpython-39.pyc new file mode 100644 index 0000000..61c3e49 Binary files /dev/null and b/hackbackend/__pycache__/urls.cpython-39.pyc differ diff --git a/hackbackend/__pycache__/wsgi.cpython-39.pyc b/hackbackend/__pycache__/wsgi.cpython-39.pyc new file mode 100644 index 0000000..36cbd88 Binary files /dev/null and b/hackbackend/__pycache__/wsgi.cpython-39.pyc differ diff --git a/hackbackend/asgi.py b/hackbackend/asgi.py new file mode 100644 index 0000000..1c04f9c --- /dev/null +++ b/hackbackend/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for hackbackend project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hackbackend.settings') + +application = get_asgi_application() diff --git a/hackbackend/settings.py b/hackbackend/settings.py new file mode 100644 index 0000000..c8079df --- /dev/null +++ b/hackbackend/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for hackbackend project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-6(#2x^yq=plz6ou!=_8wke%trs_+*80$vuorx4ybh@dhx7xpzg' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'chat_models', + 'api' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'hackbackend.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'hackbackend.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/hackbackend/urls.py b/hackbackend/urls.py new file mode 100644 index 0000000..175bbe4 --- /dev/null +++ b/hackbackend/urls.py @@ -0,0 +1,22 @@ +"""hackbackend URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', include("api.urls")) +] diff --git a/hackbackend/wsgi.py b/hackbackend/wsgi.py new file mode 100644 index 0000000..529fa08 --- /dev/null +++ b/hackbackend/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for hackbackend project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hackbackend.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..503a76e --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hackbackend.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d122001 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +asgiref==3.5.0 +Django==4.0.3 +djangorestframework==3.13.1 +pytz==2021.3 +sqlparse==0.4.2 +tzdata==2022.1