SendGrid: merge 'filters' in esp_extra

Previously, setting esp_extra['x-smtpapi']['filters']
would override the entire filters setting, potentially
undoing other Anymail options that use SendGrid
filters (like track_opens).

Now, 'filters' is special-cased, and merged with
any other Anymail filter options.

(We don't do a fully deep merge, because otherwise
there would be no way to use esp_extra to *clear*
Anymail settings.)
This commit is contained in:
medmunds
2016-04-30 10:21:11 -07:00
parent a26d284772
commit 1372ef21eb
4 changed files with 52 additions and 18 deletions

View File

@@ -91,7 +91,13 @@ class SendGridPayload(RequestsPayload):
# If esp_extra was also used to set x-smtpapi, need to merge it
if "x-smtpapi" in self.data:
esp_extra_smtpapi = self.data["x-smtpapi"]
self.smtpapi.update(esp_extra_smtpapi) # need to make this deep merge (for filters)!
for key, value in esp_extra_smtpapi.items():
if key == "filters":
# merge filters (else it's difficult to mix esp_extra with other features)
self.smtpapi.setdefault(key, {}).update(value)
else:
# all other keys replace any current value
self.smtpapi[key] = value
self.data["x-smtpapi"] = self.serialize_json(self.smtpapi)
elif "x-smtpapi" in self.data:
self.data["x-smtpapi"] = self.serialize_json(self.data["x-smtpapi"])