Skip to content

Commit

Permalink
Tender update_tender_count_fields command
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 25, 2023
1 parent ff711a1 commit 55bb1e9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lemarche/siaes/management/commands/update_count_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions lemarche/tenders/management/commands/update_tender_count_fields.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 0 additions & 17 deletions lemarche/tenders/migrations/0058_tender_siae_count.py

This file was deleted.

21 changes: 21 additions & 0 deletions lemarche/tenders/migrations/0059_tender_siae_count_updated.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
4 changes: 3 additions & 1 deletion lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55bb1e9

Please sign in to comment.