ЛР 1. Трёхуровневая клиент-серверная архитектура
This commit is contained in:
commit
36e412545c
18 changed files with 294 additions and 0 deletions
1
lab1/http.restbook
Normal file
1
lab1/http.restbook
Normal file
File diff suppressed because one or more lines are too long
0
lab1/lab1/__init__.py
Normal file
0
lab1/lab1/__init__.py
Normal file
16
lab1/lab1/asgi.py
Normal file
16
lab1/lab1/asgi.py
Normal file
|
|
@ -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()
|
||||||
132
lab1/lab1/settings.py
Normal file
132
lab1/lab1/settings.py
Normal file
|
|
@ -0,0 +1,132 @@
|
||||||
|
"""
|
||||||
|
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 = ['*']
|
||||||
|
|
||||||
|
|
||||||
|
# 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', '03042004'),
|
||||||
|
'HOST': os.environ.get('POSTGRES_HOST', 'localhost'),
|
||||||
|
'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'
|
||||||
13
lab1/lab1/urls.py
Normal file
13
lab1/lab1/urls.py
Normal file
|
|
@ -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)),
|
||||||
|
]
|
||||||
|
|
||||||
16
lab1/lab1/wsgi.py
Normal file
16
lab1/lab1/wsgi.py
Normal file
|
|
@ -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()
|
||||||
22
lab1/manage.py
Normal file
22
lab1/manage.py
Normal file
|
|
@ -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()
|
||||||
0
lab1/quiz/__init__.py
Normal file
0
lab1/quiz/__init__.py
Normal file
4
lab1/quiz/admin.py
Normal file
4
lab1/quiz/admin.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
from .models import Quiz
|
||||||
|
|
||||||
|
admin.site.register(Quiz)
|
||||||
5
lab1/quiz/apps.py
Normal file
5
lab1/quiz/apps.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class QuizConfig(AppConfig):
|
||||||
|
name = 'quiz'
|
||||||
18
lab1/quiz/gentestdata.py
Normal file
18
lab1/quiz/gentestdata.py
Normal file
|
|
@ -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('Сделана генерация')
|
||||||
26
lab1/quiz/migrations/0001_initial.py
Normal file
26
lab1/quiz/migrations/0001_initial.py
Normal file
|
|
@ -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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
0
lab1/quiz/migrations/__init__.py
Normal file
0
lab1/quiz/migrations/__init__.py
Normal file
15
lab1/quiz/models.py
Normal file
15
lab1/quiz/models.py
Normal file
|
|
@ -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
|
||||||
|
|
||||||
7
lab1/quiz/serializers.py
Normal file
7
lab1/quiz/serializers.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
from .models import Quiz
|
||||||
|
|
||||||
|
class QuizSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Quiz
|
||||||
|
fields = '__all__'
|
||||||
3
lab1/quiz/tests.py
Normal file
3
lab1/quiz/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
9
lab1/quiz/views.py
Normal file
9
lab1/quiz/views.py
Normal file
|
|
@ -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
|
||||||
7
lab1/requirements.txt
Normal file
7
lab1/requirements.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
djangorestframework
|
||||||
|
markdown
|
||||||
|
django-filter
|
||||||
|
django-cors-headers
|
||||||
|
psycopg[binary,pool]
|
||||||
|
faker
|
||||||
|
gunicorn
|
||||||
Loading…
Add table
Reference in a new issue