From d8a4afdcfaef3090bf62d050e6a89e4450c63b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20S=C3=A9bille?= Date: Thu, 25 Jul 2024 16:15:44 +0200 Subject: [PATCH] www.stats: Create a route to access a dashboard by its name --- itou/www/stats/urls.py | 1 + itou/www/stats/views.py | 13 +++++++++++++ tests/www/stats/test_views.py | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/itou/www/stats/urls.py b/itou/www/stats/urls.py index 75f3529272..8e6302fa72 100644 --- a/itou/www/stats/urls.py +++ b/itou/www/stats/urls.py @@ -10,6 +10,7 @@ # Public stats. path("", views.stats_public, name="stats_public"), path("pilotage/", views.stats_pilotage, name="stats_pilotage"), + path("redirect/", views.stats_redirect, name="redirect"), # Employer stats. path("siae/aci", views.stats_siae_aci, name="stats_siae_aci"), path("siae/etp", views.stats_siae_etp, name="stats_siae_etp"), diff --git a/itou/www/stats/views.py b/itou/www/stats/views.py index 1f4972da57..1dd4fbd6a0 100644 --- a/itou/www/stats/views.py +++ b/itou/www/stats/views.py @@ -17,6 +17,7 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied +from django.http import HttpResponseNotFound, HttpResponseRedirect from django.shortcuts import render from django.urls import reverse from django.views.decorators.clickjacking import xframe_options_exempt @@ -31,6 +32,7 @@ ) from itou.companies import models as companies_models from itou.prescribers.enums import PrescriberOrganizationKind +from itou.users.enums import UserKind from itou.utils import constants as global_constants from itou.utils.apis import metabase as mb from itou.utils.perms.company import get_current_company_or_404 @@ -185,6 +187,17 @@ def stats_pilotage(request, dashboard_id): return render_stats(request=request, context=context, template_name="stats/stats_pilotage.html") +@login_required +def stats_redirect(request, dashboard_name): + match request.user.kind: + case UserKind.LABOR_INSPECTOR: + normalized_organization_kind = request.current_organization.kind.lower().replace(" ", "_") + case _: + return HttpResponseNotFound() + + return HttpResponseRedirect(reverse(f"stats:stats_{normalized_organization_kind}_{dashboard_name}")) + + @login_required def stats_siae_aci(request): """ diff --git a/tests/www/stats/test_views.py b/tests/www/stats/test_views.py index bc9c019649..e79f752236 100644 --- a/tests/www/stats/test_views.py +++ b/tests/www/stats/test_views.py @@ -430,3 +430,16 @@ def test_get_params_aci_asp_ids_for_department_when_only_the_antenna_is_in_the_d assert get_params_aci_asp_ids_for_department(company.department) == { "id_asp_de_la_siae": [company.convention.asp_id] } + + +@pytest.mark.parametrize("dashboard_name", ["ph_prescription", "state", "tension"]) +@pytest.mark.parametrize( + "institution_kind", [InstitutionKind.DGEFP_IAE, InstitutionKind.DREETS_IAE, InstitutionKind.DDETS_IAE] +) +@override_settings(METABASE_SITE_URL="http://metabase.fake", METABASE_SECRET_KEY="foobar") +def test_stats_redirect_for_institution(client, institution_kind, dashboard_name): + institution = InstitutionWithMembershipFactory(kind=institution_kind) + client.force_login(institution.members.get()) + + response = client.get(reverse("stats:redirect", kwargs={"dashboard_name": dashboard_name}), follow=True) + assert response.status_code == 200