-
Notifications
You must be signed in to change notification settings - Fork 9
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
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 a43d643
feat(quiz): create dto and utils for PaperRecord
CapooL 285497b
feat(quiz): write functions and urls
CapooL 84df0fe
test(quiz): add test files
CapooL 92a8ddf
style(quiz): reformat for black
CapooL 144567d
Update models.py
CapooL c069685
refactor(words): rename variable
CapooL 746cf46
test(words): add test files
CapooL 3bab12d
refactor(quiz): change urls in paper
CapooL 9b20a19
feat: rewrote the token check
CapooL 4ba42b0
test: replace error test files
CapooL b69e849
feat(quiz): create model QuizRecord
CapooL bc31e7f
feat(quiz): create some utils
CapooL 2a0bbc1
feat(quiz): wrote dtos
CapooL b08e1ad
feat(quiz): main APIs and url config
CapooL 1c687b5
test(quiz): add test files
CapooL a3227da
style(quiz): reformat for black
CapooL 6b54361
Merge branch 'develop' into quiz_record
CapooL 786fba1
Update urls.py
CapooL eb48ae1
refactor(quiz): rewrite QuizRecord
CapooL a80e6e5
refactor(quiz): rewrite functions
CapooL 8533224
style(quiz): reformat for black
CapooL a019003
test(quiz): add test files
CapooL 1e41385
Merge branch 'e-dialect:develop' into quiz_record
CapooL e652e08
Merge branch 'quiz_record' of https://github.com/CapooL/hinghwa-dict-…
CapooL 820705f
docs(quiz): rename file and module
CapooL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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") |
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,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="测试题", | ||
), | ||
), | ||
], | ||
), | ||
] |
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 |
---|---|---|
|
@@ -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): | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里应该是PaperRecord吧,而且是个可选项 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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,11 @@ | ||
from ...models import Paper | ||
|
||
|
||
# 返回问卷简单信息 | ||
def paper_simple(paper: Paper): | ||
response = { | ||
"id": paper.id, | ||
"title": paper.title, | ||
} | ||
|
||
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,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
63
hinghwa-dict-backend/quiz/quiz_record/view/quiz_record_all.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,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
65
hinghwa-dict-backend/quiz/quiz_record/view/quiz_record_single.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,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) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wu 你的 QuizRecord 要依附于 PaperRecord 吗? 我本来以为是相关但独立的两个东西?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以我设想中应该还有contributor、time之类的字段