Skip to content

Commit

Permalink
Task to update Siae super_badge field
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 6, 2023
1 parent c84a5a8 commit e026731
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
3 changes: 2 additions & 1 deletion clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions clevercloud/siaes_update_super_badge_field.sh
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit e026731

Please sign in to comment.