-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Dépôt de besoins): envoi d'un email à l'auteur d'un dépôt de bes…
…oin avec 5 ESI (#1167)
- Loading branch information
1 parent
9f37452
commit 3775153
Showing
9 changed files
with
332 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
clevercloud/tenders_send_author_list_of_super_siaes_emails.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash -l | ||
|
||
# Find tender without insterested siae and send email to author with top 5 siaes | ||
|
||
# Do not run if this env var is not set: | ||
if [[ -z "$CRON_TENDER_SEND_AUTHOR_LIST_OF_SUPER_SIAES_EMAILS_ENABLED" ]]; then | ||
echo "CRON_TENDER_SEND_AUTHOR_LIST_OF_SUPER_SIAES_EMAILS_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 | ||
|
||
django-admin send_author_list_of_super_siaes_emails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
lemarche/tenders/management/commands/send_author_list_of_super_siaes_emails.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from datetime import timedelta | ||
|
||
from django.utils import timezone | ||
|
||
from lemarche.tenders.models import Tender | ||
from lemarche.utils.commands import BaseCommand | ||
from lemarche.www.tenders.tasks import send_super_siaes_email_to_author | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Daily script to check tender without insterested siae, | ||
if it was sent first time more than two days ago, send email to author with list of five siaes with super badge | ||
When? J+2 (but doesn't run on weekends!) | ||
Usage: | ||
python manage.py send_author_list_of_super_siaes_emails --dry-run | ||
python manage.py send_author_list_of_super_siaes_emails --days-since-tender-sent-date 2 | ||
python manage.py send_author_list_of_super_siaes_emails --tender-id 1 | ||
python manage.py send_author_list_of_super_siaes_emails | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--days-since-tender-sent-date", | ||
dest="days_since_tender_sent_date", | ||
type=int, | ||
default=1, | ||
help="Laps de temps depuis la date du premier envoi (first_sent_at)", | ||
) | ||
parser.add_argument( | ||
"--tender-id", dest="tender_id", type=int, default=None, help="Restreindre sur un besoin donné" | ||
) | ||
parser.add_argument("--dry-run", dest="dry_run", action="store_true", help="Dry run, no sends") | ||
|
||
def handle(self, dry_run=False, **options): | ||
self.stdout_info("Script to send Super Siae to tender author...") | ||
|
||
current_weekday = timezone.now().weekday() | ||
if current_weekday > 4: | ||
self.stdout_error("Weekend... Stopping. Come back on Monday :)") | ||
else: | ||
self.stdout_messages_info("Step 1: Find Tender") | ||
self.stdout_messages_info( | ||
f"- where sent J-{options['days_since_tender_sent_date']} and no siae interested" | ||
) | ||
|
||
lt_days_ago = timezone.now() - timedelta(days=options["days_since_tender_sent_date"]) | ||
gte_days_ago = timezone.now() - timedelta(days=options["days_since_tender_sent_date"] + 1) | ||
# The script doesn't run on weekends | ||
if current_weekday == 0: | ||
gte_days_ago = gte_days_ago - timedelta(days=2) | ||
|
||
tender_list = Tender.objects.with_siae_stats().filter( | ||
first_sent_at__gte=gte_days_ago, | ||
first_sent_at__lt=lt_days_ago, | ||
siae_detail_contact_click_count_annotated=0, | ||
) | ||
if options["tender_id"]: | ||
tender_list = tender_list.filter(id=options["tender_id"]) | ||
self.stdout_info( | ||
f"Found {tender_list.count()} Tender without siaes interested between {gte_days_ago} and {lt_days_ago}" | ||
) | ||
|
||
self.stdout_messages_info(f"Step 2: {'display top siaes' if dry_run else 'send emails'} for each tender") | ||
for tender in tender_list: | ||
top_siaes = tender.siaes.all().order_by_super_siaes()[:5] | ||
self.stdout_info(f"{top_siaes.count()} top siaes finded for #{tender.id} {tender}") | ||
if len(top_siaes) > 1: | ||
if not dry_run: | ||
send_super_siaes_email_to_author(tender, top_siaes) | ||
self.stdout_success(f"Email sent to tender author {tender.author}") | ||
else: | ||
for siae in top_siaes: | ||
self.stdout_messages_info( | ||
[ | ||
siae.name_display, | ||
siae.get_kind_display(), | ||
siae.contact_full_name, | ||
siae.contact_phone, | ||
siae.contact_email, | ||
] | ||
) | ||
else: | ||
self.stdout_error(f"Not enough siaes to send an email for #{tender.id}") | ||
|
||
self.stdout_messages_success("Done!") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
from io import StringIO | ||
from unittest.mock import patch | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from lemarche.sectors.factories import SectorFactory | ||
from lemarche.siaes import constants as siae_constants | ||
from lemarche.siaes.factories import SiaeFactory | ||
from lemarche.tenders.factories import TenderFactory | ||
from lemarche.tenders.models import TenderSiae | ||
from lemarche.users.factories import UserFactory | ||
from lemarche.users.models import User | ||
|
||
|
||
class TestSendAuthorListOfSuperSiaesEmails(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.sector = SectorFactory() | ||
|
||
cls.siae1 = SiaeFactory( | ||
is_active=True, | ||
kind=siae_constants.KIND_AI, | ||
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD], | ||
geo_range=siae_constants.GEO_RANGE_COUNTRY, | ||
) | ||
cls.siae1.sectors.add(cls.sector) | ||
|
||
cls.siae2 = SiaeFactory( | ||
is_active=True, | ||
kind=siae_constants.KIND_AI, | ||
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD], | ||
geo_range=siae_constants.GEO_RANGE_COUNTRY, | ||
) | ||
cls.siae2.sectors.add(cls.sector) | ||
|
||
cls.siae3 = SiaeFactory( | ||
is_active=True, | ||
kind=siae_constants.KIND_AI, | ||
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD], | ||
geo_range=siae_constants.GEO_RANGE_COUNTRY, | ||
) | ||
cls.siae3.sectors.add(cls.sector) | ||
|
||
cls.author = UserFactory(kind=User.KIND_BUYER) | ||
cls.tender_before = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_BUILD], | ||
sectors=[cls.sector], | ||
is_country_area=True, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 7, 15)), | ||
author=cls.author, | ||
) | ||
cls.tender_before.set_siae_found_list() | ||
cls.tender_before.refresh_from_db() | ||
|
||
cls.tender_during1 = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_BUILD], | ||
sectors=[cls.sector], | ||
is_country_area=True, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 8, 9)), | ||
author=cls.author, | ||
) | ||
cls.tender_during1.set_siae_found_list() | ||
cls.tender_during1.refresh_from_db() | ||
|
||
cls.tender_during2 = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_BUILD], | ||
sectors=[cls.sector], | ||
is_country_area=True, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 8, 15)), | ||
author=cls.author, | ||
) | ||
cls.tender_during2.set_siae_found_list() | ||
cls.tender_during2.refresh_from_db() | ||
|
||
# Tender with siaes interested | ||
cls.tender_during3 = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_BUILD], | ||
sectors=[cls.sector], | ||
is_country_area=True, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 8, 16)), | ||
author=cls.author, | ||
) | ||
cls.tender_during3.set_siae_found_list() | ||
cls.tender_during3.refresh_from_db() | ||
# add a siae interested | ||
TenderSiae.objects.create( | ||
tender=cls.tender_during3, | ||
siae=cls.siae1, | ||
detail_display_date=timezone.make_aware(timezone.datetime(2024, 4, 8, 17)), | ||
detail_contact_click_date=timezone.make_aware(timezone.datetime(2024, 4, 8, 18)), | ||
) | ||
|
||
# Tender no matching any siaes | ||
cls.tender_during4 = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_DISP], | ||
sectors=[], | ||
is_country_area=False, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 9, 6)), | ||
author=cls.author, | ||
) | ||
cls.tender_during4.set_siae_found_list() | ||
cls.tender_during4.refresh_from_db() | ||
|
||
cls.tender_after = TenderFactory( | ||
presta_type=[siae_constants.PRESTA_BUILD], | ||
sectors=[cls.sector], | ||
is_country_area=True, | ||
first_sent_at=timezone.make_aware(timezone.datetime(2024, 4, 10, 10)), | ||
author=cls.author, | ||
) | ||
cls.tender_after.set_siae_found_list() | ||
cls.tender_after.refresh_from_db() | ||
|
||
@patch("lemarche.www.tenders.tasks.send_super_siaes_email_to_author") | ||
@patch("django.utils.timezone.now") | ||
def test_command_on_weekend(self, mock_now, mock_send_email): | ||
# Assume today is Sunday | ||
mock_now.return_value = timezone.make_aware(timezone.datetime(2024, 4, 7)) | ||
|
||
out = StringIO() | ||
call_command("send_author_list_of_super_siaes_emails", stdout=out) | ||
|
||
self.assertIn("Weekend... Stopping. Come back on Monday :)", out.getvalue()) | ||
self.assertNotIn("Step 1: Find Tender", out.getvalue()) | ||
self.assertFalse(mock_send_email.called) | ||
|
||
@patch("lemarche.www.tenders.tasks.send_super_siaes_email_to_author") | ||
@patch("django.utils.timezone.now") | ||
def test_command_on_weekday(self, mock_now, mock_send_email): | ||
# Assume today is a weekday (e.g., Wednesday) | ||
mock_now.return_value = timezone.make_aware(timezone.datetime(2024, 4, 10, 7, 30)) | ||
|
||
out = StringIO() | ||
call_command("send_author_list_of_super_siaes_emails", stdout=out) | ||
|
||
self.assertEqual(self.tender_before.siaes.count(), 3) | ||
|
||
output = out.getvalue() | ||
|
||
self.assertNotIn("Weekend... Stopping. Come back on Monday :)", output) | ||
self.assertIn("Step 1: Find Tender", output) | ||
self.assertIn("Step 2: send emails for each tender", output) | ||
self.assertIn("Found 3 Tender without siaes interested", output) | ||
|
||
self.assertIn(f"3 top siaes finded for #{self.tender_during1.id}", output) | ||
self.assertIn(f"3 top siaes finded for #{self.tender_during2.id}", output) | ||
self.assertIn(f"0 top siaes finded for #{self.tender_during4.id}", output) | ||
self.assertIn(f"Not enough siaes to send an email for #{self.tender_during4.id}", output) | ||
|
||
self.assertNotIn(f"top siaes finded for #{self.tender_before.id}", output) | ||
self.assertNotIn(f"top siaes finded for #{self.tender_during3.id}", output) # with interested siae | ||
self.assertNotIn(f"top siaes finded for #{self.tender_after.id}", output) | ||
|
||
self.assertEqual(mock_send_email.call_count, 2) | ||
|
||
@patch("lemarche.www.tenders.tasks.send_super_siaes_email_to_author") | ||
@patch("django.utils.timezone.now") | ||
def test_command_on_weekday_dry_run(self, mock_now, mock_send_email): | ||
# Assume today is a weekday (e.g., Wednesday) | ||
mock_now.return_value = timezone.make_aware(timezone.datetime(2024, 4, 10, 7, 30)) | ||
|
||
out = StringIO() | ||
call_command("send_author_list_of_super_siaes_emails", stdout=out, dry_run=True) | ||
|
||
output = out.getvalue() | ||
|
||
self.assertNotIn("Weekend... Stopping. Come back on Monday :)", output) | ||
self.assertIn("Step 1: Find Tender", output) | ||
self.assertIn("Step 2: display top siaes for each tender", output) | ||
self.assertIn("Found 3 Tender without siaes interested", output) | ||
self.assertNotIn("Email sent to tender author", output) | ||
|
||
mock_send_email.assert_not_called() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters