diff --git a/clevercloud/companies_update_users.sh b/clevercloud/companies_update_users_and_count_fields.sh similarity index 82% rename from clevercloud/companies_update_users.sh rename to clevercloud/companies_update_users_and_count_fields.sh index ded074e0..41e03d63 100755 --- a/clevercloud/companies_update_users.sh +++ b/clevercloud/companies_update_users_and_count_fields.sh @@ -19,5 +19,5 @@ fi # $APP_HOME is set by default by clever cloud. cd $APP_HOME -# django-admin set_company_users --with-count -django-admin set_company_users --only-add --with-count +django-admin set_company_users --only-add +django-admin update_company_count_fields diff --git a/clevercloud/cron.json b/clevercloud/cron.json index bfa2d3cc..f3a35814 100644 --- a/clevercloud/cron.json +++ b/clevercloud/cron.json @@ -11,7 +11,7 @@ "20 7 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh", "25 7 * * 1 $ROOT/clevercloud/siaes_update_count_fields.sh", "30 7 * * 1 $ROOT/clevercloud/siaes_update_super_badge_field.sh", - "50 7 * * 1 $ROOT/clevercloud/companies_update_users.sh", + "50 7 * * 1 $ROOT/clevercloud/companies_update_users_and_count_fields.sh", "55 7 * * 1 $ROOT/clevercloud/crm_brevo_sync.sh", "0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh", "0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh", diff --git a/lemarche/companies/management/commands/set_company_users.py b/lemarche/companies/management/commands/set_company_users.py index b74e5445..8f6da4f6 100644 --- a/lemarche/companies/management/commands/set_company_users.py +++ b/lemarche/companies/management/commands/set_company_users.py @@ -12,7 +12,6 @@ class Command(BaseCommand): Usage: - poetry run python manage.py set_company_users --dry-run - poetry run python manage.py set_company_users --only-add - - poetry run python manage.py set_company_users --only-add --with-count - poetry run python manage.py set_company_users """ @@ -21,9 +20,6 @@ def add_arguments(self, parser): parser.add_argument( "--only-add", dest="only_add", action="store_true", help="Only add new users, don't delete existing" ) - parser.add_argument( - "--with-count", dest="with_count", action="store_true", help="Update user_count at the end" - ) def handle(self, *args, **options): self.stdout_info("-" * 80) @@ -72,13 +68,5 @@ def handle(self, *args, **options): f"Companies with user: before {old_companies_with_user_count} / after {new_companies_with_user_count} / {new_companies_with_user_count-old_companies_with_user_count}", # noqa ] - if options["with_count"]: - self.stdout_info("-" * 80) - self.stdout_info("Updating Company.user_count fields") - for company in Company.objects.prefetch_related("users").all(): - company.user_count = company.users.count() - company.save() - msg_success.append("Also updated Company.user_count field") - self.stdout_messages_success(msg_success) api_slack.send_message_to_channel("\n".join(msg_success)) diff --git a/lemarche/companies/management/commands/update_company_count_fields.py b/lemarche/companies/management/commands/update_company_count_fields.py new file mode 100644 index 00000000..71709619 --- /dev/null +++ b/lemarche/companies/management/commands/update_company_count_fields.py @@ -0,0 +1,44 @@ +from lemarche.companies.models import Company +from lemarche.utils.apis import api_slack +from lemarche.utils.commands import BaseCommand + + +class Command(BaseCommand): + """ + Goal: update the '_count' fields of each Company + + Usage: + - poetry run python manage.py update_company_count_fields + """ + + def handle(self, *args, **options): + self.stdout_messages_info("Updating Company count fields...") + + # Step 1a: build the queryset + company_queryset = Company.objects.has_email_domain().with_user_stats() + self.stdout_messages_info(f"Found {company_queryset.count()} companies with an email_domain") + + # Step 1b: init fields to update + update_fields = Company.FIELDS_STATS_COUNT + self.stdout_messages_info(f"Fields to update: {update_fields}") + + # Step 2: loop on each Company with an email domain + progress = 0 + for index, company in enumerate(company_queryset): + company.user_count = company.user_count_annotated + company.user_tender_count = company.user_tender_count_annotated + + # Step 3: update count fields + company.save(update_fields=update_fields) + + progress += 1 + if (progress % 50) == 0: + self.stdout_info(f"{progress}...") + + msg_success = [ + "----- Company count fields -----", + f"Done! Processed {company_queryset.count()} companies with an email_domain", + f"Fields updated: {update_fields}", + ] + self.stdout_messages_success(msg_success) + api_slack.send_message_to_channel("\n".join(msg_success)) diff --git a/lemarche/companies/migrations/0004_company_user_tender_count.py b/lemarche/companies/migrations/0004_company_user_tender_count.py new file mode 100644 index 00000000..2a0fa8d0 --- /dev/null +++ b/lemarche/companies/migrations/0004_company_user_tender_count.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.9 on 2024-03-20 09:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("companies", "0003_company_user_count"), + ] + + operations = [ + migrations.AddField( + model_name="company", + name="user_tender_count", + field=models.IntegerField(default=0, verbose_name="Nombre de besoins déposés par les utilisateurs"), + ), + ] diff --git a/lemarche/companies/models.py b/lemarche/companies/models.py index 140339f3..39313e77 100644 --- a/lemarche/companies/models.py +++ b/lemarche/companies/models.py @@ -19,7 +19,7 @@ def with_user_stats(self): class Company(models.Model): - FIELDS_STATS_COUNT = ["user_count"] + FIELDS_STATS_COUNT = ["user_count", "user_tender_count"] FIELDS_STATS_TIMESTAMPS = ["created_at", "updated_at"] READONLY_FIELDS = FIELDS_STATS_COUNT + FIELDS_STATS_TIMESTAMPS @@ -40,6 +40,7 @@ class Company(models.Model): # stats user_count = models.IntegerField("Nombre d'utilisateurs", default=0) + user_tender_count = models.IntegerField("Nombre de besoins déposés par les utilisateurs", default=0) created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now) updated_at = models.DateTimeField(verbose_name="Date de modification", auto_now=True)