Skip to content

Commit

Permalink
Siae readonly fields cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 6, 2023
1 parent 69ad7c1 commit ea06a27
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
10 changes: 4 additions & 6 deletions lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})},
Expand Down Expand Up @@ -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",
{
Expand Down
27 changes: 4 additions & 23 deletions lemarche/siaes/management/commands/update_siae_count_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
)
Expand 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
Expand Down
37 changes: 25 additions & 12 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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 = [
Expand Down

0 comments on commit ea06a27

Please sign in to comment.