From bd36e70bc3306a2abfff0d876379319fe7e19bef Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Mon, 22 Jun 2020 15:45:02 +0200 Subject: [PATCH] add hold function to OutgoingEmail, set hold=True to delay sending emails --- src/utils/email.py | 2 + .../migrations/0005_auto_20200622_1544.py | 109 ++++++++++++++++++ src/utils/models.py | 46 ++++++-- src/utils/outgoingemailworker.py | 6 +- 4 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 src/utils/migrations/0005_auto_20200622_1544.py diff --git a/src/utils/email.py b/src/utils/email.py index 53eef7fa..15a1e1f8 100644 --- a/src/utils/email.py +++ b/src/utils/email.py @@ -74,6 +74,7 @@ def add_outgoing_email( sender="BornHack ", attachment=None, attachment_filename="", + hold=False, ): """ adds an email to the outgoing queue recipients is a list of to recipients @@ -104,6 +105,7 @@ def add_outgoing_email( to_recipients=to_recipients, cc_recipients=cc_recipients, bcc_recipients=bcc_recipients, + hold=hold, ) if attachment: diff --git a/src/utils/migrations/0005_auto_20200622_1544.py b/src/utils/migrations/0005_auto_20200622_1544.py new file mode 100644 index 00000000..aacc1cdb --- /dev/null +++ b/src/utils/migrations/0005_auto_20200622_1544.py @@ -0,0 +1,109 @@ +# Generated by Django 3.0.3 on 2020-06-22 13:44 + +import django.contrib.postgres.fields +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("teams", "0052_team_permission_set"), + ("utils", "0004_uuidtaggeditem"), + ] + + operations = [ + migrations.AddField( + model_name="outgoingemail", + name="hold", + field=models.BooleanField( + default=False, + help_text="Hold (do not send) this email. Uncheck to send.", + ), + ), + migrations.AddField( + model_name="outgoingemail", + name="responsible_team", + field=models.ForeignKey( + blank=True, + help_text="The Team responsible for this email.", + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="teams.Team", + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="attachment", + field=models.FileField( + blank=True, + help_text="The attachment for this email. Optional.", + upload_to="", + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="bcc_recipients", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(blank=True, max_length=500), + blank=True, + help_text="The Bcc: recipients", + null=True, + size=None, + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="cc_recipients", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(blank=True, max_length=500), + blank=True, + help_text="The Cc: recipients", + null=True, + size=None, + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="html_template", + field=models.TextField( + blank=True, help_text="The HTML body of the email (optional)." + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="processed", + field=models.BooleanField( + default=False, + help_text="Unchecked before the email is sent, checked after the email has been sent.", + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="sender", + field=models.CharField(help_text="The email sender.", max_length=500), + ), + migrations.AlterField( + model_name="outgoingemail", + name="subject", + field=models.CharField( + help_text="The subject of the e-mail", max_length=500 + ), + ), + migrations.AlterField( + model_name="outgoingemail", + name="text_template", + field=models.TextField(help_text="The plaintext body of the email."), + ), + migrations.AlterField( + model_name="outgoingemail", + name="to_recipients", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(blank=True, max_length=500), + blank=True, + help_text="The To: recipients", + null=True, + size=None, + ), + ), + ] diff --git a/src/utils/models.py b/src/utils/models.py index 763d24ad..1499e89c 100644 --- a/src/utils/models.py +++ b/src/utils/models.py @@ -82,21 +82,49 @@ class CampRelatedModel(CreatedUpdatedModel): class OutgoingEmail(CreatedUpdatedModel): - subject = models.CharField(max_length=500) - text_template = models.TextField() - html_template = models.TextField(blank=True) - sender = models.CharField(max_length=500) + """The OutgoingEmail model contains all system emails, both unsent and sent.""" + + subject = models.CharField(max_length=500, help_text="The subject of the e-mail") + text_template = models.TextField(help_text="The plaintext body of the email.") + html_template = models.TextField( + blank=True, help_text="The HTML body of the email (optional)." + ) + sender = models.CharField(max_length=500, help_text="The email sender.") to_recipients = ArrayField( - models.CharField(max_length=500, blank=True), null=True, blank=True + models.CharField(max_length=500, blank=True), + null=True, + blank=True, + help_text="The To: recipients", ) cc_recipients = ArrayField( - models.CharField(max_length=500, blank=True), null=True, blank=True + models.CharField(max_length=500, blank=True), + null=True, + blank=True, + help_text="The Cc: recipients", ) bcc_recipients = ArrayField( - models.CharField(max_length=500, blank=True), null=True, blank=True + models.CharField(max_length=500, blank=True), + null=True, + blank=True, + help_text="The Bcc: recipients", + ) + attachment = models.FileField( + blank=True, help_text="The attachment for this email. Optional." + ) + processed = models.BooleanField( + default=False, + help_text="Unchecked before the email is sent, checked after the email has been sent.", + ) + hold = models.BooleanField( + default=False, help_text="Hold (do not send) this email. Uncheck to send." + ) + responsible_team = models.ForeignKey( + "teams.Team", + null=True, + blank=True, + on_delete=models.PROTECT, + help_text="The Team responsible for this email.", ) - attachment = models.FileField(blank=True) - processed = models.BooleanField(default=False) def __str__(self): return "OutgoingEmail Object id: {} ".format(self.id) diff --git a/src/utils/outgoingemailworker.py b/src/utils/outgoingemailworker.py index d5623b11..c265b1fe 100644 --- a/src/utils/outgoingemailworker.py +++ b/src/utils/outgoingemailworker.py @@ -12,7 +12,7 @@ def do_work(): The outgoing email worker sends emails added to the OutgoingEmail queue. """ - not_processed_email = OutgoingEmail.objects.filter(processed=False) + not_processed_email = OutgoingEmail.objects.filter(processed=False, hold=False) if len(not_processed_email) > 0: logger.debug("about to process {} emails".format(len(not_processed_email))) @@ -38,6 +38,6 @@ def do_work(): if mail_send_success: email.processed = True email.save() - logger.debug("successfully sent {}".format(email)) + logger.debug("Successfully sent {}".format(email)) else: - logger.error("unable to sent {}".format(email)) + logger.error("Unable to send {}".format(email))