Skip to content

Commit

Permalink
fix(SIAE): Evite que le champ is_delisted soit écrasé à chaque synchr…
Browse files Browse the repository at this point in the history
…o avec les emplois (#1108)
  • Loading branch information
raphodn authored Feb 29, 2024
1 parent e29a3d4 commit 57a6093
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions lemarche/siaes/management/commands/sync_with_emplois_inclusion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import re
from datetime import timedelta

from django.conf import settings
from django.contrib.gis.geos import GEOSGeometry
Expand Down Expand Up @@ -37,7 +36,6 @@
"admin_email",
"asp_id",
"is_active",
"is_delisted",
"c1_last_sync_date",
]

Expand Down Expand Up @@ -79,15 +77,6 @@ def set_is_active(siae):
return True


def set_is_delisted(siae):
"""
le-marche field
Helps to track the number of Siae who were set from active to inactive during a sync.
"""
is_active = set_is_active(siae)
return not is_active


class Command(BaseCommand):
"""
This command syncs the list of siae (creates new, updates existing) from les-emplois to le-marché.
Expand Down Expand Up @@ -130,15 +119,12 @@ def handle(self, dry_run=False, **options):
# count before
siae_total_before = Siae.objects.all().count()
siae_active_before = Siae.objects.filter(is_active=True).count()
siae_delisted_before = Siae.objects.filter(is_delisted=True).count()

self.c4_update(c1_list_filtered, dry_run)
self.c4_delist_old_siae(dry_run)

# count after
siae_total_after = Siae.objects.all().count()
siae_active_after = Siae.objects.filter(is_active=True).count()
siae_delisted_after = Siae.objects.filter(is_delisted=True).count()

self.stdout_info("Done ! Some stats...")
created_count = siae_total_after - siae_total_before
Expand All @@ -149,7 +135,6 @@ def handle(self, dry_run=False, **options):
f"Siae updated: {updated_count}",
f"Siae active: before {siae_active_before} / after {siae_active_after}",
f"Siae inactive: before {siae_total_before - siae_active_before} / after {siae_total_after - siae_active_after}", # noqa
f"Siae delisted: before {siae_delisted_before} / after {siae_delisted_after}",
]
self.stdout_messages_success(msg_success)
api_slack.send_message_to_channel("\n".join(msg_success), service_id=settings.SLACK_WEBHOOK_C4_SUPPORT_CHANNEL)
Expand Down Expand Up @@ -180,7 +165,6 @@ def c1_export(self): # noqa C901
"source": c1_siae["source"],
"is_active": set_is_active(c1_siae),
"asp_id": c1_siae["convention_asp_id"],
"is_delisted": set_is_delisted(c1_siae),
"c1_last_sync_date": timezone.now(),
}

Expand Down Expand Up @@ -244,8 +228,6 @@ def c4_update(self, c1_list, dry_run):
except Siae.DoesNotExist:
self.c4_create_siae(c1_siae, dry_run)

self.c4_delist_old_siae(dry_run)

def c4_create_siae(self, c1_siae, dry_run):
"""
Here we create a new Siae with les-emplois data
Expand All @@ -261,9 +243,6 @@ def c4_create_siae(self, c1_siae, dry_run):
c1_siae["contact_email"] = c1_siae["admin_email"] or c1_siae["email"]
c1_siae["contact_phone"] = c1_siae["phone"]

# other fields
c1_siae["is_delisted"] = False

# create object
if not dry_run:
siae = Siae.objects.create(**c1_siae)
Expand All @@ -287,9 +266,6 @@ def c4_update_siae(self, c1_siae, c4_siae, dry_run):
Here we update an existing Siae with a subset of les-emplois data
"""
if not dry_run:
# other fields
# c1_siae["is_delisted"] = True if not c1_siae["convention_is_active"] else False

# keep only certain fields for update
c1_siae_filtered = dict()
for key in UPDATE_FIELDS:
Expand All @@ -298,16 +274,3 @@ def c4_update_siae(self, c1_siae, c4_siae, dry_run):

Siae.objects.filter(c1_id=c4_siae.c1_id).update(**c1_siae_filtered) # avoid updated_at change
# self.stdout_info(f"Siae updated / {c4_siae.id} / {c4_siae.siret}")

def c4_delist_old_siae(self, dry_run):
"""
Which Siae should we delist?
- the existing ones who haven't been updated
- all the ones who have is_active as False
"""
if not dry_run:
date_yesterday = timezone.now() - timedelta(days=1)
Siae.objects.exclude(c1_sync_skip=True).filter(c1_last_sync_date__lt=date_yesterday).update(
is_delisted=True
)
Siae.objects.filter(is_active=False).update(is_delisted=True)

0 comments on commit 57a6093

Please sign in to comment.