-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tender update_tender_count_fields command
- Loading branch information
Showing
5 changed files
with
84 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
lemarche/tenders/management/commands/update_tender_count_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
lemarche/tenders/migrations/0059_tender_siae_count_updated.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters