AnymailMessage: initialize anymail_status to empty AnymailStatus

Allows clients that use AnymailMessage or AnymailMessageMixin
to access (e.g.) message.anymail_status.message_id without
an AttributeError, even when using a non-Anymail backend
(e.g., during testing).

Also clarify docs of anymail_status attribute to note it only gets
attached to a normal Django EmailMessage when sent through
an Anymail backend.

(May help with situation described in #36)
This commit is contained in:
medmunds
2016-10-20 11:50:31 -07:00
parent a1380b82f3
commit 60dc11559d
2 changed files with 18 additions and 1 deletions

View File

@@ -28,7 +28,7 @@ class AnymailMessageMixin(object):
self.template_id = kwargs.pop('template_id', UNSET) self.template_id = kwargs.pop('template_id', UNSET)
self.merge_data = kwargs.pop('merge_data', UNSET) self.merge_data = kwargs.pop('merge_data', UNSET)
self.merge_global_data = kwargs.pop('merge_global_data', UNSET) self.merge_global_data = kwargs.pop('merge_global_data', UNSET)
self.anymail_status = None self.anymail_status = AnymailStatus()
# noinspection PyArgumentList # noinspection PyArgumentList
super(AnymailMessageMixin, self).__init__(*args, **kwargs) super(AnymailMessageMixin, self).__init__(*args, **kwargs)

View File

@@ -212,6 +212,19 @@ ESP send status
:class:`~django.core.mail.EmailMessage`, with a normalized version :class:`~django.core.mail.EmailMessage`, with a normalized version
of the ESP's response. of the ESP's response.
Anymail backends create this attribute *as they process each message.*
Before that, anymail_status won't be present on an ordinary Django
EmailMessage or EmailMultiAlternatives---you'll get an :exc:`AttributeError`
if you try to access it.
This might cause problems in your test cases, because Django
`substitutes its own locmem email backend`_ during testing (so anymail_status
won't be set even after sending). Your code should either guard against
a missing anymail_status attribute, or use :class:`AnymailMessage`
(or the :class:`AnymailMessageMixin`) which initializes its anymail_status
attribute to a default AnymailStatus object.
After sending through an Anymail backend,
:attr:`~AnymailMessage.anymail_status` will be an object with these attributes: :attr:`~AnymailMessage.anymail_status` will be an object with these attributes:
.. attribute:: message_id .. attribute:: message_id
@@ -308,6 +321,10 @@ ESP send status
message.anymail_status.esp_response.json() message.anymail_status.esp_response.json()
.. _substitutes its own locmem email backend:
https://docs.djangoproject.com/en/stable/topics/testing/tools/#email-services
.. _inline-images: .. _inline-images:
Inline images Inline images