-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Sort apps by name * Add missing migration after help text change * Add new app to handle custom email dispatching * Add new model to configure sponsor notifications * Minimal admin * Update admin form to validate content as django template * Add button to preview how template will render * Add new benefit configuration to flag email targeatable * Add method to filter sponsorships by included features * Enable user to select which notification template to use * Rename variable * Display warning message if selected sponsorships aren't targetable * Introduce indirection with use case to send the emails * Implement method to create a EmailMessage from a notification template * Display non targetable sponsorship as checkbox instead of text * Add select all/delete all links * Filter emails by benefits, not feature configuration * Better display for notification objects * Add checkbox to select contact type * Update get_message method to accept boolean flags to control recipients * Rename form field name * Send notification to sponsors * Register email dispatch with admin log entry activity * Add input for custom email content * Display input for custom email content * UC expects sponsorship object, not PK * Consider email subject as a template as well * Refactor to move specific email building part to mailing app * Remove warning message * Optimizes sponsorship admin query * Add option to preview notification * Fix parameters names
- Loading branch information
Showing
27 changed files
with
934 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.contrib import admin | ||
from django.forms.models import modelform_factory | ||
from django.http import HttpResponse | ||
from django.urls import path | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from mailing.forms import BaseEmailTemplateForm | ||
|
||
|
||
class BaseEmailTemplateAdmin(admin.ModelAdmin): | ||
change_form_template = "mailing/admin/base_email_template_form.html" | ||
list_display = ["internal_name", "subject"] | ||
readonly_fields = ["created_at", "updated_at"] | ||
search_fields = ["internal_name"] | ||
fieldsets = ( | ||
(None, { | ||
'fields': ('internal_name',) | ||
}), | ||
('Email template', { | ||
'fields': ('subject', 'content') | ||
}), | ||
('Timestamps', { | ||
'classes': ('collapse',), | ||
'fields': ('created_at', 'updated_at'), | ||
}), | ||
) | ||
|
||
def get_form(self, *args, **kwargs): | ||
kwargs["form"] = modelform_factory(self.model, form=BaseEmailTemplateForm) | ||
return super().get_form(*args, **kwargs) | ||
|
||
def get_urls(self): | ||
urls = super().get_urls() | ||
prefix = self.model._meta.db_table | ||
my_urls = [ | ||
path( | ||
"<int:pk>/preview-content/$", | ||
self.admin_site.admin_view(self.preview_email_template), | ||
name=f"{prefix}_preview", | ||
), | ||
] | ||
return my_urls + urls | ||
|
||
def preview_email_template(self, request, pk, *args, **kwargs): | ||
qs = self.get_queryset(request) | ||
template = get_object_or_404(qs, pk=pk) | ||
return HttpResponse(template.render_content({})) |
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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class MailingConfig(AppConfig): | ||
name = 'mailing' |
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,20 @@ | ||
from django import forms | ||
from django.template import Template, Context, TemplateSyntaxError | ||
|
||
from mailing.models import BaseEmailTemplate | ||
|
||
|
||
class BaseEmailTemplateForm(forms.ModelForm): | ||
|
||
def clean_content(self): | ||
content = self.cleaned_data["content"] | ||
try: | ||
template = Template(content) | ||
template.render(Context({})) | ||
return content | ||
except TemplateSyntaxError as e: | ||
raise forms.ValidationError(e) | ||
|
||
class Meta: | ||
model = BaseEmailTemplate | ||
fields = "__all__" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.core.mail import EmailMessage | ||
from django.db import models | ||
from django.template import Template, Context | ||
from django.urls import reverse | ||
|
||
|
||
class BaseEmailTemplate(models.Model): | ||
internal_name = models.CharField(max_length=128) | ||
|
||
subject = models.CharField(max_length=128) | ||
content = models.TextField() | ||
|
||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
@property | ||
def preview_content_url(self): | ||
prefix = self._meta.db_table | ||
url_name = f"admin:{prefix}_preview" | ||
return reverse(url_name, args=[self.pk]) | ||
|
||
def render_content(self, context): | ||
template = Template(self.content) | ||
ctx = Context(context) | ||
return template.render(ctx) | ||
|
||
def render_subject(self, context): | ||
template = Template(self.subject) | ||
ctx = Context(context) | ||
return template.render(ctx) | ||
|
||
def get_email(self, from_email, to, context=None, **kwargs): | ||
context = context or {} | ||
context = self.get_email_context_data(**context) | ||
subject = self.render_subject(context) | ||
content = self.render_content(context) | ||
return EmailMessage(subject, content, from_email, to, **kwargs) | ||
|
||
def get_email_context_data(self, **kwargs): | ||
return kwargs | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
def __str__(self): | ||
return f"Email template: {self.internal_name}" |
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 @@ | ||
# Create your tests here |
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,29 @@ | ||
from django.test import TestCase | ||
|
||
from mailing.forms import BaseEmailTemplateForm | ||
|
||
|
||
class BaseEmailTemplateFormTests(TestCase): | ||
|
||
def setUp(self): | ||
self.data = { | ||
"content": "Hi, {{ name }}\n\nThis is a message to you.", | ||
"subject": "Hello", | ||
"internal_name": "notification 01", | ||
} | ||
|
||
def test_validate_required_fields(self): | ||
required = set(self.data) | ||
form = BaseEmailTemplateForm(data={}) | ||
self.assertFalse(form.is_valid()) | ||
self.assertEqual(required, set(form.errors)) | ||
|
||
def test_validate_with_correct_data(self): | ||
form = BaseEmailTemplateForm(data=self.data) | ||
self.assertTrue(form.is_valid()) | ||
|
||
def test_invalid_form_if_broken_template_syntax(self): | ||
self.data["content"] = "Invalid syntax {% invalid %}" | ||
form = BaseEmailTemplateForm(data=self.data) | ||
self.assertFalse(form.is_valid()) | ||
self.assertIn("content", form.errors, form.errors) |
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
Oops, something went wrong.