Skip to content

Commit 003cc06

Browse files
committed
[feature] judger port token
1 parent a9e5b91 commit 003cc06

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

judger/views.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from django.shortcuts import get_object_or_404
22
from django.utils.decorators import method_decorator
3+
from django.conf import settings
34

45
from rest_framework.response import Response
56
from rest_framework.views import APIView
67
from rest_framework import status
78

9+
import requests
10+
import secrets
11+
812
from segmentoj.decorator import parameter_required, syllable_required
913
from status.models import Status
1014
from problem.models import Problem
@@ -44,3 +48,36 @@ def get(self, request, pid):
4448
ps = ProblemSerializer(problem)
4549
return Response({'res': ps.data}, status=status.HTTP_200_OK)
4650

51+
class JudgerTokenView(APIView):
52+
@method_decorator(judger_account_required())
53+
def get(self, request):
54+
token = secrets.token_urlsafe(64)
55+
56+
try:
57+
res = requests.post('{base_url}/api/token'.format(base_url=settings.JUDGER_PORT['base_url']), json={
58+
'token': token,
59+
'password': settings.JUDGER_PORT.get('password'),
60+
})
61+
res_json = res.json()
62+
except:
63+
return Response({
64+
'detail': 'Cannot connect to judger port.',
65+
}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
66+
else:
67+
code = res_json.get('code')
68+
69+
if code is None:
70+
return Response({
71+
'detail': 'Judger Port response format incorrect.'
72+
}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
73+
elif code == 4004:
74+
return Response({
75+
'detail': 'Judger Port don\'t understand our format.\n'
76+
'Maybe an upgrade is required.'
77+
}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
78+
79+
return Response({
80+
'code': 1000,
81+
'res': token,
82+
}, status=status.HTTP_200_OK)
83+

segmentoj/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
AccountView,
2828
AccountIntroductionView,
2929
AccountUsernameAccessibilityView,
30-
# AccountAvatarView,
3130
AccountPasswordView,
3231
AccountEmailView,
3332
)
3433

35-
from judger.views import JudgerProblemView, JudgerTaskView
34+
from judger.views import JudgerProblemView, JudgerTaskView, JudgerTokenView
3635

3736
from status.views import StatusView, StatusListView
3837
from captcha.views import get_captcha
@@ -68,6 +67,7 @@
6867
# Judger
6968
path('api/judger/task/<int:tid>', JudgerTaskView.as_view()),
7069
path('api/judger/problem/<int:pid>', JudgerProblemView.as_view()),
70+
path('api/judger/token', JudgerTokenView.as_view()),
7171
# Captcha
7272
path('api/captcha/<int:key>', get_captcha),
7373
]

0 commit comments

Comments
 (0)