Skip to content

Commit

Permalink
www.stats: Create a route to access a dashboard by its name
Browse files Browse the repository at this point in the history
  • Loading branch information
rsebille committed Jul 31, 2024
1 parent 5be7770 commit abb183f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions itou/www/stats/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Public stats.
path("", views.stats_public, name="stats_public"),
path("pilotage/<int:dashboard_id>", views.stats_pilotage, name="stats_pilotage"),
path("redirect/<str:dashboard_name>", 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"),
Expand Down
13 changes: 13 additions & 0 deletions itou/www/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
13 changes: 13 additions & 0 deletions tests/www/stats/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit abb183f

Please sign in to comment.