Skip to content

Commit

Permalink
Dépôt de besoin : envoi sondage transaction : mise en place de relanc…
Browse files Browse the repository at this point in the history
…es (#1057)

* Tender transaction survey: add reminder option

* Update cron

* Improve queryset
  • Loading branch information
raphodn authored Jan 31, 2024
1 parent 72d3b3e commit e24c64e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ fi
cd $APP_HOME

django-admin send_author_transactioned_question_emails
django-admin send_author_transactioned_question_emails --reminder
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
from datetime import datetime
from datetime import datetime, timedelta

from django.core.management.base import BaseCommand

from lemarche.tenders.models import Tender
from lemarche.www.tenders.tasks import send_tenders_author_feedback_or_survey


seven_days_ago = datetime.today().date() - timedelta(days=7)
one_day_ago = datetime.today().date() - timedelta(days=1)


class Command(BaseCommand):
"""
Daily script to send an email to tender authors
When? J+7 after tender deadline_date
When?
- J+7 after tender deadline_date
- Reminder: J+1 after tender start_working_date
Usage:
python manage.py send_author_transactioned_question_emails --dry-run
python manage.py send_author_transactioned_question_emails --all
python manage.py send_author_transactioned_question_emails --kind QUOTE
python manage.py send_author_transactioned_question_emails --reminder
python manage.py send_author_transactioned_question_emails
"""

def add_arguments(self, parser):
parser.add_argument("--kind", type=str, dest="kind")
parser.add_argument("--dry-run", dest="dry_run", action="store_true", help="Dry run, no sends")
parser.add_argument(
"--all",
dest="is_all_tenders",
"--reminder",
dest="reminder",
action="store_true",
help="Send to all tenders with deadline_date 7 days ago",
help="Send a second e-mail to authors who haven't responded to the first survey",
)

def handle(self, kind=None, dry_run=False, is_all_tenders=False, **options):
self.stdout.write("-" * 80)
def handle(self, kind=None, reminder=False, dry_run=False, **options):
self.stdout.write("Script to send email transactioned_question for tenders...")

self.stdout.write("-" * 80)
start_date_feature = datetime(2022, 6, 23).date()
# we first filter on validated tenders
tender_qs = (
Tender.objects.sent()
.transaction_survey_email(kind=kind, all=is_all_tenders)
.filter(deadline_date__gte=start_date_feature)
)
tender_qs = Tender.objects.sent().filter(survey_transactioned_answer=None)
if kind:
tender_qs = tender_qs.filter(kind=kind)
if reminder:
tender_qs = tender_qs.exclude(survey_transactioned_send_date=None).filter(start_working_date=one_day_ago)
else:
tender_qs = tender_qs.filter(survey_transactioned_send_date=None).filter(deadline_date=seven_days_ago)

self.stdout.write(f"Found {tender_qs.count()} tenders")

if not dry_run:
email_kind = f"transactioned_question_7d{'_reminder' if reminder else ''}"
for tender in tender_qs:
send_tenders_author_feedback_or_survey(tender, kind="transactioned_question_7d")
self.stdout.write(f"Sent {tender_qs.count()} J+7 transactioned_question")

self.stdout.write("-" * 80)
self.stdout.write("Done!")
send_tenders_author_feedback_or_survey(tender, kind=email_kind)
self.stdout.write(f"Sent {tender_qs.count()} {email_kind}")
11 changes: 0 additions & 11 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ def is_live(self):
def has_amount(self):
return self.filter(Q(amount__isnull=False) | Q(amount_exact__isnull=False))

def transaction_survey_email(self, kind=None, all=False):
seven_days_ago = datetime.today().date() - timedelta(days=7)
qs = self.sent().filter(survey_transactioned_answer=None)
if kind:
qs = qs.filter(kind=kind)
if all:
qs = qs.filter(deadline_date__lte=seven_days_ago)
else:
qs = qs.filter(deadline_date=seven_days_ago)
return qs

def in_perimeters(self, post_code, department, region):
filters = (
Q(perimeters__post_codes__contains=[post_code])
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"):
"TENDER_KIND": tender.get_kind_display(),
}

if kind == "transactioned_question_7d":
if kind in ["transactioned_question_7d", "transactioned_question_7d_reminder"]:
template_id = settings.MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID
user_sesame_query_string = sesame_get_query_string(tender.author) # TODO: sesame scope parameter
answer_url_with_sesame_token = (
Expand Down

0 comments on commit e24c64e

Please sign in to comment.