diff --git a/aidants_connect_habilitation/admin.py b/aidants_connect_habilitation/admin.py index 026abff24..5b135bd08 100644 --- a/aidants_connect_habilitation/admin.py +++ b/aidants_connect_habilitation/admin.py @@ -1,3 +1,5 @@ +from zoneinfo import ZoneInfo + from django.conf import settings from django.contrib import admin, messages from django.contrib.admin import ( @@ -14,10 +16,14 @@ from django.shortcuts import render from django.template import loader from django.urls import path, reverse +from django.utils import timezone from django.utils.html import linebreaks, urlize from django.utils.safestring import mark_safe from django_reverse_admin import ReverseInlineModelAdmin, ReverseModelAdmin +from import_export import resources +from import_export.admin import ImportExportMixin +from import_export.fields import Field from aidants_connect.admin import VisibleToAdminMetier, VisibleToTechAdmin, admin_site from aidants_connect_common.admin import DepartmentFilter, RegionFilter @@ -163,8 +169,51 @@ def queryset(self, request, queryset): return queryset +class OrganisationRequestResource(resources.ModelResource): + request_created_at = Field() + request_updated_at = Field() + + def dehydrate_request_created_at(self, orga_request): + return timezone.make_naive(orga_request.created_at, ZoneInfo("Europe/Paris")) + + def dehydrate_request_updated_at(self, orga_request): + return timezone.make_naive(orga_request.updated_at, ZoneInfo("Europe/Paris")) + + class Meta: + model = OrganisationRequest + fields = ( + "request_created_at", + "request_updated_at", + "status", + "name", + "siret", + "legal_category", + "type__name", + "type_other", + "address", + "zipcode", + "city", + "is_private_org", + "france_services_label", + "france_services_number", + "issuer__last_name", + "issuer__first_name", + "issuer__profession", + "issuer__phone", + "manager__email", + "manager__last_name", + "manager__first_name", + "manager__profession", + "manager__email", + "manager__phone", + ) + + @admin.register(OrganisationRequest, site=admin_site) -class OrganisationRequestAdmin(VisibleToAdminMetier, ReverseModelAdmin): +class OrganisationRequestAdmin( + ImportExportMixin, VisibleToAdminMetier, ReverseModelAdmin +): + resource_classes = [OrganisationRequestResource] list_filter = ( RegionFilter, DepartmentFilter, diff --git a/aidants_connect_habilitation/tests/test_admin_resources.py b/aidants_connect_habilitation/tests/test_admin_resources.py new file mode 100644 index 000000000..d73fc56ba --- /dev/null +++ b/aidants_connect_habilitation/tests/test_admin_resources.py @@ -0,0 +1,26 @@ +from django.contrib.admin.sites import AdminSite +from django.test import TestCase, tag +from django.test.client import RequestFactory + +from aidants_connect_habilitation.admin import OrganisationRequestAdmin +from aidants_connect_habilitation.models import OrganisationRequest +from aidants_connect_habilitation.tests.factories import OrganisationRequestFactory + + +@tag("admin") +class OrganisationRequestResourceTestCase(TestCase): + + @classmethod + def setUpTestData(cls): + cls.rf = RequestFactory() + cls.orga_request_1 = OrganisationRequestFactory(name="MAIRIE 1") + cls.orga_request_2 = OrganisationRequestFactory(name="MAIRIE 2") + + def test_export_organisation_request(self): + request = self.rf.get("/", {}) + orga_requests = OrganisationRequest.objects.all() + orga_request_admin = OrganisationRequestAdmin(OrganisationRequest, AdminSite()) + data = orga_request_admin.get_data_for_export(request, orga_requests) + self.assertEqual(len(data), 2) + self.assertEqual(data[0][3], "MAIRIE 1") + self.assertEqual(data[1][3], "MAIRIE 2")