Fix:SES-Added params check in template (#3753)
* Fix:SES-Added params check in template * Added more tests and handled message * linting * fixed tests * fix attribute name in message * fix logic for exception
This commit is contained in:
parent
abc62b360d
commit
31cf3c4252
3 changed files with 62 additions and 1 deletions
|
|
@ -87,3 +87,13 @@ class RuleSetDoesNotExist(RESTError):
|
|||
|
||||
def __init__(self, message):
|
||||
super(RuleSetDoesNotExist, self).__init__("RuleSetDoesNotExist", message)
|
||||
|
||||
|
||||
class MissingRenderingAttributeException(RESTError):
|
||||
code = 400
|
||||
|
||||
def __init__(self, var):
|
||||
super(MissingRenderingAttributeException, self).__init__(
|
||||
"MissingRenderingAttributeException",
|
||||
"Attribute '{0}' is not present in the rendering data.".format(var),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
import json
|
||||
import email
|
||||
import datetime
|
||||
|
|
@ -22,6 +23,7 @@ from .exceptions import (
|
|||
RuleSetNameAlreadyExists,
|
||||
RuleSetDoesNotExist,
|
||||
RuleAlreadyExists,
|
||||
MissingRenderingAttributeException,
|
||||
)
|
||||
from .utils import get_random_message_id
|
||||
from .feedback import COMMON_MAIL, BOUNCE, COMPLAINT, DELIVERY
|
||||
|
|
@ -91,6 +93,17 @@ class SESQuota(BaseModel):
|
|||
return self.sent
|
||||
|
||||
|
||||
def are_all_variables_present(template, template_data):
|
||||
subject_part = template["subject_part"]
|
||||
text_part = template["text_part"]
|
||||
html_part = template["html_part"]
|
||||
|
||||
for var in re.findall("{{(.+?)}}", subject_part + text_part + html_part):
|
||||
if not template_data.get(var):
|
||||
return var, False
|
||||
return None, True
|
||||
|
||||
|
||||
class SESBackend(BaseBackend):
|
||||
def __init__(self):
|
||||
self.addresses = []
|
||||
|
|
@ -352,6 +365,10 @@ class SESBackend(BaseBackend):
|
|||
"Template rendering data is invalid"
|
||||
)
|
||||
|
||||
var, are_variables_present = are_all_variables_present(template, template_data)
|
||||
if not are_variables_present:
|
||||
raise MissingRenderingAttributeException(var)
|
||||
|
||||
subject_part = template["subject_part"]
|
||||
text_part = template["text_part"]
|
||||
html_part = template["html_part"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue