2018-11-16 12:08:37 +00:00
|
|
|
"""passbook administration overview"""
|
2020-09-16 21:54:55 +00:00
|
|
|
from typing import Union
|
|
|
|
|
2020-10-03 22:28:58 +00:00
|
|
|
from django.conf import settings
|
2020-11-21 18:10:05 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2019-04-29 18:37:44 +00:00
|
|
|
from django.core.cache import cache
|
2020-11-21 18:10:05 +00:00
|
|
|
from django.http.request import HttpRequest
|
|
|
|
from django.http.response import HttpResponse
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views.generic import FormView, TemplateView
|
2020-09-16 21:54:55 +00:00
|
|
|
from packaging.version import LegacyVersion, Version, parse
|
2020-10-16 18:00:17 +00:00
|
|
|
from structlog import get_logger
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook import __version__
|
2020-11-21 18:10:05 +00:00
|
|
|
from passbook.admin.forms.overview import FlowCacheClearForm, PolicyCacheClearForm
|
2018-11-16 08:10:35 +00:00
|
|
|
from passbook.admin.mixins import AdminRequiredMixin
|
2020-09-26 00:16:35 +00:00
|
|
|
from passbook.admin.tasks import VERSION_CACHE_KEY, update_latest_version
|
2020-10-05 20:09:57 +00:00
|
|
|
from passbook.core.models import Provider, User
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.policies.models import Policy
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-10-16 18:00:17 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
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
|
|
|
|
2020-09-26 00:16:35 +00:00
|
|
|
def get_latest_version(self) -> Union[LegacyVersion, Version]:
|
|
|
|
"""Get latest version from cache"""
|
|
|
|
version_in_cache = cache.get(VERSION_CACHE_KEY)
|
|
|
|
if not version_in_cache:
|
2020-10-03 22:28:58 +00:00
|
|
|
if not settings.DEBUG:
|
|
|
|
update_latest_version.delay()
|
2020-09-26 00:16:35 +00:00
|
|
|
return parse(__version__)
|
|
|
|
return parse(version_in_cache)
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2019-12-31 11:51:16 +00:00
|
|
|
kwargs["policy_count"] = len(Policy.objects.all())
|
2020-09-14 21:35:01 +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())
|
2020-06-30 08:23:39 +00:00
|
|
|
kwargs["version"] = parse(__version__)
|
2020-09-26 00:16:35 +00:00
|
|
|
kwargs["version_latest"] = self.get_latest_version()
|
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)
|
2020-11-21 18:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyCacheClearView(AdminRequiredMixin, SuccessMessageMixin, FormView):
|
|
|
|
"""View to clear Policy cache"""
|
|
|
|
|
|
|
|
form_class = PolicyCacheClearForm
|
|
|
|
|
|
|
|
template_name = "generic/form_non_model.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:overview")
|
|
|
|
success_message = _("Successfully cleared Policy cache")
|
|
|
|
|
|
|
|
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
|
|
|
keys = cache.keys("policy_*")
|
|
|
|
cache.delete_many(keys)
|
|
|
|
LOGGER.debug("Cleared Policy cache", keys=len(keys))
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class FlowCacheClearView(AdminRequiredMixin, SuccessMessageMixin, FormView):
|
|
|
|
"""View to clear Flow cache"""
|
|
|
|
|
|
|
|
form_class = FlowCacheClearForm
|
|
|
|
|
|
|
|
template_name = "generic/form_non_model.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:overview")
|
|
|
|
success_message = _("Successfully cleared Flow cache")
|
|
|
|
|
|
|
|
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
|
|
|
keys = cache.keys("flow_*")
|
|
|
|
cache.delete_many(keys)
|
|
|
|
LOGGER.debug("Cleared flow cache", keys=len(keys))
|
|
|
|
return super().post(request, *args, **kwargs)
|