Customization¶
Recipients model¶
The default recipients model in wagtail-newsletter is intentionally simple: it has a name and a reference to an audience (and, optionally, a segment) in the campaign provider’s system. If you want to associate more information with a recipients record, like a custom greeting or footer text, you can define a custom model:
from wagtail.fields import RichTextField
from wagtail_newsletter.models import NewsletterRecipientsBase
class CustomRecipients(NewsletterRecipientsBase):
greeting = RichTextField(blank=True)
class Meta:
verbose_name_plural = "Custom recipients"
Configure wagtail-newsletter to use the custom model by adding a setting to Django settings:
WAGTAIL_NEWSLETTER_RECIPIENTS_MODEL = "myapp.CustomRecipients"
Register a viewset, and permissions, for the custom recipients model:
from django.contrib.auth.models import Permission
from wagtail import hooks
from wagtail.admin.panels import FieldPanel
from wagtail_newsletter.viewsets import NewsletterRecipientsViewSet
from .models import CustomRecipients
class CustomRecipientsViewSet(NewsletterRecipientsViewSet):
model = CustomRecipients
panels = NewsletterRecipientsViewSet.panels + [
FieldPanel("greeting"),
]
@hooks.register("register_admin_viewset")
def register_admin_viewset():
return CustomRecipientsViewSet("custom_recipients")
@hooks.register("register_permissions")
def register_permissions():
return Permission.objects.filter(
content_type__app_label="myapp",
codename__in=[
"add_customrecipients",
"change_customrecipients",
"delete_customrecipients",
],
)
Finally, use content from the custom recipients model in the newsletter template:
{% load wagtail_newsletter %}
{% mrml %}
<mjml>
<mj-body>
<h1>{{ page.title }}</h1>
{% if page.newsletter_recipients.greeting %}
{{ page.newsletter_recipients.greeting|newsletter_richtext }}
{% endif %}
</mj-body>
</mjml>
{% endmrml %}
Campaign backends¶
Wagtail-newsletter is designed to work with multiple email campaign providers, though currently it only supports Mailchimp out of the box. Should you want to target another provider, or change the behaviour of an existing backend (e.g. to tweak the configuration of a campaign before it’s sent to the API), you can define your own backend class.
Backends should subclass the
wagtail_newsletter.campaign_backends.CampaignBackend abstract class and
implement its methods.
For example, here is how you would implement a custom backend that disables tracking for a Mailchimp campaign:
# myapp/campaign_backend.py
from wagtail_newsletter.campaign_backends.mailchimp import MailchimpCampaignBackend
class CustomBackend(MailchimpCampaignBackend):
def get_campaign_request_body(self, **kwargs):
body = super().get_campaign_request_body(**kwargs)
# Add tracking settings to disable all tracking
body["tracking"] = {
"opens": False,
"html_clicks": False,
}
return body
To enable the backend, configure the WAGTAIL_NEWSLETTER_CAMPAIGN_BACKEND Django setting:
WAGTAIL_NEWSLETTER_CAMPAIGN_BACKEND = "myapp.campaign_backend.CustomBackend"
Permissions¶
Out of the box, wagtail-newsletter checks the Publish permission to see whether a user is allowed to perform newsletter actions. If the user doesn’t have the permission, they will not see the corresponding panels in the editor.
Permissions can be customized by implementing the
has_newsletter_permission(user, action) method on the page model. It’s
possible to selectively grant permissions to certain actions (a user might be
able to send themselves a test email but not send the campaign). Have a look at
demo/models.py for an example.