Skip to content

Commit

Permalink
fix(Entreprises): ajuste le script de rattachement des utilisateurs e…
Browse files Browse the repository at this point in the history
…n fonction de leur email (ajoute un @) (#1406)
  • Loading branch information
raphodn authored Aug 27, 2024
1 parent df8e4cc commit daebacf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
8 changes: 8 additions & 0 deletions lemarche/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions lemarche/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")
UserFactory(email="[email protected]")
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])
Expand Down

0 comments on commit daebacf

Please sign in to comment.