Skip to content

Commit

Permalink
Ajoute les filtres par région et statut dans l'admin "habilitation" #501
Browse files Browse the repository at this point in the history
  • Loading branch information
tut-tuuut authored Jan 19, 2022
2 parents 0f63599 + 4eaf9fa commit 95bc8a6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 75 deletions.
35 changes: 34 additions & 1 deletion aidants_connect/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import operator
from functools import reduce

from admin_honeypot.admin import LoginAttemptAdmin as HoneypotLoginAttemptAdmin
from admin_honeypot.models import LoginAttempt as HoneypotLoginAttempt
from django.contrib.admin import SimpleListFilter
from django.db.models import Q
from django_celery_beat.admin import (
ClockedScheduleAdmin,
PeriodicTaskAdmin,
Expand All @@ -14,7 +18,11 @@
)
from django_otp.admin import OTPAdminSite
from magicauth.models import MagicToken
from aidants_connect_web.models import DatavizRegion, DatavizDepartment
from aidants_connect_web.models import (
DatavizRegion,
DatavizDepartment,
DatavizDepartmentsToRegion,
)

admin_site = OTPAdminSite(OTPAdminSite.name)
admin_site.login_template = "aidants_connect_web/admin/login.html"
Expand Down Expand Up @@ -68,12 +76,37 @@ class RegionFilter(SimpleListFilter):
title = "Région"

parameter_name = "region"
filter_parameter_name = "zipcode"

def lookups(self, request, model_admin):
return [(r.id, r.name) for r in DatavizRegion.objects.all()] + [
("other", "Autre")
]

def queryset(self, request, queryset):
region_id = self.value()

if not region_id:
return

if region_id == "other":
return queryset.filter(**{self.filter_parameter_name: 0})

region = DatavizRegion.objects.get(id=region_id)
d2r = DatavizDepartmentsToRegion.objects.filter(region=region)
qgroup = reduce(
operator.or_,
(
Q(
**{
f"{self.filter_parameter_name}__startswith": d.department.normalize_zipcode() # noqa: E501
}
)
for d in d2r.all()
),
)
return queryset.filter(qgroup)


class DepartmentFilter(SimpleListFilter):
title = "Département"
Expand Down
9 changes: 8 additions & 1 deletion aidants_connect_habilitation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from django.conf import settings

from aidants_connect.admin import admin_site, VisibleToAdminMetier
from aidants_connect.admin import (
admin_site,
VisibleToAdminMetier,
RegionFilter,
DepartmentFilter,
)
from aidants_connect_habilitation.models import (
AidantRequest,
Issuer,
Expand Down Expand Up @@ -32,6 +37,8 @@ class MessageInline(VisibleToAdminMetier, StackedInline):


class OrganisationRequestAdmin(VisibleToAdminMetier, ModelAdmin):
list_filter = ("status", RegionFilter, DepartmentFilter)
list_display = ("name", "issuer", "status")
raw_id_fields = ("issuer",)
readonly_fields = ("public_service_delegation_attestation",)
inlines = (
Expand Down
76 changes: 5 additions & 71 deletions aidants_connect_web/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
import operator
from collections.abc import Collection
from functools import reduce

from django.contrib import messages
from django.contrib.admin import ModelAdmin, TabularInline, SimpleListFilter
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.db.models import Q, QuerySet
from django.db.models import QuerySet
from django.http import HttpResponseRedirect, HttpResponseNotAllowed, HttpRequest
from django.shortcuts import render
from django.urls import reverse, path
Expand Down Expand Up @@ -47,8 +45,6 @@
Organisation,
Usager,
CarteTOTP,
DatavizRegion,
DatavizDepartmentsToRegion,
)

logger = logging.getLogger()
Expand Down Expand Up @@ -140,32 +136,6 @@ class Meta:
model = Organisation


class OrgansationDepartmentFilter(DepartmentFilter):
pass


class OrganisationRegionFilter(RegionFilter):
def queryset(self, request, queryset):
region_id = self.value()

if not region_id:
return

if region_id == "other":
return queryset.filter(zipcode=0)

region = DatavizRegion.objects.get(id=region_id)
d2r = DatavizDepartmentsToRegion.objects.filter(region=region)
qgroup = reduce(
operator.or_,
(
Q(zipcode__startswith=d.department.normalize_zipcode())
for d in d2r.all()
),
)
return queryset.filter(qgroup)


class WithoutDatapassIdFilter(SimpleListFilter):
title = "Numéro de demande Datapass"

Expand Down Expand Up @@ -206,8 +176,8 @@ class OrganisationAdmin(ImportMixin, VisibleToAdminMetier, ModelAdmin):
list_filter = (
"is_active",
WithoutDatapassIdFilter,
OrganisationRegionFilter,
OrgansationDepartmentFilter,
RegionFilter,
DepartmentFilter,
)

# For bulk import
Expand Down Expand Up @@ -389,25 +359,7 @@ class AidantDepartmentFilter(DepartmentFilter):


class AidantRegionFilter(RegionFilter):
def queryset(self, request, queryset):
region_id = self.value()

if not region_id:
return

if region_id == "other":
return queryset.filter(organisations__zipcode=0)

region = DatavizRegion.objects.get(id=region_id)
d2r = DatavizDepartmentsToRegion.objects.filter(region=region)
qgroup = reduce(
operator.or_,
(
Q(organisations__zipcode__startswith=d.department.zipcode)
for d in d2r.all()
),
)
return queryset.filter(qgroup)
filter_parameter_name = "organisations__zipcode"


class AidantAdmin(ImportExportMixin, VisibleToAdminMetier, DjangoUserAdmin):
Expand Down Expand Up @@ -611,25 +563,7 @@ class HabilitationDepartmentFilter(DepartmentFilter):


class HabilitationRequestRegionFilter(RegionFilter):
def queryset(self, request, queryset):
region_id = self.value()

if not region_id:
return

if region_id == "other":
return queryset.filter(organisation__zipcode=0)

region = DatavizRegion.objects.get(id=region_id)
d2r = DatavizDepartmentsToRegion.objects.filter(region=region)
qgroup = reduce(
operator.or_,
(
Q(organisation__zipcode__startswith=d.department.zipcode)
for d in d2r.all()
),
)
return queryset.filter(qgroup)
filter_parameter_name = "organisation__zipcode"


class HabilitationRequestAdmin(ExportMixin, VisibleToAdminMetier, ModelAdmin):
Expand Down
33 changes: 31 additions & 2 deletions aidants_connect_web/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.test import tag, TestCase

from aidants_connect.admin import DepartmentFilter
from aidants_connect.admin import DepartmentFilter, RegionFilter
from aidants_connect_web.admin import OrganisationAdmin
from aidants_connect_web.models import Organisation
from aidants_connect_web.tests.factories import OrganisationFactory


@tag("admin")
class DeparmentFilterTests(TestCase):
class DepartmentFilterTests(TestCase):
def test_generate_filter_list(self):
result = DepartmentFilter.generate_filter_list()
self.assertEqual(len(result), 102)
Expand Down Expand Up @@ -43,3 +43,32 @@ def test_queryset(self):
queryset_other = other_filter.queryset(None, Organisation.objects.all())
self.assertEqual(1, queryset_other.count())
self.assertEqual("0", queryset_other[0].zipcode)


@tag("admin")
class RegionFilterTests(TestCase):
def test_queryset(self):
OrganisationFactory(zipcode="13013")
OrganisationFactory(zipcode="13013")
OrganisationFactory(zipcode="20000")
OrganisationFactory(zipcode="0")
corse_filter = RegionFilter(
None, {"region": "5"}, Organisation, OrganisationAdmin
)
queryset_corse = corse_filter.queryset(None, Organisation.objects.all())
self.assertEqual(1, queryset_corse.count())
self.assertEqual("20000", queryset_corse[0].zipcode)

paca_filter = RegionFilter(
None, {"region": "17"}, Organisation, OrganisationAdmin
)
queryset_paca = paca_filter.queryset(None, Organisation.objects.all())
self.assertEqual(2, queryset_paca.count())
self.assertEqual("13013", queryset_paca[0].zipcode)

other_filter = RegionFilter(
None, {"region": "other"}, Organisation, OrganisationAdmin
)
queryset_other = other_filter.queryset(None, Organisation.objects.all())
self.assertEqual(1, queryset_other.count())
self.assertEqual("0", queryset_other[0].zipcode)

0 comments on commit 95bc8a6

Please sign in to comment.