diff --git a/lemarche/siaes/admin.py b/lemarche/siaes/admin.py index 73bf6532a..23140e478 100644 --- a/lemarche/siaes/admin.py +++ b/lemarche/siaes/admin.py @@ -184,8 +184,6 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin): "logs_display", # "import_raw_object", "import_raw_object_display", - "created_at", - "updated_at", ] formfield_overrides = { ChoiceArrayField: {"widget": forms.CheckboxSelectMultiple(attrs={"class": "custom-checkbox-select-multiple"})}, @@ -231,13 +229,13 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin): ) }, ), - ("Données C2", {"fields": Siae.READONLY_FIELDS_FROM_C2}), - ("Données API Entreprise", {"fields": Siae.READONLY_FIELDS_FROM_API_ENTREPRISE}), + ("Données C2", {"fields": Siae.FIELDS_FROM_C2}), + ("Données API Entreprise", {"fields": Siae.FIELDS_FROM_API_ENTREPRISE}), ( "Données API QPV (Quartier prioritaire de la politique de la ville)", - {"fields": Siae.READONLY_FIELDS_FROM_QPV}, + {"fields": Siae.FIELDS_FROM_QPV}, ), - ("Données API ZRR (Zone de revitalisation rurale)", {"fields": Siae.READONLY_FIELDS_FROM_ZRR}), + ("Données API ZRR (Zone de revitalisation rurale)", {"fields": Siae.FIELDS_FROM_ZRR}), ( "Détails", { diff --git a/lemarche/siaes/management/commands/update_siae_count_fields.py b/lemarche/siaes/management/commands/update_siae_count_fields.py index 113719e77..aaf5d60a1 100644 --- a/lemarche/siaes/management/commands/update_siae_count_fields.py +++ b/lemarche/siaes/management/commands/update_siae_count_fields.py @@ -4,25 +4,6 @@ from lemarche.utils.commands import BaseCommand -SIAE_COUNT_FIELDS = [ - "user_count", - "sector_count", - "network_count", - "group_count", - "offer_count", - "client_reference_count", - "label_count", - "image_count", - "etablissement_count", - "completion_rate", - "tender_count", - "tender_email_send_count", - "tender_email_link_click_count", - "tender_detail_display_count", - "tender_detail_contact_click_count", -] - - class Command(BaseCommand): """ Goal: update the '_count' fields of each Siae @@ -47,9 +28,9 @@ def handle(self, *args, **options): # Step 1a: build the queryset siae_queryset = ( - Siae.objects.prefetch_related( - "users", "sectors", "networks", "groups", "offers", "client_references", "labels", "images" - ) + Siae.objects.prefetch_many_to_many() + .prefetch_many_to_one() + .prefetch("users", "groups", "labels") .with_tender_stats() .all() ) @@ -58,7 +39,7 @@ def handle(self, *args, **options): 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 + update_fields = options["fields"] if options["fields"] else Siae.FIELDS_STATS_COUNT self.stdout_messages_info(f"Fields to update: {update_fields}") # Step 2: loop on each Siae diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index e586fe86a..ec32be9a5 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -491,7 +491,7 @@ def with_employees_stats(self): class Siae(models.Model): - READONLY_FIELDS_FROM_C1 = [ + FIELDS_FROM_C1 = [ "name", "slug", # generated from 'name' "brand", @@ -518,14 +518,14 @@ class Siae(models.Model): "c1_last_sync_date", "source", ] - READONLY_FIELDS_FROM_C2 = [ + FIELDS_FROM_C2 = [ "c2_etp_count", "c2_etp_count_date_saisie", "c2_etp_count_last_sync_date", ] - READONLY_FIELDS_FROM_QPV = ["is_qpv", "qpv_name", "qpv_code", "api_qpv_last_sync_date"] - READONLY_FIELDS_FROM_ZRR = ["is_zrr", "zrr_name", "zrr_code", "api_zrr_last_sync_date"] - READONLY_FIELDS_FROM_API_ENTREPRISE = [ + FIELDS_FROM_QPV = ["is_qpv", "qpv_name", "qpv_code", "api_qpv_last_sync_date"] + FIELDS_FROM_ZRR = ["is_zrr", "zrr_name", "zrr_code", "api_zrr_last_sync_date"] + FIELDS_FROM_API_ENTREPRISE = [ "api_entreprise_forme_juridique", "api_entreprise_forme_juridique_code", "api_entreprise_entreprise_last_sync_date", @@ -537,14 +537,27 @@ class Siae(models.Model): "api_entreprise_ca_date_fin_exercice", "api_entreprise_exercice_last_sync_date", ] - READONLY_FIELDS_STATS = ["etablissement_count", "signup_date", "content_filled_basic_date", "completion_rate"] + FIELDS_STATS_COUNT = [ + "user_count", + "sector_count", + "network_count", + "group_count", + "offer_count", + "client_reference_count", + "label_count", + "image_count", + "etablissement_count", + "completion_rate", + "tender_count", + "tender_email_send_count", + "tender_email_link_click_count", + "tender_detail_display_count", + "tender_detail_contact_click_count", + ] + FIELDS_STATS_TIMESTAMPS = ["signup_date", "content_filled_basic_date", "created_at", "updated_at"] + FIELDS_STATS = FIELDS_STATS_COUNT + FIELDS_STATS_TIMESTAMPS + ["completion_rate"] READONLY_FIELDS = ( - READONLY_FIELDS_FROM_C1 - + READONLY_FIELDS_FROM_C2 - + READONLY_FIELDS_FROM_QPV - + READONLY_FIELDS_FROM_ZRR - + READONLY_FIELDS_FROM_API_ENTREPRISE - + READONLY_FIELDS_STATS + FIELDS_FROM_C1 + FIELDS_FROM_C2 + FIELDS_FROM_QPV + FIELDS_FROM_ZRR + FIELDS_FROM_API_ENTREPRISE + FIELDS_STATS ) TRACK_UPDATE_FIELDS = [