Skip to content
Snippets Groups Projects
Commit d75bb1c3 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

mails can be sent multiple time per day

parent 28f1b764
No related branches found
No related tags found
1 merge request!240Redcap sync
Pipeline #25330 failed
......@@ -2,11 +2,10 @@
import datetime
import locale
import logging
import traceback
import platform
import time
import traceback
import pytz
import timeout_decorator
from django.contrib import messages
from django.utils.dateparse import parse_datetime
......@@ -221,36 +220,30 @@ def kit_requests_send_mail(request, start_date, end_date=None):
class KitRequestEmailSendJob(CronJobBase):
RUN_EVERY_MINUTES = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINUTES)
RUN_AT = []
times = ConfigurationItem.objects.get(
type=KIT_EMAIL_HOUR_CONFIGURATION_TYPE).value.split(";")
for entry in times:
# TODO it's a hack assuming that we are in CEST
text = str((int(entry.split(":")[0]) + 22) % 24) + ":" + entry.split(":")[1]
RUN_AT.append(text)
schedule = Schedule(run_at_times=RUN_AT)
code = 'web.kit_request_weekly_email' # a unique code
@timeout_decorator.timeout(CRON_JOB_TIMEOUT)
def do(self):
now = datetime.datetime.utcnow()
hour = int(ConfigurationItem.objects.get(
type=KIT_EMAIL_HOUR_CONFIGURATION_TYPE).value.split(":")[0])
minute = int(ConfigurationItem.objects.get(
type=KIT_EMAIL_HOUR_CONFIGURATION_TYPE).value.split(":")[1])
# check if we sent email this day already
date = now.replace(hour=hour, minute=minute)
# TODO it's a hack assuming that we are in CEST
date = pytz.utc.localize(date - datetime.timedelta(minutes=122))
jobs = CronJobLog.objects.filter(
code=KitRequestEmailSendJob.code, message="mail sent", start_time__gte=date)
code=KitRequestEmailSendJob.code, message="mail sent", start_time__gte=datetime.datetime.utcnow())
if jobs.count() == 0:
if pytz.utc.localize(datetime.datetime.utcnow()) > date:
if self.match_day_of_week():
worker = Worker.objects.create()
data = get_kit_requests(worker)
send_mail(data)
worker.delete()
return "mail sent"
else:
return "day of week doesn't match"
if self.match_day_of_week():
worker = Worker.objects.create()
data = get_kit_requests(worker)
send_mail(data)
worker.delete()
return "mail sent"
else:
return "too early"
return "day of week doesn't match"
else:
return "mail already sent"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment