From 9dde6f019a71a93721cf29515fce4f0e71f48600 Mon Sep 17 00:00:00 2001 From: lucasmndc Date: Wed, 7 Aug 2024 11:55:41 -0300 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20de=20hor=C3=A1rio=20est?= =?UTF-8?q?=C3=A1tico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/utils/models.py | 56 ++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/sigi/apps/utils/models.py b/sigi/apps/utils/models.py index c7f6a4c..9fe630a 100644 --- a/sigi/apps/utils/models.py +++ b/sigi/apps/utils/models.py @@ -38,6 +38,17 @@ class SigiAlert(models.Model): class Cronjob(models.Model): + DIGEST_CHOICES = [ + ("N", _("Enviar sem digest")), + ("D", _("Enviar com digest diário")), + ("S", _("Enviar com digest semanal")), + ] + digest = models.CharField( + _("digest"), + max_length=1, + choices=DIGEST_CHOICES, + default="N", + ) app_name = models.CharField(_("app"), max_length=100, editable=False) job_name = models.CharField(_("job"), max_length=100, editable=False) expressao_cron = models.CharField( @@ -78,18 +89,6 @@ class Cronjob(models.Model): def __str__(self): return f"Destinatários: {', '.join(self.get_emails_list())}" - DIGEST_CHOICES = [ - ("N", _("Enviar sem digest")), - ("D", _("Enviar com digest diário")), - ("S", _("Enviar com digest semanal")), - ] - digest = models.CharField( - _("digest"), - max_length=1, - choices=DIGEST_CHOICES, - default="N", - ) - class Meta: ordering = ("app_name", "job_name") verbose_name = _("Cron job") @@ -246,24 +245,45 @@ class JobSchedule(models.Model): def send_digest_email(self, frequency): """Envia email de digest diário ou semanal.""" now = timezone.localtime() + + # Definir horário estático + send_time = timezone.datetime.min.time() # Meia-noite + today = now.date() + if frequency == "daily": - start_time = now - timedelta(days=1) + # Verifica se é meia-noite + if now.time() != send_time: + return + + # Definir início do período como o dia anterior + period_start = today - timedelta(days=1) + elif frequency == "weekly": - start_time = now - timedelta(weeks=1) + # Verifica se é segunda-feira e se é meia-noite + if today.weekday() != 0 or now.time() != send_time: + return + + # Define o início do período como segunda-feira da semana passada + period_start = today - timedelta(days=7) + else: raise ValueError("Invalid frequency for digest email.") job_schedules = JobSchedule.objects.filter( job=self.job, status=JobSchedule.STATUS_CONCLUIDO, - iniciado__gte=start_time, + iniciado__gte=period_start, enviado=False, ) if job_schedules.exists(): - message = "\n\n".join( - [f"{js.iniciado}: {js.resultado}" for js in job_schedules] - ) + message_lines = [] + for js in job_schedules: + message_lines.append( + f"{localize(js.iniciado)}: {js.resultado}" + ) + message = "\n\n".join(message_lines) + send_mail( subject=f"Digest JOB: {self.job.job_name} ({frequency})", message=message,