Skip to content

Commit

Permalink
Add FEEDBACK_ENDPOINT & FEEDBACK_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Oct 24, 2024
1 parent 87b65b6 commit f6c8816
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions conf/local_settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ EXTERNAL_LOGINS = {
'use_smtp': False,
}
}
FEEDBACK_ENDPOINT = 'https://example.com/bot/' # or None
FEEDBACK_KEY = '1145141919811'
30 changes: 30 additions & 0 deletions frontend/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from urllib.parse import quote
from datetime import timedelta
import requests

from django.contrib import messages
from django.contrib.admin import site
Expand All @@ -10,6 +11,7 @@
from django.template.response import TemplateResponse
from django.views import View
from django.utils import timezone
from django.conf import settings

from server.announcement.interface import Announcement
from server.challenge.interface import Challenge
Expand Down Expand Up @@ -258,6 +260,9 @@ def check_frequency(self, challenge_id):
return too_frequent, latest

def get(self, request, challenge_id):
# check if this is set, even as None
# to make admins quickly notice if they forgot this...
settings.FEEDBACK_ENDPOINT and settings.FEEDBACK_KEY
challenge = self.check(challenge_id)
if not challenge:
return redirect('hub')
Expand Down Expand Up @@ -289,6 +294,31 @@ def post(self, request, challenge_id):
"latest_submit": latest,
"contents": contents,
})
user = User.get(Context.from_request(request), request.user.pk)
# send to user-defined endpoint
if settings.FEEDBACK_ENDPOINT:
try:
response = requests.post(
url=settings.FEEDBACK_ENDPOINT,
headers={
'Authorization': 'Bearer ' + settings.FEEDBACK_KEY,
},
json={
'user_id': user.pk,
'contents': contents,
'challenge_name': challenge_name,
},
timeout=15
)
response.raise_for_status()
except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e:
messages.error(request, "反馈发送失败,请向管理员反馈此问题。")
return TemplateResponse(request, 'challenge_feedback.html', {
"challenge_name": challenge_name,
"too_frequent": too_frequent,
"latest_submit": latest,
"contents": contents,
})
feedback = UnidirectionalFeedback.objects.create(challenge_id=challenge_id, user=request.user, contents=contents)
feedback.save()

Expand Down

0 comments on commit f6c8816

Please sign in to comment.