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 19, 2023
1 parent c95d2d6 commit 23fb1de
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 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
14 changes: 7 additions & 7 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"kind",
"deadline_date",
"start_working_date",
"siae_count_with_link",
"siae_count_annotated_with_link",
# "siae_email_send_count_with_link",
"siae_email_link_click_count_with_link",
"siae_detail_display_count_with_link",
Expand Down Expand Up @@ -173,7 +173,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"survey_transactioned_answer_date",
"validated_at",
"question_count_with_link",
"siae_count_with_link",
"siae_count_annotated_with_link",
"siae_email_send_count_with_link",
"siae_email_link_click_count_with_link",
"siae_detail_display_count_with_link",
Expand Down Expand Up @@ -279,7 +279,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"Structures",
{
"fields": (
"siae_count_with_link",
"siae_count_annotated_with_link",
"siae_email_send_count_with_link",
"siae_email_link_click_count_with_link",
"siae_detail_display_count_with_link",
Expand Down Expand Up @@ -412,12 +412,12 @@ def question_count_with_link(self, tender):
question_count_with_link.short_description = TenderQuestion._meta.verbose_name_plural
question_count_with_link.admin_order_field = "questions_count"

def siae_count_with_link(self, tender):
def siae_count_annotated_with_link(self, tender):
url = reverse("admin:siaes_siae_changelist") + f"?tenders__in={tender.id}"
return format_html(f'<a href="{url}">{getattr(tender, "siae_count", 0)}</a>')
return format_html(f'<a href="{url}">{getattr(tender, "siae_count_annotated", 0)}</a>')

siae_count_with_link.short_description = "Structures concernées"
siae_count_with_link.admin_order_field = "siae_count"
siae_count_annotated_with_link.short_description = "Structures concernées"
siae_count_annotated_with_link.admin_order_field = "siae_count_annotated"

def siae_email_send_count_with_link(self, tender):
url = (
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",
),
),
]
6 changes: 4 additions & 2 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def with_siae_stats(self):
Enrich each Tender with stats on their linked Siae
"""
return self.annotate(
siae_count=Count("siaes", distinct=True),
siae_count_annotated=Count("siaes", distinct=True),
siae_email_send_count=Sum(
Case(When(tendersiae__email_send_date__isnull=False, then=1), default=0, output_field=IntegerField())
),
Expand Down Expand Up @@ -393,7 +393,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 23fb1de

Please sign in to comment.