Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configurable email on completion for export job #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,29 @@ By default, there is no time limit on celery import/export tasks. This can be cu
IMPORT_EXPORT_CELERY_EXPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
IMPORT_EXPORT_CELERY_EXPORT_HARD_TIME_LIMIT = 360 # 6 minutes

Customizing email template for export job completion email
Customizing email settings for export job completion
----------------------------------------------------------

By default this is the subject and template used to send the email
By default the export job completion email uses the following settings


::

Subject: 'Django: Export job completed'
Email template: 'email/export_job_completion.html'
Email on completion: True


The default email template can be found `here <https://github.com/auto-mat/django-import-export-celery/blob/master/import_export_celery/templates/email/export_job_completion.html>`__

The default email subject and template can be customized by overriding these values from django settings:-
The default email subject, template and sending behavior can be customized by overriding these values from django settings:-


::

EXPORT_JOB_COMPLETION_MAIL_SUBJECT="Your custom subject"
EXPORT_JOB_COMPLETION_MAIL_TEMPLATE="path_to_folder/your_custom_template.html"
EXPORT_JOB_EMAIL_ON_COMPLETION = True # Set to False to disable email


The email template will get some context variables that you can use to customize your template.
Expand Down
20 changes: 19 additions & 1 deletion example/winners/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from django.test import TestCase
from django.test import TestCase, override_settings
from django.core.files.base import ContentFile

from import_export_celery.models.exportjob import ExportJob
from import_export_celery.models.importjob import ImportJob


Expand All @@ -16,3 +17,20 @@ def test_delete_file_on_job_delete(self):
job.delete()
assert not os.path.exists(file_path)
assert not ImportJob.objects.filter(id=job.id).exists()


class ExportJobTestCases(TestCase):
def test_create_export_job_default_email_on_completion(self):
job = ExportJob.objects.create(
app_label="winners", model="Winner"
)
job.refresh_from_db()
self.assertTrue(job.email_on_completion)

@override_settings(EXPORT_JOB_EMAIL_ON_COMPLETION=False)
def test_create_export_job_false_email_on_completion(self):
job = ExportJob.objects.create(
app_label="winners", model="Winner"
)
job.refresh_from_db()
self.assertFalse(job.email_on_completion)
11 changes: 11 additions & 0 deletions example/winners/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
get_export_job_mail_subject,
get_export_job_mail_template,
get_export_job_mail_context,
get_export_job_email_on_completion,
DEFAULT_EXPORT_JOB_COMPLETION_MAIL_SUBJECT,
DEFAULT_EXPORT_JOB_COMPLETION_MAIL_TEMPLATE,
DEFAULT_EXPORT_JOB_EMAIL_ON_COMPLETION,
)
from import_export_celery.models import ExportJob

Expand All @@ -29,6 +31,15 @@ def test_get_export_job_mail_template_default(self):
def test_get_export_job_mail_template_overridden(self):
self.assertEqual("mytemplate.html", get_export_job_mail_template())

def test_get_export_job_email_on_completion_default(self):
self.assertEqual(
DEFAULT_EXPORT_JOB_EMAIL_ON_COMPLETION, get_export_job_email_on_completion()
)

@override_settings(EXPORT_JOB_EMAIL_ON_COMPLETION=False)
def test_get_export_job_email_on_completion_overridden(self):
self.assertEqual(False, get_export_job_email_on_completion())

def test_get_export_job_mail_context(self):
export_job = ExportJob.objects.create(
app_label="winners", model="Winner", site_of_origin="http://127.0.0.1:8000"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-11-22 12:19

from django.db import migrations, models
import import_export_celery.utils


class Migration(migrations.Migration):

dependencies = [
('import_export_celery', '0010_auto_20231013_0904'),
]

operations = [
migrations.AlterField(
model_name='exportjob',
name='email_on_completion',
field=models.BooleanField(default=import_export_celery.utils.get_export_job_email_on_completion, verbose_name='Send me an email when this export job is complete'),
),
]
4 changes: 2 additions & 2 deletions import_export_celery/models/exportjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from ..fields import ImportExportFileField
from ..tasks import run_export_job
from ..utils import get_formats
from ..utils import get_formats, get_export_job_email_on_completion


@with_author
Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(self, *args, **kwargs):

email_on_completion = models.BooleanField(
verbose_name=_("Send me an email when this export job is complete"),
default=True,
default=get_export_job_email_on_completion,
)

site_of_origin = models.TextField(
Expand Down
9 changes: 9 additions & 0 deletions import_export_celery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.urls import reverse
from import_export.formats.base_formats import DEFAULT_FORMATS

DEFAULT_EXPORT_JOB_EMAIL_ON_COMPLETION = True
DEFAULT_EXPORT_JOB_COMPLETION_MAIL_SUBJECT = "Django: Export job completed"
DEFAULT_EXPORT_JOB_COMPLETION_MAIL_TEMPLATE = (
"email/export_job_completion.html"
Expand Down Expand Up @@ -53,6 +54,14 @@ def get_export_job_mail_context(export_job):
return context


def get_export_job_email_on_completion():
return getattr(
settings,
"EXPORT_JOB_EMAIL_ON_COMPLETION",
DEFAULT_EXPORT_JOB_EMAIL_ON_COMPLETION,
)


def get_export_job_mail_subject():
return getattr(
settings,
Expand Down