Skip to content

Commit

Permalink
Rename tendersiae_reminders to tendersiae_contacted_reminders for cla…
Browse files Browse the repository at this point in the history
…rification. ref #723
  • Loading branch information
raphodn committed Apr 24, 2023
1 parent 3978376 commit 23e9f6a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"45 7 * * 1 $ROOT/clevercloud/siaes_update_api_qpv_fields.sh",
"0 8 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh",
"0 9 * * * $ROOT/clevercloud/siaes_send_user_request_reminders.sh",
"5 9 * * * $ROOT/clevercloud/tenders_send_siae_reminders.sh",
"5 9 * * * $ROOT/clevercloud/tenders_send_siae_contacted_reminders.sh",
"10 9 * * * $ROOT/clevercloud/tenders_send_author_incremental.sh"
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Send TenderSiae reminders

# Do not run if this env var is not set:
if [[ -z "$CRON_TENDER_SEND_SIAE_REMINDERS_ENABLED" ]]; then
echo "CRON_TENDER_SEND_SIAE_REMINDERS_ENABLED not set. Exiting..."
if [[ -z "$CRON_TENDER_SEND_SIAE_CONTACTED_REMINDERS_ENABLED" ]]; then
echo "CRON_TENDER_SEND_SIAE_CONTACTED_REMINDERS_ENABLED not set. Exiting..."
exit 0
fi

Expand All @@ -19,4 +19,4 @@ fi
# $APP_HOME is set by default by clever cloud.
cd $APP_HOME

django-admin send_siae_reminder_emails --days-since-email-send-date 2
django-admin send_siae_contacted_reminder_emails --days-since-email-send-date 2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone

from lemarche.tenders.models import Tender, TenderSiae
from lemarche.www.tenders.tasks import send_tender_reminder_email_to_siaes
from lemarche.www.tenders.tasks import send_tender_contacted_reminder_email_to_siaes


class Command(BaseCommand):
Expand All @@ -13,10 +13,10 @@ class Command(BaseCommand):
When? J+2 (but doesn't run on weekends!)
Usage:
python manage.py send_siae_reminder_emails --dry-run
python manage.py send_siae_reminder_emails --days-since-email-send-date 2
python manage.py send_siae_reminder_emails --tender-id 1
python manage.py send_siae_reminder_emails
python manage.py send_siae_contacted_reminder_emails --dry-run
python manage.py send_siae_contacted_reminder_emails --days-since-email-send-date 2
python manage.py send_siae_contacted_reminder_emails --tender-id 1
python manage.py send_siae_contacted_reminder_emails
"""

def add_arguments(self, parser):
Expand Down Expand Up @@ -51,29 +51,35 @@ def handle(self, dry_run=False, **options):
# gte_days_ago = gte_days_ago+2 to account for Saturday & Sunday
if current_weekday == 0:
gte_days_ago = timezone.now() - timedelta(days=options["days_since_email_send_date"] + 1 + 2)
tendersiae_reminder_list = TenderSiae.objects.email_click_reminder(
tendersiae_contacted_reminder_list = TenderSiae.objects.email_click_reminder(
gte_days_ago=gte_days_ago, lt_days_ago=lt_days_ago
)

if options["tender_id"]:
tendersiae_reminder_list = tendersiae_reminder_list.filter(tender_id=options["tender_id"])
self.stdout.write(f"Found {tendersiae_reminder_list.count()} TenderSiaes to remind")
tendersiae_contacted_reminder_list = tendersiae_contacted_reminder_list.filter(
tender_id=options["tender_id"]
)
self.stdout.write(f"Found {tendersiae_contacted_reminder_list.count()} TenderSiaes to remind")

if not dry_run:
self.stdout.write("-" * 80)
self.stdout.write("Step 2: Send emails for each tender")
tender_id_list = tendersiae_reminder_list.values_list("tender_id", flat=True).distinct().order_by()
tender_id_list = (
tendersiae_contacted_reminder_list.values_list("tender_id", flat=True).distinct().order_by()
)
self.stdout.write(f"{tender_id_list.count()} tenders concerned")
for tender_id in tender_id_list:
tender = Tender.objects.get(id=tender_id)
tender_tendersiae_reminder_list = tendersiae_reminder_list.filter(tender_id=tender_id)
tender_tendersiae_contacted_reminder_list = tendersiae_contacted_reminder_list.filter(
tender_id=tender_id
)
tender_tendersiae = TenderSiae.objects.filter(tender_id=tender_id).filter(
email_send_date__isnull=False
)
self.stdout.write(
f"Tender {tender_id}: {tender_tendersiae_reminder_list.count()} TenderSiaes to remind (out of {tender_tendersiae.count()})" # noqa
f"Tender {tender_id}: {tender_tendersiae_contacted_reminder_list.count()} TenderSiaes to remind (out of {tender_tendersiae.count()})" # noqa
)
send_tender_reminder_email_to_siaes(
send_tender_contacted_reminder_email_to_siaes(
tender, days_since_email_send_date=options["days_since_email_send_date"]
)
self.stdout.write("Emails sent")
Expand Down
16 changes: 9 additions & 7 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def send_tender_email_to_siae(tender: Tender, siae: Siae, email_subject: str, em
)


def send_tender_reminder_email_to_siaes(tender: Tender, days_since_email_send_date=2, send_on_weekends=False):
def send_tender_contacted_reminder_email_to_siaes(
tender: Tender, days_since_email_send_date=2, send_on_weekends=False
):
email_subject = f"Un {tender.get_kind_display().lower()} pour vous sur le Marché de l'inclusion"

current_weekday = timezone.now().weekday()
Expand All @@ -157,26 +159,26 @@ def send_tender_reminder_email_to_siaes(tender: Tender, days_since_email_send_da
if current_weekday == 0 and not send_on_weekends:
# Monday: special case (need to account for Saturday & Sunday)
gte_days_ago = timezone.now() - timedelta(days=days_since_email_send_date + 1 + 2)
tendersiae_reminder_list = TenderSiae.objects.filter(tender_id=tender.id).email_click_reminder(
tendersiae_contacted_reminder_list = TenderSiae.objects.filter(tender_id=tender.id).email_click_reminder(
gte_days_ago=gte_days_ago, lt_days_ago=lt_days_ago
)

for tendersiae in tendersiae_reminder_list:
for tendersiae in tendersiae_contacted_reminder_list:
# send to siae 'contact_email'
send_tender_reminder_email_to_siae(tendersiae, email_subject, days_since_email_send_date)
send_tender_contacted_reminder_email_to_siae(tendersiae, email_subject, days_since_email_send_date)

# log email batch
siaes_reminder_log_item = {
"action": f"email_siaes_reminder_{days_since_email_send_date}d",
"email_subject": email_subject,
"email_count": tendersiae_reminder_list.count(),
"email_count": tendersiae_contacted_reminder_list.count(),
"email_timestamp": timezone.now().isoformat(),
}
tender.logs.append(siaes_reminder_log_item)
tender.save()


def send_tender_reminder_email_to_siae(tendersiae: TenderSiae, email_subject, days_since_email_send_date):
def send_tender_contacted_reminder_email_to_siae(tendersiae: TenderSiae, email_subject, days_since_email_send_date):
recipient_list = whitelist_recipient_list([tendersiae.siae.contact_email])
if recipient_list:
recipient_email = recipient_list[0] if recipient_list else ""
Expand Down Expand Up @@ -204,7 +206,7 @@ def send_tender_reminder_email_to_siae(tendersiae: TenderSiae, email_subject, da

# log email
log_item = {
"action": f"email_siae_reminder_{days_since_email_send_date}d",
"action": f"email_siae_contacted_reminder_{days_since_email_send_date}d",
"email_to": recipient_email,
"email_subject": email_subject,
# "email_body": email_body,
Expand Down

0 comments on commit 23e9f6a

Please sign in to comment.