Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(quiz): create QuizRecord #352

Merged
merged 26 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7bdf000
feat(quiz): create model PaperRecord
CapooL Nov 30, 2023
a43d643
feat(quiz): create dto and utils for PaperRecord
CapooL Nov 30, 2023
285497b
feat(quiz): write functions and urls
CapooL Nov 30, 2023
84df0fe
test(quiz): add test files
CapooL Nov 30, 2023
92a8ddf
style(quiz): reformat for black
CapooL Nov 30, 2023
144567d
Update models.py
CapooL Nov 30, 2023
c069685
refactor(words): rename variable
CapooL Dec 6, 2023
746cf46
test(words): add test files
CapooL Dec 6, 2023
3bab12d
refactor(quiz): change urls in paper
CapooL Dec 6, 2023
9b20a19
feat: rewrote the token check
CapooL Dec 6, 2023
4ba42b0
test: replace error test files
CapooL Dec 6, 2023
b69e849
feat(quiz): create model QuizRecord
CapooL Dec 7, 2023
bc31e7f
feat(quiz): create some utils
CapooL Dec 7, 2023
2a0bbc1
feat(quiz): wrote dtos
CapooL Dec 7, 2023
b08e1ad
feat(quiz): main APIs and url config
CapooL Dec 7, 2023
1c687b5
test(quiz): add test files
CapooL Dec 7, 2023
a3227da
style(quiz): reformat for black
CapooL Dec 7, 2023
6b54361
Merge branch 'develop' into quiz_record
CapooL Dec 7, 2023
786fba1
Update urls.py
CapooL Dec 7, 2023
eb48ae1
refactor(quiz): rewrite QuizRecord
CapooL Dec 21, 2023
a80e6e5
refactor(quiz): rewrite functions
CapooL Dec 21, 2023
8533224
style(quiz): reformat for black
CapooL Dec 21, 2023
a019003
test(quiz): add test files
CapooL Dec 21, 2023
1e41385
Merge branch 'e-dialect:develop' into quiz_record
CapooL Dec 21, 2023
e652e08
Merge branch 'quiz_record' of https://github.com/CapooL/hinghwa-dict-…
CapooL Dec 21, 2023
820705f
docs(quiz): rename file and module
CapooL Dec 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion hinghwa-dict-backend/quiz/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from django import forms
from .models import Quiz
from .models import Quiz, QuizRecord


class QuizForm(forms.ModelForm):
class Meta:
model = Quiz
fields = ("question", "options", "answer", "explanation")


class QuizRecordForm(forms.ModelForm):
class Meta:
model = QuizRecord
fields = ("answer", "correctness", "contributor")
58 changes: 58 additions & 0 deletions hinghwa-dict-backend/quiz/migrations/0006_quiz_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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", "0005_paper_record"),
]

operations = [
migrations.CreateModel(
name="QuizRecord",
fields=[
("answer", models.CharField(max_length=1000, verbose_name="答案")),
("correctness", models.BooleanField(verbose_name="是否正确")),
("timestamp", models.DateTimeField(verbose_name="时间")),
(
"id",
models.CharField(
max_length=20,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"contributor",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="QuizRecordUser",
to=settings.AUTH_USER_MODEL,
verbose_name="答题人",
),
),
(
"paper",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz_paper",
to="quiz.paperrecord",
verbose_name="所在试卷",
),
),
(
"quiz",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz",
to="quiz.quiz",
verbose_name="测试题",
),
),
],
),
]
23 changes: 23 additions & 0 deletions hinghwa-dict-backend/quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,26 @@ class PaperRecord(models.Model):
verbose_name="试卷",
)
id = models.CharField(max_length=20, verbose_name="ID", primary_key=True)


class QuizRecord(models.Model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wu 你的 QuizRecord 要依附于 PaperRecord 吗? 我本来以为是相关但独立的两个东西?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以我设想中应该还有contributor、time之类的字段

contributor = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name="QuizRecordUser",
verbose_name="答题人",
)
quiz = models.ForeignKey(
Quiz, on_delete=models.CASCADE, related_name="quiz", verbose_name="测试题"
)
paper = models.ForeignKey(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该是PaperRecord吧,而且是个可选项

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为我认为不在卷子里也可能可以答题

PaperRecord,
on_delete=models.CASCADE,
related_name="quiz_paper",
verbose_name="所在答卷记录",
null=True,
)
answer = models.CharField(max_length=1000, verbose_name="答案")
correctness = models.BooleanField(verbose_name="是否正确")
timestamp = models.DateTimeField(verbose_name="时间")
id = models.CharField(max_length=20, verbose_name="ID", primary_key=True)
11 changes: 11 additions & 0 deletions hinghwa-dict-backend/quiz/paper/dto/paper_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ...models import Paper


# 返回问卷简单信息
def paper_simple(paper: Paper):
response = {
"id": paper.id,
"title": paper.title,
}

return response
18 changes: 18 additions & 0 deletions hinghwa-dict-backend/quiz/quiz_record/dto/quiz_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ...models import QuizRecord
from ...dto.quiz_all import quiz_all
from ...paper.dto.paper_record_dto import paper_record_all
from user.dto.user_simple import user_simple


def quiz_record(record: QuizRecord):
response = {
"id": record.id,
"contributor": user_simple(record.contributor),
"timestamp": record.timestamp,
"quiz": quiz_all(record.quiz),
"answer": record.answer,
"correctness": record.correctness,
"paper": None if record.paper is None else paper_record_all(record.paper),
}

return response
63 changes: 63 additions & 0 deletions hinghwa-dict-backend/quiz/quiz_record/view/quiz_record_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from django.http import JsonResponse
import demjson
from ..dto.quiz_record import quiz_record
from django.views import View
from ...models import Quiz, Paper, QuizRecord
from utils.token import token_pass, token_user
from utils.generate_id import generate_quiz_record_id
from ...forms import QuizRecordForm
from utils.exception.types.not_found import (
PaperRecordNotFoundException,
QuizNotFoundException,
UserNotFoundException,
)
from utils.exception.types.bad_request import BadRequestException
from django.utils import timezone
from user.models import User
from ...models import PaperRecord


class QuizRecordAll(View):
# QZ0401创建答题记录
def post(self, request):
token_pass(request.headers)
body = demjson.decode(request.body)
record_form = QuizRecordForm(body)
quiz_id = request.GET["quiz_id"]
quiz = Quiz.objects.filter(id=quiz_id)
if body["paper_record"] != "":
paper = PaperRecord.objects.filter(id=body["paper_record"])
if not paper.exists():
raise PaperRecordNotFoundException()
if not quiz.exists():
raise QuizNotFoundException()
quiz = quiz[0]
contributor = User.objects.filter(id=body["contributor"])
if not contributor.exists():
raise UserNotFoundException()
contributor = contributor[0]
if not record_form.is_valid():
raise BadRequestException()
record = record_form.save(commit=False)
record.id = generate_quiz_record_id()
if body["paper_record"] != "":
paper = PaperRecord.objects.filter(id=body["paper_record"])
if not paper.exists():
raise PaperRecordNotFoundException()
paper = paper[0]
record.paper = paper
record.quiz = quiz
record.contributor = contributor
record.timestamp = timezone.now()
print("test")
record.save()
return JsonResponse(quiz_record(record), status=200)

# QZ0402查询所有答题记录
def get(self, request):
token_pass(request.headers, -1)
records = QuizRecord.objects.all()
results = []
for record in records:
results.append(quiz_record(record))
return JsonResponse({"total": len(results), "records": results}, status=200)
65 changes: 65 additions & 0 deletions hinghwa-dict-backend/quiz/quiz_record/view/quiz_record_single.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.http import JsonResponse
import demjson
from ..dto.quiz_record import quiz_record
from django.views import View
from ...models import Paper, QuizRecord, PaperRecord
from utils.token import token_pass
from ...forms import QuizRecordForm
from utils.exception.types.not_found import (
UserNotFoundException,
PaperRecordNotFoundException,
QuizRecordNotFoundException,
)
from django.utils import timezone
from user.models import User


class QuizRecordSingle(View):
# QZ0403查询特定答题记录
def get(self, request, record_id):
token_pass(request.headers, -1)
record = QuizRecord.objects.filter(id=record_id)
if not record.exists():
raise QuizRecordNotFoundException
record = record[0]
return JsonResponse(quiz_record(record), status=200)

# QZ0404更改特定答题记录
def put(self, request, record_id):
token_pass(request.headers, -1)
body = demjson.decode(request.body)
if body["paper_record"] != "":
paper = PaperRecord.objects.filter(id=body["paper_record"])
if not paper.exists():
raise PaperRecordNotFoundException()
record = QuizRecord.objects.filter(id=record_id)
if not record.exists():
raise QuizRecordNotFoundException
record = record[0]
record.answer = body["answer"]
record.correctness = body["correctness"]
contributor = User.objects.filter(id=body["contributor"])
if not contributor.exists():
raise UserNotFoundException()
contributor = contributor[0]
if body["paper_record"] != "":
paper = PaperRecord.objects.filter(id=body["paper_record"])
if not paper.exists():
raise PaperRecordNotFoundException()
paper = paper[0]
record.paper = paper
record.timestamp = timezone.now()
record.contributor = contributor
record.save()
return JsonResponse(quiz_record(record), status=200)

# QZ0405删除特定答题记录
def delete(self, request, record_id):
token_pass(request.headers, -1)
record = QuizRecord.objects.filter(id=record_id)
id = str(record_id)
if not record.exists():
raise QuizRecordNotFoundException
record = record[0]
record.delete()
return JsonResponse({"msg": "答题记录{}删除成功".format(id)}, status=200)
8 changes: 6 additions & 2 deletions hinghwa-dict-backend/quiz/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.urls import path
from .paper.view.manage_all import *
from .paper.view.manage_single import *
from .views import *
from .paper.view.paper_record_all import *
from .quiz_record.view.quiz_record_all import *
from .quiz_record.view.quiz_record_single import *
from .paper.view.paper_record_single import *

app_name = "users"
Expand All @@ -12,4 +12,8 @@
path("/<int:id>", csrf_exempt(SingleQuiz.as_view())), # QZ0101 QZ0103 QZ0104
path("/<int:id>/visibility", csrf_exempt(ManageVisibility.as_view())), # QZ0105
path("/random", csrf_exempt(RandomQuiz.as_view())), # QZ0202
path("/records", csrf_exempt(QuizRecordAll.as_view())), # QZ0401 QZ0402
path(
"/records/<str:record_id>", csrf_exempt(QuizRecordSingle.as_view())
), # QZ0403 QZ0404
]
12 changes: 12 additions & 0 deletions hinghwa-dict-backend/utils/exception/types/not_found.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,15 @@ def __init__(self, id=""):
super().__init__()
self.status = 404
self.msg = "答卷记录{}不存在!".format(id)


class QuizRecordNotFoundException(NotFoundException):
"""
答题记录不存在异常
parma id:答题记录id
"""

def __init__(self, id=""):
super().__init__()
self.status = 404
self.msg = "答题记录{}不存在!".format(id)
11 changes: 11 additions & 0 deletions hinghwa-dict-backend/utils/generate_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rewards.products.models.product import Product
from rewards.orders.models.order import Order
from word.models import List
from quiz.models import Paper, PaperRecord, QuizRecord
from quiz.models import Paper, PaperRecord


Expand Down Expand Up @@ -74,3 +75,13 @@ def generate_paper_record_id():
else:
new_id = 1
return f"DJ{new_id:06d}"


def generate_quiz_record_id():
last_quiz_record = QuizRecord.objects.order_by("-id").first()
if last_quiz_record:
last_id = int(last_quiz_record.id[2:])
new_id = last_id + 1
else:
new_id = 1
return f"DT{new_id:06d}"
2 changes: 1 addition & 1 deletion hinghwa-dict-backend/word/lists/view/manage_all_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def get(self, request):
result = []
for list in total_list:
result.append(list_all(list))
return JsonResponse({"total": len(result), "lists": result})
return JsonResponse({"total": len(result), "lists": result}, status=200)
Loading
Loading