2016-11-20 19:18:25 +08:00
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
from django.utils import timezone
|
2016-09-25 14:07:45 +08:00
|
|
|
|
|
2016-11-19 13:47:59 +08:00
|
|
|
|
from account.decorators import super_admin_required
|
2017-05-10 19:40:26 +08:00
|
|
|
|
from judge.dispatcher import process_pending_task
|
2017-10-02 03:54:34 +08:00
|
|
|
|
from judge.languages import languages, spj_languages
|
|
|
|
|
from options.options import SysOptions
|
2016-11-20 19:18:25 +08:00
|
|
|
|
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
2017-10-02 03:54:34 +08:00
|
|
|
|
from .models import JudgeServer
|
2017-01-23 16:48:04 +08:00
|
|
|
|
from .serializers import (CreateEditWebsiteConfigSerializer,
|
2016-11-19 13:47:59 +08:00
|
|
|
|
CreateSMTPConfigSerializer, EditSMTPConfigSerializer,
|
2017-01-23 16:48:04 +08:00
|
|
|
|
JudgeServerHeartbeatSerializer,
|
2017-10-02 03:54:34 +08:00
|
|
|
|
JudgeServerSerializer, TestSMTPConfigSerializer)
|
2016-11-19 13:47:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SMTPAPI(APIView):
|
|
|
|
|
@super_admin_required
|
|
|
|
|
def get(self, request):
|
2017-10-02 03:54:34 +08:00
|
|
|
|
smtp = SysOptions.smtp_config
|
2016-11-19 13:47:59 +08:00
|
|
|
|
if not smtp:
|
|
|
|
|
return self.success(None)
|
2017-10-02 03:54:34 +08:00
|
|
|
|
smtp.pop("password")
|
|
|
|
|
return self.success(smtp)
|
2016-11-19 13:47:59 +08:00
|
|
|
|
|
|
|
|
|
@validate_serializer(CreateSMTPConfigSerializer)
|
2017-01-23 16:01:56 +08:00
|
|
|
|
@super_admin_required
|
2016-11-19 13:47:59 +08:00
|
|
|
|
def post(self, request):
|
2017-10-02 03:54:34 +08:00
|
|
|
|
SysOptions.smtp_config = request.data
|
|
|
|
|
return self.success()
|
2016-11-19 13:47:59 +08:00
|
|
|
|
|
|
|
|
|
@validate_serializer(EditSMTPConfigSerializer)
|
2017-01-23 16:01:56 +08:00
|
|
|
|
@super_admin_required
|
2016-11-19 13:47:59 +08:00
|
|
|
|
def put(self, request):
|
2017-10-02 03:54:34 +08:00
|
|
|
|
smtp = SysOptions.smtp_config
|
2016-11-19 13:47:59 +08:00
|
|
|
|
data = request.data
|
2017-10-02 03:54:34 +08:00
|
|
|
|
for item in ["server", "port", "email", "tls"]:
|
|
|
|
|
smtp[item] = data[item]
|
|
|
|
|
if "password" in data:
|
|
|
|
|
smtp["password"] = data["password"]
|
|
|
|
|
SysOptions.smtp_config = smtp
|
|
|
|
|
return self.success()
|
2016-11-19 13:47:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SMTPTestAPI(APIView):
|
|
|
|
|
@super_admin_required
|
|
|
|
|
@validate_serializer(TestSMTPConfigSerializer)
|
|
|
|
|
def post(self, request):
|
|
|
|
|
return self.success({"result": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebsiteConfigAPI(APIView):
|
|
|
|
|
def get(self, request):
|
2017-10-02 03:54:34 +08:00
|
|
|
|
ret = {key: getattr(SysOptions, key) for key in
|
|
|
|
|
["website_base_url", "website_name", "website_name_shortcut",
|
|
|
|
|
"website_footer", "allow_register", "submission_list_show_all"]}
|
|
|
|
|
return self.success(ret)
|
2016-11-19 13:47:59 +08:00
|
|
|
|
|
|
|
|
|
@validate_serializer(CreateEditWebsiteConfigSerializer)
|
|
|
|
|
@super_admin_required
|
|
|
|
|
def post(self, request):
|
2017-10-02 03:54:34 +08:00
|
|
|
|
for k, v in request.data.items():
|
|
|
|
|
setattr(SysOptions, k, v)
|
|
|
|
|
return self.success()
|
2016-11-20 19:18:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JudgeServerAPI(APIView):
|
|
|
|
|
@super_admin_required
|
|
|
|
|
def get(self, request):
|
|
|
|
|
servers = JudgeServer.objects.all().order_by("-last_heartbeat")
|
2017-10-02 03:54:34 +08:00
|
|
|
|
return self.success({"token": SysOptions.judge_server_token,
|
2016-11-20 19:18:25 +08:00
|
|
|
|
"servers": JudgeServerSerializer(servers, many=True).data})
|
|
|
|
|
|
|
|
|
|
@super_admin_required
|
|
|
|
|
def delete(self, request):
|
2017-01-24 16:48:39 +08:00
|
|
|
|
hostname = request.GET.get("hostname")
|
|
|
|
|
if hostname:
|
|
|
|
|
JudgeServer.objects.filter(hostname=hostname).delete()
|
2017-01-24 13:22:40 +08:00
|
|
|
|
return self.success()
|
2016-11-20 19:18:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JudgeServerHeartbeatAPI(CSRFExemptAPIView):
|
|
|
|
|
@validate_serializer(JudgeServerHeartbeatSerializer)
|
|
|
|
|
def post(self, request):
|
|
|
|
|
data = request.data
|
2017-01-26 13:55:31 +08:00
|
|
|
|
client_token = request.META.get("HTTP_X_JUDGE_SERVER_TOKEN")
|
2017-10-02 03:54:34 +08:00
|
|
|
|
if hashlib.sha256(SysOptions.judge_server_token.encode("utf-8")).hexdigest() != client_token:
|
2016-11-20 19:18:25 +08:00
|
|
|
|
return self.error("Invalid token")
|
|
|
|
|
service_url = data.get("service_url")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
server = JudgeServer.objects.get(hostname=data["hostname"])
|
|
|
|
|
server.judger_version = data["judger_version"]
|
|
|
|
|
server.cpu_core = data["cpu_core"]
|
|
|
|
|
server.memory_usage = data["memory"]
|
|
|
|
|
server.cpu_usage = data["cpu"]
|
2017-01-23 16:25:14 +08:00
|
|
|
|
server.service_url = service_url
|
2017-01-24 16:22:22 +08:00
|
|
|
|
server.ip = request.META["REMOTE_ADDR"]
|
2016-11-20 19:18:25 +08:00
|
|
|
|
server.last_heartbeat = timezone.now()
|
|
|
|
|
server.save()
|
|
|
|
|
except JudgeServer.DoesNotExist:
|
|
|
|
|
JudgeServer.objects.create(hostname=data["hostname"],
|
|
|
|
|
judger_version=data["judger_version"],
|
|
|
|
|
cpu_core=data["cpu_core"],
|
|
|
|
|
memory_usage=data["memory"],
|
|
|
|
|
cpu_usage=data["cpu"],
|
2017-01-24 16:22:22 +08:00
|
|
|
|
ip=request.META["REMOTE_ADDR"],
|
2016-11-20 19:18:25 +08:00
|
|
|
|
service_url=service_url,
|
|
|
|
|
last_heartbeat=timezone.now(),
|
|
|
|
|
)
|
2017-05-10 17:20:52 +08:00
|
|
|
|
# 新server上线 处理队列中的,防止没有新的提交而导致一直waiting
|
2017-08-15 20:32:14 +08:00
|
|
|
|
process_pending_task()
|
2017-05-10 17:20:52 +08:00
|
|
|
|
|
2016-11-20 19:18:25 +08:00
|
|
|
|
return self.success()
|
2017-01-24 13:22:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LanguagesAPI(APIView):
|
|
|
|
|
def get(self, request):
|
|
|
|
|
return self.success({"languages": languages, "spj_languages": spj_languages})
|