Skip to content

Commit

Permalink
Allow setting feedback content without code change
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Oct 25, 2024
1 parent 49ab8f5 commit 5978a0e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
4 changes: 2 additions & 2 deletions frontend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from server.terms.models import Terms
from server.trigger.models import Trigger
from server.user.models import User
from .models import Page, Account, Code, AccountLog, SpecialProfileUsedRecord, Qa, Credits, UnidirectionalFeedback
from .models import Page, Account, Code, AccountLog, SpecialProfileUsedRecord, Qa, Credits, UnidirectionalFeedback, Feedback

admin.site.register([Page, Account, Code, Qa, Credits, SpecialProfileUsedRecord, UnidirectionalFeedback])
admin.site.register([Page, Account, Code, Qa, Credits, SpecialProfileUsedRecord, UnidirectionalFeedback, Feedback])


class PermissionListFilter(admin.SimpleListFilter):
Expand Down
33 changes: 33 additions & 0 deletions frontend/migrations/0011_feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.16 on 2024-10-25 06:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("frontend", "0010_unidirectionalfeedback"),
]

operations = [
migrations.CreateModel(
name="Feedback",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"content",
models.TextField(
blank=True, help_text="会被放入 <code>div</code> 的 HTML"
),
),
],
),
]
8 changes: 8 additions & 0 deletions frontend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def get(cls):
return cls.objects.get_or_create()[0]


class Feedback(models.Model):
content = models.TextField(blank=True, help_text='会被放入 <code>div</code> 的 HTML')

@classmethod
def get(cls):
return cls.objects.get_or_create()[0]


class UnidirectionalFeedback(models.Model):
"""
User could submit feedback for a specific challenge.
Expand Down
13 changes: 3 additions & 10 deletions frontend/templates/challenge_feedback.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
{% endblock %}

{% block content %}
{% verbatim %}

<div id="app">
<h1>提交对题目 {{challenge_name}} 的反馈</h1>
<p>提示:</p>
<ul>
<li>反馈功能是单向的。如果题目没有问题,你不会得到任何形式的通知;如果题目确实需要更新,我们会更新并通知所有选手。如果希望能够得到回复,请将您遇到的问题详细描述并发送邮件到 hackergame AT ustclug.org(请将 AT 替换为 @)。邮件中需要包含题目名称、Token 中冒号前的数字编号、你的操作、实际结果与预期结果,详见<a href="/qa/">问与答</a></li>
<li>同一道题目两次反馈之间至少间隔 1 小时。</li>
<li>反馈限制最多 1024 个字符。</li>
<li>使用反馈功能大量提交垃圾信息可能导致你的账号被封禁。</li>
</ul>
<div>{{ feedback.content|safe }}</div>
{% verbatim %}
<div v-if="!too_frequent">
{% endverbatim %}
<form method="post" class="pure-form pure-form-stacked">
Expand All @@ -40,14 +35,12 @@ <h1>提交对题目 {{challenge_name}} 的反馈</h1>
</div>
</div>
{% endverbatim %}
{{ challenge_name|json_script:'challenge-name' }}
{{ too_frequent|json_script:'too-frequent' }}
{{ latest_submit|json_script:'latest-submit' }}
<script>
app = new Vue({
el: '#app',
data: {
challenge_name: JSON.parse(document.getElementById('challenge-name').textContent),
too_frequent: JSON.parse(document.getElementById('too-frequent').textContent),
latest_submit: JSON.parse(document.getElementById('latest-submit').textContent),
},
Expand Down
32 changes: 13 additions & 19 deletions frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from server.context import Context
from server.exceptions import Error, NotFound, WrongFormat

from frontend.models import Account, AccountLog, Credits, Qa, SpecialProfileUsedRecord, UnidirectionalFeedback
from frontend.models import Account, AccountLog, Credits, Qa, SpecialProfileUsedRecord, UnidirectionalFeedback, Feedback

import logging

Expand Down Expand Up @@ -261,6 +261,14 @@ def check_frequency(self, challenge_id):
too_frequent = True

return too_frequent, latest

def return_template(self, challenge_name, too_frequent, latest):
return TemplateResponse(self.request, 'challenge_feedback.html', {
"feedback": Feedback.get(),
"challenge_name": challenge_name,
"too_frequent": too_frequent,
"latest_submit": latest,
})

def get(self, request, challenge_id):
# check if this is set, even as None
Expand All @@ -274,12 +282,8 @@ def get(self, request, challenge_id):
challenge_name = challenge.name

too_frequent, latest = self.check_frequency(challenge_id)

return TemplateResponse(request, 'challenge_feedback.html', {
"challenge_name": challenge_name,
"too_frequent": too_frequent,
"latest_submit": latest,
})

return self.return_template(challenge_name, too_frequent, latest)

def post(self, request, challenge_id):
challenge = self.check(challenge_id)
Expand All @@ -293,12 +297,7 @@ def post(self, request, challenge_id):
contents = request.POST.get("contents")
if len(contents) > 1024:
messages.error(request, "提交内容超过字数限制。")
return TemplateResponse(request, 'challenge_feedback.html', {
"challenge_name": challenge_name,
"too_frequent": too_frequent,
"latest_submit": latest,
"contents": contents,
})
return self.return_template(challenge_name, too_frequent, latest)
user = User.get(Context.from_request(request), request.user.pk)
# send to user-defined endpoint
if settings.FEEDBACK_ENDPOINT:
Expand All @@ -319,12 +318,7 @@ def post(self, request, challenge_id):
except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e:
messages.error(request, "反馈发送失败,请向管理员反馈此问题。")
logger.exception("反馈发送失败")
return TemplateResponse(request, 'challenge_feedback.html', {
"challenge_name": challenge_name,
"too_frequent": too_frequent,
"latest_submit": latest,
"contents": contents,
})
return self.return_template(challenge_name, too_frequent, latest)
feedback = UnidirectionalFeedback.objects.create(challenge_id=challenge_id, user=request.user, contents=contents)
feedback.save()

Expand Down

0 comments on commit 5978a0e

Please sign in to comment.