From e7c80af14aea3950866a9ae885ee8aec6477534f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Fri, 19 Apr 2024 14:09:02 +0200 Subject: [PATCH] =?UTF-8?q?feat(Entreprises):=20commande=20interactive=20p?= =?UTF-8?q?our=20cr=C3=A9er=20de=20nouvelles=20entreprises=20=C3=A0=20part?= =?UTF-8?q?ir=20des=20infos=20des=20acheteurs=20(#1165)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../management/commands/create_companies.py | 81 +++++++++++++++++++ lemarche/utils/emails.py | 27 +++++++ 2 files changed, 108 insertions(+) create mode 100644 lemarche/companies/management/commands/create_companies.py diff --git a/lemarche/companies/management/commands/create_companies.py b/lemarche/companies/management/commands/create_companies.py new file mode 100644 index 000000000..eaa931d96 --- /dev/null +++ b/lemarche/companies/management/commands/create_companies.py @@ -0,0 +1,81 @@ +import operator +from functools import reduce + +from django.db.models import Q + +from lemarche.companies.models import Company +from lemarche.users.models import User +from lemarche.utils.apis import api_slack +from lemarche.utils.commands import BaseCommand +from lemarche.utils.emails import GENERIC_EMAIL_DOMAIN_SUFFIX_LIST + + +class Command(BaseCommand): + """ + Usage: + - poetry run python manage.py create_companies + + Common rules: + "ville", "commune" --> Commune (138) + "agglo" --> Communauté d'agglomération (121) + "metropole" --> Métropole (123) + "@cc" --> Communauté de communes (131) + "départe" --> Département (31) + "région" --> Région (111) + "hospital" --> CHU (36) + "pnr", "parc" --> Parc naturel régional (195) + "@ac-" --> Académie (177) + "@univ-" --> Université (139) + ... + """ + + def handle(self, *args, **options): + self.stdout_info("-" * 80) + + user_buyer_without_company = User.objects.filter(kind="BUYER", company_id__isnull=True) + self.stdout_info(f"Users without company: {user_buyer_without_company.count()}") + user_buyer_without_company_with_tender = user_buyer_without_company.filter(tenders__isnull=False) + self.stdout_info(f"Users without company with tender: {user_buyer_without_company_with_tender.count()}") + + user_qs = ( + user_buyer_without_company.filter( + reduce(operator.and_, (~Q(email__endswith=x) for x in GENERIC_EMAIL_DOMAIN_SUFFIX_LIST)) + ) + .exclude(company_name__icontains="particulier") + .exclude(company_name="") + ) + self.stdout_info(f"Users final queryset (extra filtering): {user_qs.count()}") + companies_created = 0 + + for user in user_qs: + user_email_suffix = user.email.split("@")[1] + # check that no company already exist + company_qs = Company.objects.filter(email_domain_list__contains=[user_email_suffix]) + if not company_qs.count(): + result = input(f"Create company: {user_email_suffix} / {user.company_name} ? (y//n) ") + if result == "y": + company = Company.objects.create(name=user.company_name, email_domain_list=[user_email_suffix]) + company.users.add(user) + self.stdout_info(f"Company created for {user_email_suffix}") + companies_created += 1 + elif result.isdigit(): + company = Company.objects.get(id=int(result)) + company.email_domain_list += [user_email_suffix] + company.save() + company.users.add(user) + self.stdout_info(f"User added to existing company {company.name} (and email_domain_list updated)") + else: + pass + + elif company_qs.count() == 1: + company = company_qs.first() + company.users.add(user) + self.stdout_info(f"User added to existing company {company.name}") + + msg_success = [ + "----- Create companies -----", + f"Done! Processed {user_qs.count()} users without company", + f"Created {companies_created} companies", + ] + self.stdout_messages_success(msg_success) + api_slack.send_message_to_channel("\n".join(msg_success)) diff --git a/lemarche/utils/emails.py b/lemarche/utils/emails.py index 6cc437bfb..f0f6c6dec 100644 --- a/lemarche/utils/emails.py +++ b/lemarche/utils/emails.py @@ -9,6 +9,33 @@ from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX +GENERIC_EMAIL_DOMAIN_SUFFIX_LIST = [ + "gmail.com", + "orange.fr", + "wanadoo.fr", + "hotmail.fr", + "hotmail.com", + "live.fr", + "yahoo.fr", + "yahoo.com", + "outlook.fr", + "outlook.com", + "laposte.net", + "free.fr", + "sfr.fr", + "icloud.com", + "yandex.com", + "msn.com", + "cegetel.net", + "bbox.fr", + "yopmail.com", + "neuf.fr", + "numericable.fr", + "gmx.fr", + "googlemail.com", +] + + def anonymize_email(email): email_split = email.split("@") email_username = email_split[0]