diff --git a/clevercloud/cron.json b/clevercloud/cron.json index db5f36fe7..5291c2770 100644 --- a/clevercloud/cron.json +++ b/clevercloud/cron.json @@ -9,7 +9,8 @@ "15 7 * * 1 $ROOT/clevercloud/siaes_update_api_qpv_fields.sh", "20 7 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh", "25 7 * * 1 $ROOT/clevercloud/siaes_update_count_fields.sh", - "30 7 * * 1 $ROOT/clevercloud/companies_update_users.sh", + "30 7 * * 1 $ROOT/clevercloud/siaes_update_super_badge_field.sh", + "50 7 * * 1 $ROOT/clevercloud/companies_update_users.sh", "0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh", "0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh", "30 8 * * * $ROOT/clevercloud/tenders_send_author_transactioned_question_emails.sh", diff --git a/clevercloud/siaes_update_super_badge_field.sh b/clevercloud/siaes_update_super_badge_field.sh new file mode 100755 index 000000000..74fd92a9f --- /dev/null +++ b/clevercloud/siaes_update_super_badge_field.sh @@ -0,0 +1,23 @@ +#!/bin/bash -l + +# Update siae super_badge field + +# Do not run if this env var is not set: +if [[ -z "$CRON_UPDATE_SIAE_SUPER_BADGE_FIELD_ENABLED" ]]; then + echo "CRON_UPDATE_SIAE_SUPER_BADGE_FIELD_ENABLED not set. Exiting..." + exit 0 +fi + +# About clever cloud cronjobs: +# https://www.clever-cloud.com/doc/tools/crons/ + +if [[ "$INSTANCE_NUMBER" != "0" ]]; then + echo "Instance number is ${INSTANCE_NUMBER}. Stop here." + exit 0 +fi + +# $APP_HOME is set by default by clever cloud. +cd $APP_HOME + +# Run only on the first Monday of each month +django-admin update_siae_super_badge_field --day-of-week 0 --day-of-month first diff --git a/lemarche/siaes/management/commands/update_siae_count_fields.py b/lemarche/siaes/management/commands/update_siae_count_fields.py index f39fcf1a2..f09c263e1 100644 --- a/lemarche/siaes/management/commands/update_siae_count_fields.py +++ b/lemarche/siaes/management/commands/update_siae_count_fields.py @@ -8,7 +8,7 @@ class Command(BaseCommand): """ Goal: update the '_count' fields of each Siae - Note: these fields should be updated automatically on each Siae save() + Note: some of these fields are updated on each Siae save() Usage: python manage.py update_siae_count_fields diff --git a/lemarche/siaes/management/commands/update_siae_super_badge_field.py b/lemarche/siaes/management/commands/update_siae_super_badge_field.py new file mode 100644 index 000000000..08bca1e1c --- /dev/null +++ b/lemarche/siaes/management/commands/update_siae_super_badge_field.py @@ -0,0 +1,80 @@ +import calendar + +from django.core.management.base import CommandError +from django.utils import timezone + +from lemarche.siaes.models import Siae +from lemarche.utils.apis import api_slack +from lemarche.utils.commands import BaseCommand + + +class Command(BaseCommand): + """ + Goal: update the 'super_badge' field of each Siae + + Note: some of these fields are updated on each Siae save() + + Usage: + python manage.py update_siae_super_badge_field + python manage.py update_siae_super_badge_field --id 1 + python manage.py update_siae_super_badge_field --day-of-week 0 --day-of-month last + """ + + def add_arguments(self, parser): + parser.add_argument("--id", type=int, default=None, help="Indiquer l'ID d'une structure") + parser.add_argument( + "--day-of-week", + dest="day_of_week", + type=int, + help="Lundi = 0 ; Dimanche = 6", + ) + parser.add_argument( + "--day-of-month", + dest="day_of_month", + type=str, + help="'first' for the first weekday of the month ; 'last' for the last weekday of the month", + ) + + def handle(self, *args, **options): + self.stdout_messages_info("Updating Siae super_badge field...") + + if options["day_of_week"] is not None: + if options["day_of_week"] != timezone.now().weekday(): + raise CommandError("Day of week not compatible with day_of_week parameter. Stopping.") + + if options["day_of_month"] is not None: + current_year = timezone.now().year + current_month = timezone.now().month + current_day = timezone.now().day + current_month_day_count = calendar.monthrange(year=current_year, month=current_month)[1] + if (options["day_of_month"] == "first") and (current_day > 7): + raise CommandError("Not the first weekday of the month. Stopping.") + elif (options["day_of_month"] == "last") and (current_month_day_count - current_day >= 7): + raise CommandError("Not the last weekday of the month. Stopping.") + + siae_with_super_badge_count_before = Siae.objects.filter(super_badge=True).count() + + # Step 1: build the queryset + siae_queryset = Siae.objects.all() + if options["id"]: + siae_queryset = siae_queryset.filter(id=options["id"]) + self.stdout_messages_info(f"Found {siae_queryset.count()} siaes") + + # Step 2: loop on each Siae + progress = 0 + for index, siae in enumerate(siae_queryset): + # Step 3: update super_badge field + siae.set_super_badge() + + progress += 1 + if (progress % 500) == 0: + self.stdout_info(f"{progress}...") + + siae_with_super_badge_count_after = Siae.objects.filter(super_badge=True).count() + msg_success = [ + "----- Siae super_badge field -----", + f"Done! Processed {siae_queryset.count()} siaes", + f"Siaes with badge: before {siae_with_super_badge_count_before} / after {siae_with_super_badge_count_after}", # noqa + ] + self.stdout_messages_success(msg_success) + api_slack.send_message_to_channel("\n".join(msg_success))