Browse Source

Small cleanups in version check logic (#1294)

Rafał Pitoń 5 years ago
parent
commit
a35c1b44cd

+ 2 - 2
misago/graphql/admin/tests/test_version_check.py

@@ -5,7 +5,7 @@ from ariadne import gql
 from requests.exceptions import RequestException
 
 from .... import __version__
-from ..versioncheck import CACHE_KEY, CACHE_LENGTH, resolve_version, get_latest_version
+from ..versioncheck import CACHE_KEY, CACHE_LENGTH, get_latest_version, resolve_version
 
 test_query = gql("{ version { status message description } }")
 
@@ -77,5 +77,5 @@ def test_remote_api_is_not_called_if_version_check_cache_is_available(mocker):
 def test_version_check_cache_is_returned_when_set(mocker):
     mocker.patch("misago.graphql.admin.versioncheck.__released__", True)
     mocker.patch("django.core.cache.cache.get", return_value={"status": "TEST"})
-    api_mock = mock_requests_get(mocker, Mock())
+    mock_requests_get(mocker, Mock())
     assert get_latest_version() == {"status": "TEST"}

+ 9 - 7
misago/graphql/admin/versioncheck.py

@@ -8,7 +8,7 @@ from ... import __released__, __version__
 from .status import Status
 
 CACHE_KEY = "misago_admin_version_check"
-CACHE_LENGTH = 3600 * 8  # 4 hours
+CACHE_LENGTH = 3600 * 4  # 4 hours
 
 version_check = QueryType()
 
@@ -50,17 +50,19 @@ def check_version_with_api():
 
 def get_latest_version():
     data = cache.get(CACHE_KEY)
-
     if not data:
-        api_url = "https://pypi.org/pypi/Misago/json"
-        r = requests.get(api_url)
-        r.raise_for_status()
-        data = r.json()["info"]["version"]
+        data = get_latest_version_from_api()
         cache.set(CACHE_KEY, data, CACHE_LENGTH)
-
     return data
 
 
+def get_latest_version_from_api():
+    api_url = "https://pypi.org/pypi/Misago/json"
+    r = requests.get(api_url)
+    r.raise_for_status()
+    return r.json()["info"]["version"]
+
+
 def compare_versions(current, latest):
     if latest == current:
         return {