2016-11-20 11:18:25 +00:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from django.utils import timezone
|
2016-09-25 06:07:45 +00:00
|
|
|
|
2016-11-19 05:47:59 +00:00
|
|
|
from account.decorators import super_admin_required
|
2017-01-24 05:22:40 +00:00
|
|
|
from judge.languages import languages, spj_languages
|
2016-11-20 11:18:25 +00:00
|
|
|
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
|
|
|
from utils.shortcuts import rand_str
|
2016-11-19 05:47:59 +00:00
|
|
|
|
2017-01-23 08:48:04 +00:00
|
|
|
from .models import JudgeServer, JudgeServerToken, SMTPConfig, WebsiteConfig
|
|
|
|
from .serializers import (CreateEditWebsiteConfigSerializer,
|
2016-11-19 05:47:59 +00:00
|
|
|
CreateSMTPConfigSerializer, EditSMTPConfigSerializer,
|
2017-01-23 08:48:04 +00:00
|
|
|
JudgeServerHeartbeatSerializer,
|
|
|
|
JudgeServerSerializer, SMTPConfigSerializer,
|
|
|
|
TestSMTPConfigSerializer, WebsiteConfigSerializer)
|
2016-11-19 05:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SMTPAPI(APIView):
|
|
|
|
@super_admin_required
|
|
|
|
def get(self, request):
|
|
|
|
smtp = SMTPConfig.objects.first()
|
|
|
|
if not smtp:
|
|
|
|
return self.success(None)
|
|
|
|
return self.success(SMTPConfigSerializer(smtp).data)
|
|
|
|
|
|
|
|
@validate_serializer(CreateSMTPConfigSerializer)
|
2017-01-23 08:01:56 +00:00
|
|
|
@super_admin_required
|
2016-11-19 05:47:59 +00:00
|
|
|
def post(self, request):
|
|
|
|
SMTPConfig.objects.all().delete()
|
|
|
|
smtp = SMTPConfig.objects.create(**request.data)
|
|
|
|
return self.success(SMTPConfigSerializer(smtp).data)
|
|
|
|
|
|
|
|
@validate_serializer(EditSMTPConfigSerializer)
|
2017-01-23 08:01:56 +00:00
|
|
|
@super_admin_required
|
2016-11-19 05:47:59 +00:00
|
|
|
def put(self, request):
|
|
|
|
data = request.data
|
|
|
|
smtp = SMTPConfig.objects.first()
|
|
|
|
if not smtp:
|
|
|
|
return self.error("SMTP config is missing")
|
|
|
|
smtp.server = data["server"]
|
|
|
|
smtp.port = data["port"]
|
|
|
|
smtp.email = data["email"]
|
|
|
|
smtp.tls = data["tls"]
|
|
|
|
if data.get("password"):
|
|
|
|
smtp.password = data["password"]
|
|
|
|
smtp.save()
|
|
|
|
return self.success(SMTPConfigSerializer(smtp).data)
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
config = WebsiteConfig.objects.first()
|
|
|
|
if not config:
|
|
|
|
config = WebsiteConfig.objects.create()
|
|
|
|
return self.success(WebsiteConfigSerializer(config).data)
|
|
|
|
|
|
|
|
@validate_serializer(CreateEditWebsiteConfigSerializer)
|
|
|
|
@super_admin_required
|
|
|
|
def post(self, request):
|
|
|
|
data = request.data
|
|
|
|
WebsiteConfig.objects.all().delete()
|
|
|
|
config = WebsiteConfig.objects.create(**data)
|
|
|
|
return self.success(WebsiteConfigSerializer(config).data)
|
2016-11-20 11:18:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JudgeServerAPI(APIView):
|
|
|
|
@super_admin_required
|
|
|
|
def get(self, request):
|
|
|
|
judge_server_token = JudgeServerToken.objects.first()
|
|
|
|
if not judge_server_token:
|
|
|
|
token = rand_str(12)
|
|
|
|
JudgeServerToken.objects.create(token=token)
|
|
|
|
else:
|
|
|
|
token = judge_server_token.token
|
|
|
|
servers = JudgeServer.objects.all().order_by("-last_heartbeat")
|
|
|
|
return self.success({"token": token,
|
|
|
|
"servers": JudgeServerSerializer(servers, many=True).data})
|
|
|
|
|
|
|
|
@super_admin_required
|
|
|
|
def delete(self, request):
|
2017-01-24 08:48:39 +00:00
|
|
|
hostname = request.GET.get("hostname")
|
|
|
|
if hostname:
|
|
|
|
JudgeServer.objects.filter(hostname=hostname).delete()
|
2017-01-24 05:22:40 +00:00
|
|
|
return self.success()
|
2016-11-20 11:18:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JudgeServerHeartbeatAPI(CSRFExemptAPIView):
|
|
|
|
@validate_serializer(JudgeServerHeartbeatSerializer)
|
|
|
|
def post(self, request):
|
|
|
|
judge_server_token = JudgeServerToken.objects.first()
|
|
|
|
if not judge_server_token:
|
|
|
|
return self.error("Web server token not set")
|
|
|
|
token = judge_server_token.token
|
|
|
|
data = request.data
|
|
|
|
judge_server_token = request.META.get("HTTP_X_JUDGE_SERVER_TOKEN")
|
|
|
|
if hashlib.sha256(token.encode("utf-8")).hexdigest() != judge_server_token:
|
|
|
|
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 08:25:14 +00:00
|
|
|
server.service_url = service_url
|
2017-01-24 08:22:22 +00:00
|
|
|
server.ip = request.META["REMOTE_ADDR"]
|
2016-11-20 11:18:25 +00: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 08:22:22 +00:00
|
|
|
ip=request.META["REMOTE_ADDR"],
|
2016-11-20 11:18:25 +00:00
|
|
|
service_url=service_url,
|
|
|
|
last_heartbeat=timezone.now(),
|
|
|
|
)
|
|
|
|
return self.success()
|
2017-01-24 05:22:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LanguagesAPI(APIView):
|
|
|
|
def get(self, request):
|
|
|
|
return self.success({"languages": languages, "spj_languages": spj_languages})
|