diff --git a/lab-2/local/backend/lab1/__init__.py b/lab-2/local/backend/lab1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab-2/local/backend/lab1/asgi.py b/lab-2/local/backend/lab1/asgi.py new file mode 100644 index 0000000..1351cca --- /dev/null +++ b/lab-2/local/backend/lab1/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for lab1 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/6.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lab1.settings') + +application = get_asgi_application() diff --git a/lab-2/local/backend/lab1/settings.py b/lab-2/local/backend/lab1/settings.py new file mode 100644 index 0000000..91da8aa --- /dev/null +++ b/lab-2/local/backend/lab1/settings.py @@ -0,0 +1,133 @@ +""" +Django settings for lab1 project. + +Generated by 'django-admin startproject' using Django 6.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/6.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/6.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure--xqwhdo2$)n*y3yk=d*vmwx6nuwt8h=m5@_og_uzj+11^hu7))' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] +CSRF_TRUSTED_ORIGINS = ['http://localhost'] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'rest_framework', + 'quiz', +] + +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 = 'lab1.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'lab1.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/6.0/ref/settings/#databases + +import os + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.environ.get('POSTGRES_DB', 'lab_db'), + 'USER': os.environ.get('POSTGRES_USER', 'django'), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'password'), + 'HOST': os.environ.get('POSTGRES_HOST', 'postgres_service'), + 'PORT': os.environ.get('POSTGRES_PORT', 5432), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/6.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/6.0/topics/i18n/ + +LANGUAGE_CODE = 'ru-ru' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/6.0/howto/static-files/ + +STATIC_URL = 'static/' +STATIC_ROOT = BASE_DIR / 'staticfiles' + +# Default primary key field type +# https://docs.djangoproject.com/en/6.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/lab-2/local/backend/lab1/urls.py b/lab-2/local/backend/lab1/urls.py new file mode 100644 index 0000000..e617532 --- /dev/null +++ b/lab-2/local/backend/lab1/urls.py @@ -0,0 +1,13 @@ +from django.contrib import admin +from django.urls import path, include +from rest_framework import routers +from quiz.views import QuizViewSet + +router = routers.DefaultRouter() +router.register(r'quiz', QuizViewSet) + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', include(router.urls)), +] + diff --git a/lab-2/local/backend/lab1/wsgi.py b/lab-2/local/backend/lab1/wsgi.py new file mode 100644 index 0000000..4716070 --- /dev/null +++ b/lab-2/local/backend/lab1/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for lab1 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/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lab1.settings') + +application = get_wsgi_application() diff --git a/lab-2/local/backend/manage.py b/lab-2/local/backend/manage.py new file mode 100644 index 0000000..cfc0053 --- /dev/null +++ b/lab-2/local/backend/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', 'lab1.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/lab-2/local/backend/quiz/__init__.py b/lab-2/local/backend/quiz/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab-2/local/backend/quiz/admin.py b/lab-2/local/backend/quiz/admin.py new file mode 100644 index 0000000..3847636 --- /dev/null +++ b/lab-2/local/backend/quiz/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Quiz + +admin.site.register(Quiz) diff --git a/lab-2/local/backend/quiz/apps.py b/lab-2/local/backend/quiz/apps.py new file mode 100644 index 0000000..eaa49da --- /dev/null +++ b/lab-2/local/backend/quiz/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class QuizConfig(AppConfig): + name = 'quiz' diff --git a/lab-2/local/backend/quiz/gentestdata.py b/lab-2/local/backend/quiz/gentestdata.py new file mode 100644 index 0000000..cd9d892 --- /dev/null +++ b/lab-2/local/backend/quiz/gentestdata.py @@ -0,0 +1,18 @@ +import random +from .models import Quiz +from django.db import transaction +from faker import Faker + +fk = Faker() + +def gentestdata(): + with transaction.atomic(): + for i in range(100): + new_quiz = Quiz() + new_quiz.title = fk.sentence(nb_words=4) + new_quiz.description = fk.paragraph() + new_quiz.author = fk.name() + new_quiz.time_limit = random.randint(5, 60) + new_quiz.is_published = random.random() > 0.5 + new_quiz.save() + print('Сделана генерация') diff --git a/lab-2/local/backend/quiz/migrations/0001_initial.py b/lab-2/local/backend/quiz/migrations/0001_initial.py new file mode 100644 index 0000000..6e07c29 --- /dev/null +++ b/lab-2/local/backend/quiz/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 6.0.3 on 2026-03-05 08:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Quiz', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('description', models.TextField()), + ('author', models.CharField(max_length=100)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('time_limit', models.IntegerField(help_text='Ограничение по времени в минутах')), + ('is_published', models.BooleanField(default=False)), + ], + ), + ] diff --git a/lab-2/local/backend/quiz/migrations/__init__.py b/lab-2/local/backend/quiz/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab-2/local/backend/quiz/models.py b/lab-2/local/backend/quiz/models.py new file mode 100644 index 0000000..3936c59 --- /dev/null +++ b/lab-2/local/backend/quiz/models.py @@ -0,0 +1,15 @@ +from django.db import models + +# Create your models here. + +class Quiz(models.Model): + title = models.CharField(max_length=200) + description = models.TextField() + author = models.CharField(max_length=100) + created_at = models.DateTimeField(auto_now_add=True) + time_limit = models.IntegerField(help_text='Ограничение по времени в минутах') + is_published = models.BooleanField(default=False) + + def __str__(self): + return self.title + diff --git a/lab-2/local/backend/quiz/serializers.py b/lab-2/local/backend/quiz/serializers.py new file mode 100644 index 0000000..08dac96 --- /dev/null +++ b/lab-2/local/backend/quiz/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from .models import Quiz + +class QuizSerializer(serializers.ModelSerializer): + class Meta: + model = Quiz + fields = '__all__' \ No newline at end of file diff --git a/lab-2/local/backend/quiz/tests.py b/lab-2/local/backend/quiz/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/lab-2/local/backend/quiz/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/lab-2/local/backend/quiz/views.py b/lab-2/local/backend/quiz/views.py new file mode 100644 index 0000000..a7c4b53 --- /dev/null +++ b/lab-2/local/backend/quiz/views.py @@ -0,0 +1,9 @@ +from django.shortcuts import render + +from rest_framework import viewsets +from .models import Quiz +from .serializers import QuizSerializer + +class QuizViewSet(viewsets.ModelViewSet): + queryset = Quiz.objects.all() + serializer_class = QuizSerializer diff --git a/lab-2/local/backend/requirements.txt b/lab-2/local/backend/requirements.txt new file mode 100644 index 0000000..c40c3d2 --- /dev/null +++ b/lab-2/local/backend/requirements.txt @@ -0,0 +1,8 @@ +Django +djangorestframework +markdown +django-filter +django-cors-headers +psycopg[binary,pool] +faker +gunicorn diff --git a/lab-2/local/docker-compose.yaml b/lab-2/local/docker-compose.yaml new file mode 100644 index 0000000..d080e0b --- /dev/null +++ b/lab-2/local/docker-compose.yaml @@ -0,0 +1,45 @@ +name: lab2-local + +services: + postgres_service: + # https://hub.docker.com/_/postgres + image: postgres:alpine + env_file: + - .env + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 10s + timeout: 30s + retries: 5 + volumes: + - postgres_data:/var/lib/postgresql/data + + backend_service: + build: ./backend + command: > # python manage.py createsuperuser --noinput && + sh -c "python manage.py migrate && + python manage.py shell -c 'from quiz.gentestdata import gentestdata; gentestdata()' && + python manage.py collectstatic --no-input && + gunicorn lab1.wsgi:application --bind 0.0.0.0:8000" + volumes: + - static_volume:/app/staticfiles + env_file: + - .env + depends_on: + postgres_service: + condition: service_healthy + restart: unless-stopped + + nginx_service: + build: ./nginx + ports: + - "80:80" + volumes: + - static_volume:/staticfiles + depends_on: + - backend_service + restart: unless-stopped + +volumes: + static_volume: + postgres_data: