lab-1: add REST API endpoints

This commit is contained in:
DeevEV 2026-04-21 16:10:00 +03:00
parent c5ea3d0e44
commit 6634e796aa
6 changed files with 42 additions and 0 deletions

1
lab-1/http.restbook Normal file
View file

@ -0,0 +1 @@
[{"kind":2,"language":"rest-book","value":"GET http://localhost:8000/api/quiz/","outputs":[{"mime":"x-application/rest-book","value":{"status":200,"statusText":"OK","headers":{"Allow":"GET, HEAD, OPTIONS","Date":"Thu, 02 Apr 2026 11:20:40 GMT","Content-Type":"application/json","Content-Length":"37","Server":"nginx/1.29.7","X-Frame-Options":"DENY","Connection":"close"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","User-Agent":"axios/0.21.4"}},"request":{"method":"GET","httpVersion":"1.1","responseUrl":"http://localhost/api/quiz/","timeout":10000,"headers":{}},"data":{"quiz":"http://localhost/api/quiz/"}}},{"mime":"text/x-json","value":{"status":200,"statusText":"OK","headers":{"Allow":"GET, HEAD, OPTIONS","Date":"Thu, 02 Apr 2026 11:20:40 GMT","Content-Type":"application/json","Content-Length":"37","Server":"nginx/1.29.7","X-Frame-Options":"DENY","Connection":"close"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","User-Agent":"axios/0.21.4"}},"request":{"method":"GET","httpVersion":"1.1","responseUrl":"http://localhost/api/quiz/","timeout":10000,"headers":{}},"data":{"quiz":"http://localhost/api/quiz/"}}},{"mime":"text/html","value":"[object Object]"}]},{"kind":2,"language":"rest-book","value":"POST http://localhost:8000/api/quiz/\r\nContent-Type: application/json\r\n\r\n{\r\n \"title\": \"Название\",\r\n \"description\": \"Описание\",\r\n \"author\": \"Автор\",\r\n \"time_limit\": 40,\r\n \"is_published\": false\r\n}","outputs":[{"mime":"x-application/rest-book","value":{"status":405,"statusText":"Method Not Allowed","headers":{"Allow":"GET, HEAD, OPTIONS","Date":"Thu, 02 Apr 2026 11:20:56 GMT","Content-Type":"application/json","Content-Length":"55","Server":"nginx/1.29.7","X-Frame-Options":"DENY","Connection":"close"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"axios/0.21.4","Content-Length":120}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost/api/quiz/","timeout":10000,"headers":{"Content-Type":"application/json"},"data":{"title":"Название","description":"Описание","author":"Автор","time_limit":40,"is_published":false}},"data":{"detail":"Метод \"POST\" не разрешен."}}},{"mime":"text/x-json","value":{"status":405,"statusText":"Method Not Allowed","headers":{"Allow":"GET, HEAD, OPTIONS","Date":"Thu, 02 Apr 2026 11:20:56 GMT","Content-Type":"application/json","Content-Length":"55","Server":"nginx/1.29.7","X-Frame-Options":"DENY","Connection":"close"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"axios/0.21.4","Content-Length":120}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost/api/quiz/","timeout":10000,"headers":{"Content-Type":"application/json"},"data":{"title":"Название","description":"Описание","author":"Автор","time_limit":40,"is_published":false}},"data":{"detail":"Метод \"POST\" не разрешен."}}},{"mime":"text/html","value":"[object Object]"}]},{"kind":2,"language":"rest-book","value":"GET http://localhost:8000/api/quiz/8/","outputs":[{"mime":"application/vnd.code.notebook.error","value":{"name":"Error","message":"{}"}}]},{"kind":2,"language":"rest-book","value":"PUT http://localhost:8000/api/quiz/8/\r\nContent-Type: application/json\r\n\r\n{\r\n \"title\": \"Новое Название\",\r\n \"description\": \"Новое Описание\",\r\n \"author\": \"Новый Автор\",\r\n \"time_limit\": 40,\r\n \"is_published\": true\r\n}","outputs":[{"mime":"application/vnd.code.notebook.error","value":{"name":"Error","message":"{}"}}]},{"kind":2,"language":"rest-book","value":"DELETE http://localhost:8000/api/quiz/8/","outputs":[{"mime":"application/vnd.code.notebook.error","value":{"name":"Error","message":"{}"}}]}]

4
lab-1/quiz/admin.py Normal file
View file

@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Quiz
admin.site.register(Quiz)

18
lab-1/quiz/gentestdata.py Normal file
View 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('Сделана генерация')

View 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
lab-1/quiz/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

9
lab-1/quiz/views.py Normal file
View 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