Skip to content

Commit

Permalink
Fixed error palcement
Browse files Browse the repository at this point in the history
  • Loading branch information
Mudiwa Matanda authored and Mudiwa Matanda committed Jan 22, 2024
1 parent f805089 commit 435b92f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 239 deletions.
190 changes: 0 additions & 190 deletions home/migrations/0042_variable_validation.py

This file was deleted.

62 changes: 40 additions & 22 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from taggit.models import ItemBase, TagBase, TaggedItemBase
from wagtail import blocks
from wagtail.api import APIField
from wagtail.blocks import StructBlockValidationError
from wagtail.blocks import StreamBlockValidationError, StructBlockValidationError
from wagtail.contrib.settings.models import BaseSiteSetting, register_setting
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.fields import StreamField
Expand Down Expand Up @@ -296,8 +296,6 @@ def clean(self, value):
f"{len(result['message'])} characters long"
)



if errors:
raise StructBlockValidationError(errors)
return result
Expand Down Expand Up @@ -751,40 +749,60 @@ def save_revision(
revision.save(update_fields=["content"])
return revision



def clean(self):

result = super().clean()
errors = {}

#the WA title is needed for all templates to generate a name for the template
# The WA title is needed for all templates to generate a name for the template
if self.is_whatsapp_template and not self.whatsapp_title:
errors["whatsapp_title"] = ValidationError('All WhatsApp templates need a title.')


if self.is_whatsapp_template:
#find variables
WA_message = self.whatsapp_body.raw_data[0]["value"]["message"]
vars_in_msg = re.findall(r"{{(.*?)}}", WA_message)
errors.setdefault("whatsapp_title", []).append(
ValidationError("All WhatsApp templates need a title.")
)
# 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"]
vars_in_msg = re.findall(r"{{(.*?)}}", whatsapp_message)
non_digit_variables = [var for var in vars_in_msg if not var.isdecimal()]

if non_digit_variables:
errors["whatsapp_body"] = ValidationError(f"Please provide numeric variables only. You provided {non_digit_variables}.")
errors.setdefault("whatsapp_body", []).append(
StreamBlockValidationError(
{
0: StreamBlockValidationError(
{
"message": ValidationError(
f"Please provide numeric variables only. You provided {non_digit_variables}."
)
}
)
}
)
)

# check variable order
# Check variable order
actual_digit_variables = [var for var in vars_in_msg if var.isdecimal()]
expected_variables = [str(i + 1) for i in range(len(actual_digit_variables))]
expected_variables = [
str(j + 1) for j in range(len(actual_digit_variables))
]
if actual_digit_variables != expected_variables:
errors["whatsapp_body"] = ValidationError(
f"Variables must be sequential, starting with \"{{1}}\". Your first variable was \"{actual_digit_variables[0]}\"")


errors.setdefault("whatsapp_body", []).append(
StreamBlockValidationError(
{
0: StreamBlockValidationError(
{
"message": ValidationError(
f'Variables must be sequential, starting with "{{1}}". Your first variable was "{actual_digit_variables[0]}"'
)
}
)
}
)
)

if errors:
raise ValidationError(errors)
return result

return result


# Allow slug to be blank in forms, we fill it in in full_clean
Expand Down
Loading

0 comments on commit 435b92f

Please sign in to comment.