add new version api

This commit is contained in:
zema1 2018-01-02 20:05:33 +08:00 committed by virusdefender
parent 8454b36792
commit 914c4727fc
5 changed files with 54 additions and 3 deletions

View File

@ -6,6 +6,7 @@ from django.utils import timezone
from options.options import SysOptions
from utils.api.tests import APITestCase
from .models import JudgeServer
from .views import CheckNewVersionAPI
class SMTPConfigTest(APITestCase):
@ -153,3 +154,27 @@ class TestCasePruneAPITest(APITestCase):
resp = self.client.delete(self.url)
self.assertSuccess(resp)
mocked_delete_one.assert_called_once_with(valid_id)
class CheckNewVersionAPITest(APITestCase):
def setUp(self):
self.url = self.reverse("check_new_version_api")
self.create_super_admin()
self.latest_data = {"update": [
{
"version": "2099-12-25",
"level": 1,
"title": "Update at 2099-12-25",
"details": ["test get", ]
}
]}
@mock.patch("conf.views.requests.get", autospec=True)
def test_get_latest_version(self, mocked_requests):
resp = self.client.get(self.url)
self.assertSuccess(resp)
mocked_requests.assert_called_once()
def test_get_version(self):
version = CheckNewVersionAPI().get_latest_version(self.latest_data)
self.assertListEqual(version, [2099, 12, 25])

View File

@ -1,6 +1,7 @@
from django.conf.urls import url
from ..views import SMTPAPI, JudgeServerAPI, WebsiteConfigAPI, TestCasePruneAPI, SMTPTestAPI
from ..views import CheckNewVersionAPI
urlpatterns = [
url(r"^smtp/?$", SMTPAPI.as_view(), name="smtp_admin_api"),
@ -8,4 +9,5 @@ urlpatterns = [
url(r"^website/?$", WebsiteConfigAPI.as_view(), name="website_config_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"^new_version/?$", CheckNewVersionAPI.as_view(), name="check_new_version_api"),
]

View File

@ -2,6 +2,9 @@ import os
import re
import hashlib
import shutil
import json
import requests
from requests.exceptions import RequestException
from django.utils import timezone
from django.conf import settings
@ -156,7 +159,7 @@ class TestCasePruneAPI(APIView):
@super_admin_required
def get(self, request):
"""
return isolated test_case list
return orphan test_case list
"""
ret_data = []
dir_to_be_removed = self.get_orphan_ids()
@ -190,3 +193,24 @@ class TestCasePruneAPI(APIView):
test_case_dir = os.path.join(settings.TEST_CASE_DIR, id)
if os.path.isdir(test_case_dir):
shutil.rmtree(test_case_dir, ignore_errors=True)
class CheckNewVersionAPI(APIView):
@staticmethod
def get_latest_version(data):
return list(map(lambda x: int(x), data["update"][0]["version"].split("-")))
def get(self, request):
try:
resp = requests.get("https://raw.githubusercontent.com/QingdaoU/OnlineJudge/master/docs/data.json",
timeout=3)
remote = resp.json()
except (RequestException, ValueError):
return self.success()
with open("docs/data.json", "r") as f:
local = json.load(f)
remote_version = self.get_latest_version(remote)
local_version = self.get_latest_version(local)
if remote_version > local_version:
return self.success(remote["update"][0])
return self.success()

View File

@ -2,7 +2,7 @@
"update": [
{
"version": "2017-12-25",
"level": 1,
"level": "Recommend",
"title": "Update at 2017-12-25",
"details": [
"Fix some issues under IE/Edge",

View File

@ -423,7 +423,7 @@ class MakeContestProblemPublicAPIView(APIView):
return self.error("Problem does not exist")
if not problem.contest or problem.is_public:
return self.error("Alreay be a public problem")
return self.error("Already be a public problem")
problem.is_public = True
problem.save()
# https://docs.djangoproject.com/en/1.11/topics/db/queries/#copying-model-instances