mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
Mailgun: fix event/metadata param extraction in tracking webhook
Mailgun merges user-variables (metadata) into the webhook post data interspersed with the actual event params. This can lead to ambiguity interpreting post data. To extract metadata from an event, Anymail had been attempting to avoid that ambiguity by instead using X-Mailgun-Variables fields found in the event's message-headers param. But message-headers isn't included in some tracking events (opened, clicked, unsubscribed), resulting in empty metadata for those events. (#76) Also, conflicting metadata keys could confuse Anymail's Mailgun event parsing, leading to unexpected values in the normalized event. (#77) This commit: * Cleans up Anymail's tracking webhook to be explicit about which multi-value params it uses, avoiding conflicts with metadata keys. Fixes #77. * Extracts metadata from post params for opened, clicked and unsubscribed events. All unknown event params are assumed to be metadata. Fixes #76. * Documents a few metadata key names where it's impossible (or likely to be unreliable) for Anymail to extract metadata from the post data. For reference, the order of params in the Mailgun's post data *appears* to be (from live testing): * For the timestamp, token and signature params, any user-variable with the same name appears *before* the corresponding event data. * For all other params, any user-variable with the same name as a Mailgun event param appears *after* the Mailgun data.
This commit is contained in:
@@ -398,6 +398,34 @@ def collect_all_methods(cls, method_name):
|
||||
return methods
|
||||
|
||||
|
||||
def querydict_getfirst(qdict, field, default=UNSET):
|
||||
"""Like :func:`django.http.QueryDict.get`, but returns *first* value of multi-valued field.
|
||||
|
||||
>>> from django.http import QueryDict
|
||||
>>> q = QueryDict('a=1&a=2&a=3')
|
||||
>>> querydict_getfirst(q, 'a')
|
||||
'1'
|
||||
>>> q.get('a')
|
||||
'3'
|
||||
>>> q['a']
|
||||
'3'
|
||||
|
||||
You can bind this to a QueryDict instance using the "descriptor protocol":
|
||||
>>> q.getfirst = querydict_getfirst.__get__(q)
|
||||
>>> q.getfirst('a')
|
||||
'1'
|
||||
"""
|
||||
# (Why not instead define a QueryDict subclass with this method? Because there's no simple way
|
||||
# to efficiently initialize a QueryDict subclass with the contents of an existing instance.)
|
||||
values = qdict.getlist(field)
|
||||
if len(values) > 0:
|
||||
return values[0]
|
||||
elif default is not UNSET:
|
||||
return default
|
||||
else:
|
||||
return qdict[field] # raise appropriate KeyError
|
||||
|
||||
|
||||
EPOCH = datetime(1970, 1, 1, tzinfo=utc)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user