mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-11-04 14:49:58 +08:00
add dashboard api
This commit is contained in:
parent
914c4727fc
commit
b388c5dd03
@ -6,7 +6,6 @@ from django.utils import timezone
|
|||||||
from options.options import SysOptions
|
from options.options import SysOptions
|
||||||
from utils.api.tests import APITestCase
|
from utils.api.tests import APITestCase
|
||||||
from .models import JudgeServer
|
from .models import JudgeServer
|
||||||
from .views import CheckNewVersionAPI
|
|
||||||
|
|
||||||
|
|
||||||
class SMTPConfigTest(APITestCase):
|
class SMTPConfigTest(APITestCase):
|
||||||
@ -156,9 +155,9 @@ class TestCasePruneAPITest(APITestCase):
|
|||||||
mocked_delete_one.assert_called_once_with(valid_id)
|
mocked_delete_one.assert_called_once_with(valid_id)
|
||||||
|
|
||||||
|
|
||||||
class CheckNewVersionAPITest(APITestCase):
|
class ReleaseNoteAPITest(APITestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.url = self.reverse("check_new_version_api")
|
self.url = self.reverse("get_release_notes_api")
|
||||||
self.create_super_admin()
|
self.create_super_admin()
|
||||||
self.latest_data = {"update": [
|
self.latest_data = {"update": [
|
||||||
{
|
{
|
||||||
@ -169,12 +168,17 @@ class CheckNewVersionAPITest(APITestCase):
|
|||||||
}
|
}
|
||||||
]}
|
]}
|
||||||
|
|
||||||
@mock.patch("conf.views.requests.get", autospec=True)
|
def test_get_versions(self):
|
||||||
def test_get_latest_version(self, mocked_requests):
|
|
||||||
resp = self.client.get(self.url)
|
resp = self.client.get(self.url)
|
||||||
self.assertSuccess(resp)
|
self.assertSuccess(resp)
|
||||||
mocked_requests.assert_called_once()
|
|
||||||
|
|
||||||
def test_get_version(self):
|
|
||||||
version = CheckNewVersionAPI().get_latest_version(self.latest_data)
|
class DashboardInfoAPITest(APITestCase):
|
||||||
self.assertListEqual(version, [2099, 12, 25])
|
def setUp(self):
|
||||||
|
self.url = self.reverse("dashboard_info_api")
|
||||||
|
self.create_admin()
|
||||||
|
|
||||||
|
def test_get_info(self):
|
||||||
|
resp = self.client.get(self.url)
|
||||||
|
self.assertSuccess(resp)
|
||||||
|
self.assertEqual(resp.data["data"]["user_count"], 1)
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from ..views import SMTPAPI, JudgeServerAPI, WebsiteConfigAPI, TestCasePruneAPI, SMTPTestAPI
|
from ..views import SMTPAPI, JudgeServerAPI, WebsiteConfigAPI, TestCasePruneAPI, SMTPTestAPI
|
||||||
from ..views import CheckNewVersionAPI
|
from ..views import ReleaseNotesAPI, DashboardInfoAPI
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r"^smtp/?$", SMTPAPI.as_view(), name="smtp_admin_api"),
|
url(r"^smtp/?$", SMTPAPI.as_view(), name="smtp_admin_api"),
|
||||||
@ -9,5 +9,6 @@ urlpatterns = [
|
|||||||
url(r"^website/?$", WebsiteConfigAPI.as_view(), name="website_config_api"),
|
url(r"^website/?$", WebsiteConfigAPI.as_view(), name="website_config_api"),
|
||||||
url(r"^judge_server/?$", JudgeServerAPI.as_view(), name="judge_server_api"),
|
url(r"^judge_server/?$", JudgeServerAPI.as_view(), name="judge_server_api"),
|
||||||
url(r"^prune_test_case/?$", TestCasePruneAPI.as_view(), name="prune_test_case_api"),
|
url(r"^prune_test_case/?$", TestCasePruneAPI.as_view(), name="prune_test_case_api"),
|
||||||
url(r"^new_version/?$", CheckNewVersionAPI.as_view(), name="check_new_version_api"),
|
url(r"^versions/?$", ReleaseNotesAPI.as_view(), name="get_release_notes_api"),
|
||||||
|
url(r"^dashboard_info", DashboardInfoAPI.as_view(), name="dashboard_info_api"),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -3,7 +3,9 @@ import re
|
|||||||
import hashlib
|
import hashlib
|
||||||
import shutil
|
import shutil
|
||||||
import json
|
import json
|
||||||
|
import pytz
|
||||||
import requests
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
from requests.exceptions import RequestException
|
from requests.exceptions import RequestException
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@ -11,11 +13,14 @@ from django.conf import settings
|
|||||||
|
|
||||||
from account.decorators import super_admin_required
|
from account.decorators import super_admin_required
|
||||||
from problem.models import Problem
|
from problem.models import Problem
|
||||||
|
from account.models import User
|
||||||
|
from submission.models import Submission
|
||||||
|
from contest.models import Contest
|
||||||
from judge.dispatcher import process_pending_task
|
from judge.dispatcher import process_pending_task
|
||||||
from judge.languages import languages, spj_languages
|
from judge.languages import languages, spj_languages
|
||||||
from options.options import SysOptions
|
from options.options import SysOptions
|
||||||
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
||||||
from utils.shortcuts import send_email
|
from utils.shortcuts import send_email, get_env
|
||||||
from utils.xss_filter import XSSHtml
|
from utils.xss_filter import XSSHtml
|
||||||
from .models import JudgeServer
|
from .models import JudgeServer
|
||||||
from .serializers import (CreateEditWebsiteConfigSerializer,
|
from .serializers import (CreateEditWebsiteConfigSerializer,
|
||||||
@ -195,22 +200,34 @@ class TestCasePruneAPI(APIView):
|
|||||||
shutil.rmtree(test_case_dir, ignore_errors=True)
|
shutil.rmtree(test_case_dir, ignore_errors=True)
|
||||||
|
|
||||||
|
|
||||||
class CheckNewVersionAPI(APIView):
|
class ReleaseNotesAPI(APIView):
|
||||||
@staticmethod
|
|
||||||
def get_latest_version(data):
|
|
||||||
return list(map(lambda x: int(x), data["update"][0]["version"].split("-")))
|
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
try:
|
try:
|
||||||
resp = requests.get("https://raw.githubusercontent.com/QingdaoU/OnlineJudge/master/docs/data.json",
|
resp = requests.get("https://raw.githubusercontent.com/QingdaoU/OnlineJudge/master/docs/data.json",
|
||||||
timeout=3)
|
timeout=3)
|
||||||
remote = resp.json()
|
releases = resp.json()
|
||||||
except (RequestException, ValueError):
|
except (RequestException, ValueError):
|
||||||
return self.success()
|
return self.success()
|
||||||
with open("docs/data.json", "r") as f:
|
with open("docs/data.json", "r") as f:
|
||||||
local = json.load(f)
|
local_version = json.load(f)["update"][0]["version"]
|
||||||
remote_version = self.get_latest_version(remote)
|
releases["local_version"] = local_version
|
||||||
local_version = self.get_latest_version(local)
|
return self.success(releases)
|
||||||
if remote_version > local_version:
|
|
||||||
return self.success(remote["update"][0])
|
|
||||||
return self.success()
|
class DashboardInfoAPI(APIView):
|
||||||
|
def get(self, request):
|
||||||
|
today = datetime.today()
|
||||||
|
today_submission_count = Submission.objects.filter(
|
||||||
|
create_time__gte=datetime(today.year, today.month, today.day, 0, 0, tzinfo=pytz.UTC)).count()
|
||||||
|
recent_contest_count = Contest.objects.exclude(end_time__lt=timezone.now()).count()
|
||||||
|
judge_server_count = len(list(filter(lambda x: x.status == "normal", JudgeServer.objects.all())))
|
||||||
|
return self.success({
|
||||||
|
"user_count": User.objects.count(),
|
||||||
|
"recent_contest_count": recent_contest_count,
|
||||||
|
"today_submission_count": today_submission_count,
|
||||||
|
"judge_server_count": judge_server_count,
|
||||||
|
"env": {
|
||||||
|
"FORCE_HTTPS": get_env("FORCE_HTTPS", default=False),
|
||||||
|
"STATIC_CDN_HOST": get_env("STATIC_CDN_HOST", default="")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|||||||
@ -1,9 +1,4 @@
|
|||||||
import os
|
from utils.shortcuts import get_env
|
||||||
|
|
||||||
|
|
||||||
def get_env(name, default=""):
|
|
||||||
return os.environ.get(name, default)
|
|
||||||
|
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import datetime
|
import datetime
|
||||||
import random
|
import random
|
||||||
@ -76,3 +77,7 @@ def send_email(smtp_config, from_name, to_email, to_name, subject, content):
|
|||||||
password=smtp_config["password"],
|
password=smtp_config["password"],
|
||||||
port=smtp_config["port"],
|
port=smtp_config["port"],
|
||||||
tls=smtp_config["tls"])
|
tls=smtp_config["tls"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_env(name, default=""):
|
||||||
|
return os.environ.get(name, default)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user