Skip to content

Commit

Permalink
Move queryset to Tender model
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 23, 2023
1 parent f0dd1d3 commit c1866fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timedelta
from datetime import datetime

from django.core.management.base import BaseCommand
from django.db.models import Q

from lemarche.tenders.models import Tender
from lemarche.www.tenders.tasks import send_tenders_author_feedback_or_survey
Expand Down Expand Up @@ -34,29 +33,18 @@ def handle(self, kind=None, dry_run=False, is_all_tenders=False, **options):
self.stdout.write("Script to send email transactioned_question for tenders...")

self.stdout.write("-" * 80)
seven_days_ago = datetime.today().date() - timedelta(days=7)
start_date_feature = datetime(2022, 6, 23).date()
# we first filter on validated tenders
tender_qs = Tender.objects.validated()
# we also filter on tenders who haven't answered to the email yet
tender_qs = tender_qs.filter(survey_transactioned_answer=None)
# optional filter on kind
if kind:
tender_qs = tender_qs.filter(kind=kind)
# date filter
if is_all_tenders:
# all tenders with deadline_date 7 days or more
tender_qs = tender_qs.filter(deadline_date__lte=seven_days_ago)
else:
# only tenders with deadline_date 7 days ago and up to 23/06/2022
start_date_feature = datetime(2022, 6, 23).date()
tender_qs = tender_qs.filter(Q(deadline_date=seven_days_ago) & Q(deadline_date__gte=start_date_feature))
tender_qs = Tender.objects.transaction_survey_email(kind=kind, all=is_all_tenders).filter(
deadline_date__gte=start_date_feature
)

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

if not dry_run:
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+30 feedbacks")
self.stdout.write(f"Sent {tender_qs.count()} J+7 transactioned_question")

self.stdout.write("-" * 80)
self.stdout.write("Done!")
13 changes: 12 additions & 1 deletion lemarche/tenders/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
from uuid import uuid4

from django.conf import settings
Expand Down Expand Up @@ -58,6 +58,17 @@ 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.validated().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

0 comments on commit c1866fe

Please sign in to comment.