-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[POC] Implement sending report via email feature
- Loading branch information
Showing
13 changed files
with
200 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.core.management.base import BaseCommand | ||
from akvo.rsr.views.py_reports.email_report import run_job | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
run_job() |
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,26 @@ | ||
# Generated by Django 3.2.10 on 2022-08-02 22:45 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('rsr', '0218_project_thumbnail_cache'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='EmailReportJob', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True, null=True)), | ||
('started_at', models.DateTimeField(null=True)), | ||
('finished_at', models.DateTimeField(null=True)), | ||
('attempts', models.PositiveSmallIntegerField(default=0)), | ||
('report', models.CharField(max_length=100)), | ||
('payload', models.JSONField(default=dict)), | ||
('recipient', models.EmailField(max_length=254)), | ||
], | ||
), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.db import models | ||
from django.utils.timezone import now | ||
|
||
|
||
class EmailReportJob(models.Model): | ||
created_at = models.DateTimeField(null=True, auto_now_add=True, db_index=True, editable=False) | ||
started_at = models.DateTimeField(null=True) | ||
finished_at = models.DateTimeField(null=True) | ||
attempts = models.PositiveSmallIntegerField(default=0) | ||
report = models.CharField(max_length=100) | ||
payload = models.JSONField(default=dict) | ||
recipient = models.EmailField() | ||
|
||
def mark_started(self): | ||
self.started_at = now() | ||
self.attempts = self.attempts + 1 | ||
self.save(update_fields=['started_at', 'attempts']) | ||
|
||
def mark_finished(self): | ||
self.finished_at = now() | ||
self.save(update_fields=['finished_at']) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
|
||
from datetime import timedelta | ||
from django.db.models import Q | ||
from django.utils.timezone import now | ||
from akvo.rsr.models import EmailReportJob | ||
|
||
from .program_overview_pdf_report import email_report as handle_program_overview_pdf_report | ||
|
||
TIMEOUT = timedelta(minutes=30) | ||
MAX_ATTEMPTS = 3 | ||
HANDLER = { | ||
'program_overview_pdf_report': handle_program_overview_pdf_report | ||
} | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def run_job(): | ||
pending_jobs = _get_pending_jobs() | ||
if not pending_jobs.exists(): | ||
return | ||
job = pending_jobs.first() | ||
job.mark_started() | ||
try: | ||
handler = _get_report_handler(job.report) | ||
if handler: | ||
handler(job.payload, job.recipient) | ||
job.mark_finished() | ||
except Exception: | ||
logger.exception(f'Failed to genereate report {job.report} for {job.recipient}') | ||
|
||
|
||
def _get_pending_jobs(): | ||
started_timeout = now() - TIMEOUT | ||
return EmailReportJob.objects\ | ||
.order_by('created_at')\ | ||
.filter(finished_at__isnull=True)\ | ||
.exclude(Q(attempts__gte=MAX_ATTEMPTS) | Q(started_at__gte=started_timeout)) | ||
|
||
|
||
def _get_report_handler(report): | ||
return HANDLER[report] if report in HANDLER else None |
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
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
Empty file.
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