Skip to content

Commit

Permalink
Mise à jour de siae.extra_data
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Nov 17, 2024
1 parent de60c04 commit 7b0ab70
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lemarche/crm/management/commands/crm_brevo_sync_companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from lemarche.utils.commands import BaseCommand


ten_days_ago = timezone.now() - timedelta(days=10)
two_weeks_ago = timezone.now() - timedelta(weeks=2)


class Command(BaseCommand):
Expand All @@ -34,11 +34,25 @@ def handle(self, recently_updated: bool, **options):
self.stdout.write(f"Sync Siae > Brevo: we have {Siae.objects.count()} siaes")
# Update only the recently updated siaes
if recently_updated:
siaes_qs = siaes_qs.filter(updated_at__gte=ten_days_ago)
siaes_qs = siaes_qs.filter(updated_at__gte=two_weeks_ago)
self.stdout.write(f"Sync Siae > Brevo: {siaes_qs.count()} recently updated")

# Step 2: loop on the siaes
# Step 2: Add the 90-day limited annotations
siaes_qs = Siae.objects.with_tender_stats(days=90)

# Step 3: loop on the siaes
for index, siae in enumerate(siaes_qs):
new_extra_data = {
"completion_rate": siae.completion_rate,
"recent_tender_email_send_count": siae.tender_email_send_count_annotated,
"recent_tender_detail_click_count": siae.tender_detail_contact_click_count_annotated,
}

# extra_data update if needed
if siae.extra_data != new_extra_data:
siae.extra_data = new_extra_data
siae.save(update_fields=["extra_data"])

api_brevo.create_or_update_company(siae)
if (index % 10) == 0: # avoid API rate-limiting
time.sleep(1)
Expand Down

0 comments on commit 7b0ab70

Please sign in to comment.