From 55bb1e9fe4297acc0c7b958abec1f69c55ccc03a Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 19 Oct 2023 11:44:03 +0200 Subject: [PATCH] Tender update_tender_count_fields command --- .../commands/update_count_fields.py | 4 +- .../commands/update_tender_count_fields.py | 58 +++++++++++++++++++ .../migrations/0058_tender_siae_count.py | 17 ------ .../0059_tender_siae_count_updated.py | 21 +++++++ lemarche/tenders/models.py | 4 +- 5 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 lemarche/tenders/management/commands/update_tender_count_fields.py delete mode 100644 lemarche/tenders/migrations/0058_tender_siae_count.py create mode 100644 lemarche/tenders/migrations/0059_tender_siae_count_updated.py diff --git a/lemarche/siaes/management/commands/update_count_fields.py b/lemarche/siaes/management/commands/update_count_fields.py index e06a82ecf..5f4280e65 100644 --- a/lemarche/siaes/management/commands/update_count_fields.py +++ b/lemarche/siaes/management/commands/update_count_fields.py @@ -39,13 +39,13 @@ def add_arguments(self, parser): def handle(self, *args, **options): self.stdout_messages_info("Updating Siae count fields...") - # Step 1a: build Siae queryset + # Step 1a: build the queryset siae_queryset = Siae.objects.prefetch_related( "users", "sectors", "networks", "groups", "offers", "client_references", "labels", "images" ).all() if options["id"]: siae_queryset = siae_queryset.filter(id=options["id"]) - self.stdout_messages_info(f"Found {siae_queryset.count()} Siae") + self.stdout_messages_info(f"Found {siae_queryset.count()} siaes") # Step 1b: init fields to update update_fields = options["fields"] if options["fields"] else SIAE_COUNT_FIELDS diff --git a/lemarche/tenders/management/commands/update_tender_count_fields.py b/lemarche/tenders/management/commands/update_tender_count_fields.py new file mode 100644 index 000000000..e5939cf4c --- /dev/null +++ b/lemarche/tenders/management/commands/update_tender_count_fields.py @@ -0,0 +1,58 @@ +from lemarche.siaes.models import Tender +from lemarche.utils.commands import BaseCommand + + +TENDER_COUNT_FIELDS = [ + "siae_count", +] + + +class Command(BaseCommand): + """ + Goal: update the '_count' fields of each Tender + + Usage: + python manage.py update_tender_count_fields + python manage.py update_tender_count_fields --id 1 + python manage.py update_tender_count_fields --id 1 --fields siae_count + python manage.py update_tender_count_fields --id 1 --fields siae_count --fields siae_count + """ + + def add_arguments(self, parser): + parser.add_argument("--id", type=int, default=None, help="Indiquer l'ID d'un besoin") + parser.add_argument( + "--fields", action="append", default=[], help="Filtrer sur les champs count à mettre à jour" + ) + + def handle(self, *args, **options): + self.stdout_messages_info("Updating Tender count fields...") + + # Step 1a: build the queryset + tender_queryset = Tender.objects.with_siae_stats().all() + if options["id"]: + tender_queryset = tender_queryset.filter(id=options["id"]) + self.stdout_messages_info(f"Found {tender_queryset.count()} tenders") + + # Step 1b: init fields to update + update_fields = options["fields"] if options["fields"] else TENDER_COUNT_FIELDS + self.stdout_messages_info(f"Fields to update: {update_fields}") + + # Step 2: loop on each Tender + progress = 0 + for index, tender in enumerate(tender_queryset): + # M2M + tender.siae_count = tender.siae_count_annotated + + # Step 3: update count fields + tender.save(update_fields=update_fields) + + progress += 1 + if (progress % 500) == 0: + self.stdout_info(f"{progress}...") + + msg_success = [ + "----- Tender count fields -----", + f"Done! Processed {tender_queryset.count()} tenders", + f"Fields updated: {update_fields}", + ] + self.stdout_messages_success(msg_success) diff --git a/lemarche/tenders/migrations/0058_tender_siae_count.py b/lemarche/tenders/migrations/0058_tender_siae_count.py deleted file mode 100644 index 936ce9605..000000000 --- a/lemarche/tenders/migrations/0058_tender_siae_count.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 4.2.2 on 2023-10-03 09:24 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - dependencies = [ - ("tenders", "0057_alter_tender_siae_transactioned"), - ] - - operations = [ - migrations.AddField( - model_name="tender", - name="siae_count", - field=models.IntegerField(default=0, verbose_name="Nombre de structures concernées"), - ), - ] diff --git a/lemarche/tenders/migrations/0059_tender_siae_count_updated.py b/lemarche/tenders/migrations/0059_tender_siae_count_updated.py new file mode 100644 index 000000000..8af0e07fa --- /dev/null +++ b/lemarche/tenders/migrations/0059_tender_siae_count_updated.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.2 on 2023-10-19 09:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("tenders", "0058_tenderstepsdata"), + ] + + operations = [ + migrations.AddField( + model_name="tender", + name="siae_count", + field=models.IntegerField( + default=0, + help_text="Champ recalculé à intervalles réguliers", + verbose_name="Nombre de structures concernées", + ), + ), + ] diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index cbf924c0c..d9c587bb9 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -395,7 +395,9 @@ class Tender(models.Model): ) # stats - siae_count = models.IntegerField("Nombre de structures concernées", default=0) + siae_count = models.IntegerField( + "Nombre de structures concernées", help_text="Champ recalculé à intervalles réguliers", default=0 + ) published_at = models.DateTimeField("Date de publication", blank=True, null=True) siae_list_last_seen_date = models.DateTimeField( "Date de dernière visite de l'auteur sur la page 'structures intéressées'", blank=True, null=True