From daebacf126a644b99cd2247677002f388fc78a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Tue, 27 Aug 2024 11:00:17 +0200 Subject: [PATCH] fix(Entreprises): ajuste le script de rattachement des utilisateurs en fonction de leur email (ajoute un @) (#1406) --- .../companies/management/commands/set_company_users.py | 2 +- lemarche/users/models.py | 8 ++++++++ lemarche/users/tests.py | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lemarche/companies/management/commands/set_company_users.py b/lemarche/companies/management/commands/set_company_users.py index 8f6da4f60..bc73a9a3f 100644 --- a/lemarche/companies/management/commands/set_company_users.py +++ b/lemarche/companies/management/commands/set_company_users.py @@ -46,7 +46,7 @@ def handle(self, *args, **options): for company in companies_with_email_domain_list: company_email_domain_list_users = list() for company_email_domain in company.email_domain_list: - company_email_domain_users = User.objects.filter(email__iendswith=company_email_domain) + company_email_domain_users = User.objects.has_email_domain(company_email_domain) company_email_domain_list_users += company_email_domain_users if not options["dry_run"]: if options["only_add"]: diff --git a/lemarche/users/models.py b/lemarche/users/models.py index d5d6c7cf3..3ca6a83be 100644 --- a/lemarche/users/models.py +++ b/lemarche/users/models.py @@ -43,6 +43,11 @@ def has_partner_network(self): def has_api_key(self): return self.filter(api_key__isnull=False) + def has_email_domain(self, email_domain): + if not email_domain.startswith("@"): + email_domain = f"@{email_domain}" + return self.filter(email__iendswith=email_domain) + def with_siae_stats(self): return self.prefetch_related("siaes").annotate(siae_count_annotated=Count("siaes", distinct=True)) @@ -114,6 +119,9 @@ def has_partner_network(self): def has_api_key(self): return self.get_queryset().has_api_key() + def has_email_domain(self, email_domain): + return self.get_queryset().has_email_domain(email_domain) + def with_siae_stats(self): return self.get_queryset().with_siae_stats() diff --git a/lemarche/users/tests.py b/lemarche/users/tests.py index a8ecedddb..335480fdd 100644 --- a/lemarche/users/tests.py +++ b/lemarche/users/tests.py @@ -86,6 +86,14 @@ def test_has_api_key(self): self.assertEqual(User.objects.count(), 1 + 1) self.assertEqual(User.objects.has_api_key().count(), 1) + def test_has_email_domain(self): + UserFactory(email="test@ain.fr") + UserFactory(email="test@plateau-urbain.fr") + self.assertEqual(User.objects.count(), 1 + 2) + for EMAIL_DOMAIN in ["ain.fr", "@ain.fr"]: + with self.subTest(email_domain=EMAIL_DOMAIN): + self.assertEqual(User.objects.has_email_domain(email_domain=EMAIL_DOMAIN).count(), 1) + def test_with_siae_stats(self): user_2 = UserFactory() SiaeFactory(users=[user_2])