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): add cert system #354

Merged
merged 9 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions hinghwa-dict-backend/HinghwaDict/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import logging
import os

Expand Down Expand Up @@ -188,8 +189,12 @@
"token",
)
# parameter of Tencent cos
COS_SECRET_ID = env.str("COS_SECRET_ID", "DEFAULT_COS_SECRET_ID") # 替换为用户的 secretId
COS_SECRET_KEY = env.str("COS_SECRET_KEY", "DEFAULT_COS_SECRET_KEY") # 替换为用户的 secretKey
COS_SECRET_ID = env.str(
"COS_SECRET_ID", "DEFAULT_COS_SECRET_ID"
) # 替换为用户的 secretId
COS_SECRET_KEY = env.str(
"COS_SECRET_KEY", "DEFAULT_COS_SECRET_KEY"
) # 替换为用户的 secretKey
COS_BUCKET = env.str("COS_BUCKET", "DEFAULT_COS_BUCKET") # BucketName-APPID
COS_REGION = env.str("COS_REGION", "DEFAULT_COS_REGION")

Expand Down
2 changes: 2 additions & 0 deletions hinghwa-dict-backend/HinghwaDict/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path, include

Expand Down Expand Up @@ -56,4 +57,5 @@
path("orders", include("rewards.orders.urls", namespace="rewards.orders")),
path("lists", include("word.lists.urls", namespace="word.lists")),
path("papers", include("quiz.paper.urls", namespace="quiz.papers")),
path("certs", include("quiz.cert.urls", namespace="quiz.certs")),
]
14 changes: 11 additions & 3 deletions hinghwa-dict-backend/article/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Migration(migrations.Migration):
),
(
"views",
models.IntegerField(default=0, editable=False, verbose_name="阅读量"),
models.IntegerField(
default=0, editable=False, verbose_name="阅读量"
),
),
(
"publish_time",
Expand All @@ -39,7 +41,10 @@ class Migration(migrations.Migration):
),
("content", models.TextField(verbose_name="正文")),
("cover", models.URLField(verbose_name="图片地址")),
("visibility", models.BooleanField(default=False, verbose_name="是否审核")),
(
"visibility",
models.BooleanField(default=False, verbose_name="是否审核"),
),
(
"author",
models.ForeignKey(
Expand Down Expand Up @@ -78,7 +83,10 @@ class Migration(migrations.Migration):
),
),
("content", models.TextField(max_length=500, verbose_name="内容")),
("time", models.DateTimeField(auto_now_add=True, verbose_name="评论时间")),
(
"time",
models.DateTimeField(auto_now_add=True, verbose_name="评论时间"),
),
(
"article",
models.ForeignKey(
Expand Down
5 changes: 4 additions & 1 deletion hinghwa-dict-backend/article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class Comment(models.Model):
verbose_name="父评论",
)
article = models.ForeignKey(
Article, on_delete=models.CASCADE, related_name="comments", verbose_name="评论文章"
Article,
on_delete=models.CASCADE,
related_name="comments",
verbose_name="评论文章",
)
like_users = models.ManyToManyField(
User,
Expand Down
12 changes: 10 additions & 2 deletions hinghwa-dict-backend/article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def post(self, request) -> JsonResponse:
article.save()
content = f"我创建了文章(id={article.id}),请及时去审核"
sendNotification(
article.author, None, content, title="【提醒】文章待审核", action_object=article
article.author,
None,
content,
title="【提醒】文章待审核",
action_object=article,
)
return JsonResponse({"id": article.id}, status=200)

Expand Down Expand Up @@ -165,7 +169,11 @@ def put(self, request, id) -> JsonResponse:
article.save()
content = f"我修改了文章(id={article.id}),请及时去审核"
sendNotification(
article.author, None, content, title="【提醒】文章待审核", action_object=article
article.author,
None,
content,
title="【提醒】文章待审核",
action_object=article,
)
return JsonResponse({}, status=200)

Expand Down
5 changes: 4 additions & 1 deletion hinghwa-dict-backend/music/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class Migration(migrations.Migration):
("title", models.CharField(max_length=100, verbose_name="曲名")),
("artist", models.CharField(max_length=100, verbose_name="作者")),
("cover", models.URLField(verbose_name="音乐封面地址")),
("visibility", models.BooleanField(default=False, verbose_name="是否可见")),
(
"visibility",
models.BooleanField(default=False, verbose_name="是否可见"),
),
(
"contributor",
models.ForeignKey(
Expand Down
17 changes: 17 additions & 0 deletions hinghwa-dict-backend/quiz/cert/dto/cert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ...models import Cert
from user.dto.user_simple import user_simple


def cert_info(cert: Cert):
response = {
"level": cert.level,
"name": cert.name,
"place": cert.place,
"sequence": cert.sequence,
"grade": cert.grade,
"scores": cert.scores,
"time": cert.time,
"ID": cert.id,
"user": None if cert.user is None else user_simple(cert.user),
}
return response
8 changes: 8 additions & 0 deletions hinghwa-dict-backend/quiz/cert/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django import forms
from ..models import Cert


class CertForm(forms.ModelForm):
class Meta:
model = Cert
fields = ("level", "name", "place", "sequence", "grade", "scores")
11 changes: 11 additions & 0 deletions hinghwa-dict-backend/quiz/cert/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from django.views.decorators.csrf import csrf_exempt
from .view.cert_all import *
from .view.cert_single import *

app_name = "quiz.certs"

urlpatterns = [
path("", csrf_exempt(AllCert.as_view())),
path("/<str:id>", csrf_exempt(SingleCert.as_view())),
]
56 changes: 56 additions & 0 deletions hinghwa-dict-backend/quiz/cert/view/cert_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import demjson
from django.http import JsonResponse
from django.views import View
from ...models import Cert
from ..forms import CertForm
from user.models import User
from ..dto.cert import cert_info
from utils.exception.types.bad_request import BadRequestException
from utils.token import token_pass
from utils.generate_id import generate_cert_id
from utils.exception.types.not_found import UserNotFoundException
from user.utils import get_user_by_id
from django.utils import timezone


class AllCert(View):
# QZ0502创建证书
def post(self, request):
token_pass(request.headers, -1)
body = demjson.decode(request.body)
cert_form = CertForm(body["cert"])
if not cert_form.is_valid():
raise BadRequestException()
cert = cert_form.save(commit=False)
if body["user"] != "":
user = User.objects.filter(id=body["user"])
if not user.exists():
raise UserNotFoundException()
user = user[0]
cert.user = user
cert.id = generate_cert_id()
cert.time = timezone.now()
cert.save()
return JsonResponse(cert_info(cert), status=200)

# QZ0501查询所有证书
def get(self, request):
token_pass(request.headers)
result = []
name = request.GET["name"]
name_set = Cert.objects.filter(name=name)
for name in name_set:
result.append(cert_info(name))
if request.GET["user"]:
user_id = request.GET["user"]
users = Cert.objects.filter(user=get_user_by_id(user_id))
for user in users:
result.append(cert_info(user))
result = list(tuple(result))
return JsonResponse(
{
"amount": len(result),
"certs": result,
},
status=200,
)
17 changes: 17 additions & 0 deletions hinghwa-dict-backend/quiz/cert/view/cert_single.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.http import JsonResponse
from django.views import View
from ...models import Cert
from utils.token import token_pass
from utils.exception.types.not_found import CertNotFoundException
from ..dto.cert import cert_info


class SingleCert(View):
# QZ0503查询证书信息
def get(self, request, id):
token_pass(request.headers)
cert = Cert.objects.filter(id=id)
if not cert.exists():
raise CertNotFoundException()
cert = cert[0]
return JsonResponse({"cert": cert_info(cert)}, status=200)
5 changes: 4 additions & 1 deletion hinghwa-dict-backend/quiz/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class Migration(migrations.Migration):
),
("answer", models.IntegerField(default=0, verbose_name="答案序号")),
("explanation", models.TextField(verbose_name="答案解析")),
("visibility", models.BooleanField(default=False, verbose_name="是否审核")),
(
"visibility",
models.BooleanField(default=False, verbose_name="是否审核"),
),
],
options={
"verbose_name": "测试题",
Expand Down
4 changes: 3 additions & 1 deletion hinghwa-dict-backend/quiz/migrations/0004_quiz_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class Migration(migrations.Migration):
fields=[
(
"title",
models.CharField(blank=True, max_length=50, verbose_name="试卷标题"),
models.CharField(
blank=True, max_length=50, verbose_name="试卷标题"
),
),
(
"id",
Expand Down
58 changes: 58 additions & 0 deletions hinghwa-dict-backend/quiz/migrations/0007_cert.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", "0006_quiz_record"),
]

operations = [
migrations.AlterField(
model_name="quizrecord",
name="paper",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="quiz_paper",
to="quiz.paperrecord",
verbose_name="所在答卷记录",
),
),
migrations.CreateModel(
name="Cert",
fields=[
("level", models.IntegerField(verbose_name="等级")),
("name", models.CharField(max_length=20, verbose_name="参与者")),
("place", models.CharField(max_length=100, verbose_name="地址")),
("sequence", models.IntegerField(verbose_name="第几次认证")),
("grade", models.CharField(max_length=20, verbose_name="等级")),
(
"scores",
models.TextField(blank=True, default="[]", verbose_name="分数"),
),
("time", models.DateTimeField(max_length=20, verbose_name="时间")),
(
"id",
models.CharField(
max_length=20,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="cert_user",
to=settings.AUTH_USER_MODEL,
verbose_name="证书用户",
),
),
],
),
]
22 changes: 21 additions & 1 deletion hinghwa-dict-backend/quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Quiz(models.Model):
answer = models.IntegerField(default=0, verbose_name="答案序号", editable=True)
explanation = models.TextField(verbose_name="答案解析")
visibility = models.BooleanField(default=False, verbose_name="是否审核")
voice_source = models.URLField(max_length=200, verbose_name="播报语音链接", default="")
voice_source = models.URLField(
max_length=200, verbose_name="播报语音链接", default=""
)
type = models.CharField(blank=True, max_length=50, verbose_name="问题类型")

def __str__(self):
Expand Down Expand Up @@ -83,3 +85,21 @@ class QuizRecord(models.Model):
correctness = models.BooleanField(verbose_name="是否正确")
timestamp = models.DateTimeField(verbose_name="时间")
id = models.CharField(max_length=20, verbose_name="ID", primary_key=True)


class Cert(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name="cert_user",
verbose_name="证书用户",
null=True,
)
level = models.IntegerField(verbose_name="等级")
name = models.CharField(max_length=20, verbose_name="参与者")
place = models.CharField(max_length=100, verbose_name="地址")
sequence = models.IntegerField(verbose_name="第几次认证")
grade = models.CharField(max_length=20, verbose_name="等级")
scores = models.TextField(blank=True, default="[]", verbose_name="分数")
time = models.DateTimeField(max_length=20, verbose_name="时间")
id = models.CharField(max_length=20, verbose_name="ID", primary_key=True)
4 changes: 3 additions & 1 deletion hinghwa-dict-backend/rewards/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class Migration(migrations.Migration):
),
(
"details",
models.TextField(blank=True, max_length=200, verbose_name="商品详情"),
models.TextField(
blank=True, max_length=200, verbose_name="商品详情"
),
),
(
"id",
Expand Down
8 changes: 5 additions & 3 deletions hinghwa-dict-backend/user/dto/user_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ def user_all(user: User) -> dict:

response.update(
{
"login_time": localtime(user.last_login).__format__("%Y-%m-%d %H:%M:%S")
if user.last_login
else "",
"login_time": (
localtime(user.last_login).__format__("%Y-%m-%d %H:%M:%S")
if user.last_login
else ""
),
}
)
return response
Loading
Loading