admin: use django cache for admin version (expiry)

This commit is contained in:
Jens Langhammer 2020-07-07 13:12:54 +02:00
parent 2dc1b65718
commit d8f27f595a
1 changed files with 12 additions and 11 deletions

View File

@ -1,6 +1,4 @@
"""passbook administration overview""" """passbook administration overview"""
from functools import lru_cache
from django.core.cache import cache from django.core.cache import cache
from django.shortcuts import redirect, reverse from django.shortcuts import redirect, reverse
from django.views.generic import TemplateView from django.views.generic import TemplateView
@ -15,18 +13,21 @@ from passbook.policies.models import Policy
from passbook.root.celery import CELERY_APP from passbook.root.celery import CELERY_APP
from passbook.stages.invitation.models import Invitation from passbook.stages.invitation.models import Invitation
VERSION_CACHE_KEY = "passbook_latest_version"
@lru_cache
def latest_version() -> Version: def latest_version() -> Version:
"""Get latest release from GitHub, cached""" """Get latest release from GitHub, cached"""
if not cache.get(VERSION_CACHE_KEY):
try: try:
data = get( data = get(
"https://api.github.com/repos/beryju/passbook/releases/latest" "https://api.github.com/repos/beryju/passbook/releases/latest"
).json() ).json()
tag_name = data.get("tag_name") tag_name = data.get("tag_name")
return parse(tag_name.split("/")[1]) cache.set(VERSION_CACHE_KEY, tag_name.split("/")[1], 30)
except RequestException: except (RequestException, IndexError):
return parse("0.0.0") cache.set(VERSION_CACHE_KEY, "0.0.0", 30)
return parse(cache.get(VERSION_CACHE_KEY))
class AdministrationOverviewView(AdminRequiredMixin, TemplateView): class AdministrationOverviewView(AdminRequiredMixin, TemplateView):