Skip to content

Commit

Permalink
web preview field fix + variable mismatch update
Browse files Browse the repository at this point in the history
  • Loading branch information
Mudiwa Matanda authored and Mudiwa Matanda committed Jan 30, 2024
1 parent 1134257 commit 8a5b0b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 20 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,26 @@ def clean(self):
# The variable check is only for templates
if self.is_whatsapp_template and len(self.whatsapp_body.raw_data) > 0:
whatsapp_message = self.whatsapp_body.raw_data[0]["value"]["message"]

right_mismatch = re.findall(r"(?<!\{){[^{}]*}\}", whatsapp_message)
left_mismatch = re.findall(r"\{{[^{}]*}(?!\})", whatsapp_message)
mismatches = right_mismatch + left_mismatch

if mismatches:
errors.setdefault("whatsapp_body", []).append(
StreamBlockValidationError(
{
0: StreamBlockValidationError(
{
"message": ValidationError(
f"Please provide variables with matching braces. You provided {mismatches}."
)
}
)
}
)
)

vars_in_msg = re.findall(r"{{(.*?)}}", whatsapp_message)
non_digit_variables = [var for var in vars_in_msg if not var.isdecimal()]

Expand Down
6 changes: 1 addition & 5 deletions home/templates/home/content_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@

{% block content %}
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>

<div class="intro">{{ page.intro }}</div>


<h2>{{ page.subtitle }}</h2>
{% include_block page.body %}

{% endblock %}
2 changes: 1 addition & 1 deletion home/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def test_clean_text_valid_variables(self):
"WA Title",
[
WABlk(
"{{2}}{{1}} {{foo}}",
"{{2}}{{1}} {{foo}} {{mismatch1} {mismatch2}}",
)
],
)
Expand Down

0 comments on commit 8a5b0b7

Please sign in to comment.