2018-11-16 12:08:37 +00:00
|
|
|
"""passbook administration overview"""
|
2019-04-29 18:37:44 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.shortcuts import redirect, reverse
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.views.generic import TemplateView
|
2020-06-30 08:23:39 +00:00
|
|
|
from packaging.version import Version, parse
|
|
|
|
from requests import RequestException, get
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook import __version__
|
2018-11-16 08:10:35 +00:00
|
|
|
from passbook.admin.mixins import AdminRequiredMixin
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.core.models import Application, Provider, Source, User
|
2020-05-08 17:46:39 +00:00
|
|
|
from passbook.flows.models import Flow, Stage
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.policies.models import Policy
|
2019-06-25 16:00:54 +00:00
|
|
|
from passbook.root.celery import CELERY_APP
|
2020-05-11 19:58:02 +00:00
|
|
|
from passbook.stages.invitation.models import Invitation
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-07-07 11:12:54 +00:00
|
|
|
VERSION_CACHE_KEY = "passbook_latest_version"
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-06-30 08:23:39 +00:00
|
|
|
def latest_version() -> Version:
|
|
|
|
"""Get latest release from GitHub, cached"""
|
2020-07-07 11:12:54 +00:00
|
|
|
if not cache.get(VERSION_CACHE_KEY):
|
|
|
|
try:
|
|
|
|
data = get(
|
|
|
|
"https://api.github.com/repos/beryju/passbook/releases/latest"
|
|
|
|
).json()
|
|
|
|
tag_name = data.get("tag_name")
|
|
|
|
cache.set(VERSION_CACHE_KEY, tag_name.split("/")[1], 30)
|
|
|
|
except (RequestException, IndexError):
|
|
|
|
cache.set(VERSION_CACHE_KEY, "0.0.0", 30)
|
|
|
|
return parse(cache.get(VERSION_CACHE_KEY))
|
2020-06-30 08:23:39 +00:00
|
|
|
|
|
|
|
|
2018-11-16 08:10:35 +00:00
|
|
|
class AdministrationOverviewView(AdminRequiredMixin, TemplateView):
|
2018-11-16 12:08:37 +00:00
|
|
|
"""Overview View"""
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
template_name = "administration/overview.html"
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-04-29 18:37:44 +00:00
|
|
|
def post(self, *args, **kwargs):
|
2019-04-29 18:42:24 +00:00
|
|
|
"""Handle post (clear cache from modal)"""
|
2019-12-31 11:51:16 +00:00
|
|
|
if "clear" in self.request.POST:
|
2019-04-29 18:37:44 +00:00
|
|
|
cache.clear()
|
2020-05-12 12:49:47 +00:00
|
|
|
return redirect(reverse("passbook_flows:default-authentication"))
|
2019-04-29 18:37:44 +00:00
|
|
|
return self.get(*args, **kwargs)
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-12-31 11:51:16 +00:00
|
|
|
kwargs["application_count"] = len(Application.objects.all())
|
|
|
|
kwargs["policy_count"] = len(Policy.objects.all())
|
2020-09-14 21:19:16 +00:00
|
|
|
kwargs["user_count"] = len(User.objects.all()) - 1 # Remove anonymous user
|
2020-05-16 14:02:42 +00:00
|
|
|
kwargs["provider_count"] = len(Provider.objects.all())
|
|
|
|
kwargs["source_count"] = len(Source.objects.all())
|
2020-05-08 17:46:39 +00:00
|
|
|
kwargs["stage_count"] = len(Stage.objects.all())
|
|
|
|
kwargs["flow_count"] = len(Flow.objects.all())
|
2019-12-31 11:51:16 +00:00
|
|
|
kwargs["invitation_count"] = len(Invitation.objects.all())
|
2020-06-30 08:23:39 +00:00
|
|
|
kwargs["version"] = parse(__version__)
|
|
|
|
kwargs["version_latest"] = latest_version()
|
2019-12-31 11:51:16 +00:00
|
|
|
kwargs["worker_count"] = len(CELERY_APP.control.ping(timeout=0.5))
|
2020-05-16 14:02:42 +00:00
|
|
|
kwargs["providers_without_application"] = Provider.objects.filter(
|
|
|
|
application=None
|
|
|
|
)
|
2020-02-21 13:20:16 +00:00
|
|
|
kwargs["policies_without_binding"] = len(
|
2020-09-14 13:44:33 +00:00
|
|
|
Policy.objects.filter(bindings__isnull=True, promptstage__isnull=True)
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
kwargs["cached_policies"] = len(cache.keys("policy_*"))
|
2020-07-07 11:13:15 +00:00
|
|
|
kwargs["cached_flows"] = len(cache.keys("flow_*"))
|
2018-11-11 12:41:48 +00:00
|
|
|
return super().get_context_data(**kwargs)
|