-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
219 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
hinghwa-dict-backend/quiz/migrations/0004_auto_20231019_1116.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Generated by Django 3.1.14 on 2023-10-19 03:16 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("quiz", "0003_quiz_voice_source"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Paper", | ||
fields=[ | ||
( | ||
"title", | ||
models.CharField(blank=True, max_length=50, verbose_name="试卷标题"), | ||
), | ||
("quantity", models.IntegerField(blank=True, verbose_name="题目数量")), | ||
( | ||
"id", | ||
models.CharField( | ||
max_length=20, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name="quiz", | ||
name="type", | ||
field=models.CharField(blank=True, max_length=50, verbose_name="问题类型"), | ||
), | ||
migrations.CreateModel( | ||
name="Record", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("timestamp", models.DateTimeField(blank=True, verbose_name="时间")), | ||
( | ||
"correct_answer", | ||
models.IntegerField(blank=True, verbose_name="答对数量"), | ||
), | ||
( | ||
"exam", | ||
models.ManyToManyField( | ||
related_name="exam_record", to="quiz.Paper", verbose_name="答卷记录" | ||
), | ||
), | ||
( | ||
"user_answer", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="user", | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name="词单作者", | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name="paper", | ||
name="quizzes", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="exam_questions", | ||
to="quiz.Quiz", | ||
verbose_name="问题列表", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from ...models import Quiz, Paper, Record | ||
from .record_all import record_all | ||
from quiz.dto.quiz_all import quiz_all | ||
|
||
|
||
# 返回问卷信息 | ||
def paper_all(paper: Paper): | ||
response = { | ||
"id": paper.id, | ||
"title": paper.title, | ||
"quantity": paper.quantity, | ||
# "record": [record_all(x) for x in paper.record.all()] | ||
"quizzes": [quiz_all(quiz) for quiz in paper.quizzes.all()], | ||
} | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ...models import Record | ||
|
||
|
||
def record_all(record: Record): | ||
response = {"timestamp": record.timestamp, "correct_answer": record.correct_answer} | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.http import JsonResponse | ||
import demjson | ||
from ..dto.paper_all import paper_all | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from ...models import Quiz, Paper, Record | ||
from utils.exception.types.bad_request import InsufficientQuiz | ||
from utils.token import token_pass, token_user | ||
from django.utils import timezone | ||
from utils.generate_id import generate_paper_id | ||
|
||
|
||
class ManageAllPaper(View): | ||
# QZ0203 测试题组卷 | ||
@csrf_exempt | ||
def post(self, request): | ||
token_pass(request.headers, -1) | ||
number = int(request.GET["number"]) | ||
body = demjson.decode(request.body) | ||
title = body["title"] | ||
request_type = request.GET["type"] | ||
type_list = request_type.split(",") | ||
quizzes = Quiz.objects.filter(type__in=type_list).order_by("?")[:number] | ||
if len(quizzes) != number: | ||
raise InsufficientQuiz() | ||
paper = Paper() | ||
paper.quantity = number | ||
paper.id = generate_paper_id() | ||
paper.title = title | ||
paper.save() | ||
for quiz in quizzes: | ||
paper.quizzes.add(quiz) | ||
return JsonResponse(paper_all(paper), status=200) | ||
|
||
# QZ0204 查询所有试卷 | ||
@csrf_exempt | ||
def get(self, request): | ||
# token_pass(request.headers, -1) | ||
total_papers = Paper.objects.all() | ||
result = [] | ||
for paper in total_papers: | ||
result.append(paper_all(paper)) | ||
return JsonResponse({"total": len(result), "paper": result}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import demjson | ||
from django.http import JsonResponse | ||
from ..dto.paper_all import paper_all | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from ...models import Quiz, Paper, Record | ||
from utils.exception.types.bad_request import InsufficientQuiz | ||
from utils.exception.types.not_found import PaperNotFoundException | ||
|
||
|
||
class ManageSinglePaper(View): | ||
# QZ0205 查询特定试卷 | ||
@csrf_exempt | ||
def get(self, request, paper_id) -> JsonResponse: | ||
paper = Paper.objects.filter(id=paper_id) | ||
if not paper.exists(): | ||
raise PaperNotFoundException() | ||
paper = paper[0] | ||
return JsonResponse(paper_all(paper), status=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.