Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix max chars #357

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions home/migrations/0081_alter_whatsapptemplate_submission_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.11 on 2024-08-06 03:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("home", "0080_assessment_skip_high_result_page_and_more"),
]

operations = [
migrations.AlterField(
model_name="whatsapptemplate",
name="submission_status",
field=models.CharField(
blank=True,
choices=[
("NOT_SUBMITTED_YET", "Not Submitted Yet"),
("SUBMITTED", "Submitted"),
("FAILED", "Failed"),
],
default="",
max_length=30,
),
),
]
37 changes: 25 additions & 12 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ class SubmissionStatus(models.TextChoices):
max_length=30,
choices=SubmissionStatus.choices,
blank=True,
default=SubmissionStatus.NOT_SUBMITTED_YET,
default="",
)

submission_result = models.TextField(
Expand Down Expand Up @@ -1629,6 +1629,8 @@ def save_revision(
return revision

self.template_name = self.create_whatsapp_template_name()
self.submission_status = ""
self.submission_result = ""
try:
response_json = create_standalone_whatsapp_template(
name=self.template_name,
Expand All @@ -1639,22 +1641,32 @@ def save_revision(
image_obj=self.image,
example_values=[v["value"] for v in self.example_values.raw_data],
)
revision.content["name"] = self.name
revision.content["submission_name"] = self.template_name
revision.content["submission_status"] = self.SubmissionStatus.SUBMITTED
revision.content["submission_result"] = (
f"Success! Template ID = {response_json['id']}"
)
self.submission_status = _(self.SubmissionStatus.SUBMITTED)
self.submission_result = f"Success! Template ID = {response_json['id']}"

except TemplateSubmissionException as e:
# The submission failed
revision.content["name"] = self.name
revision.content["submission_name"] = self.template_name
revision.content["submission_status"] = self.SubmissionStatus.FAILED
revision.content["submission_result"] = (
self.submission_status = _(self.SubmissionStatus.FAILED)
self.submission_result = (
f"Error! {e.response_json['error']['error_user_msg']} "
)

revision.content["name"] = self.name
revision.content["submission_name"] = self.template_name
revision.content["submission_status"] = self.submission_status
revision.content["submission_result"] = self.submission_result

revision.save(update_fields=["content"])
# TODO this publish is a workaround for now, as described in ticket Delta-877
revision.publish()

# Update the submissions status fields for all revisions, regardless of Draft vs Live status
revisions = self.revisions
for r in revisions:
r.content["submission_name"] = self.template_name
r.content["submission_status"] = self.submission_status
r.content["submission_result"] = self.submission_result
r.save()
return revision

def clean(self):
Expand All @@ -1669,7 +1681,8 @@ def clean(self):

message = self.message

# TODO: Explain what this does, or find a cleaner implementation
if len(message) > 1024:
errors["message"] = ValidationError("Message cannot exceed 1024 characters")

# Checks for mismatches in the number of opening and closing brackets. First from right to left, then from left to right
# TODO: Currently "{1}" is allowed to pass and throws an error. Add a check for this, or redo this section in a cleaner way
Expand Down
18 changes: 18 additions & 0 deletions home/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,24 @@ def test_variables_are_ordered(self) -> None:
],
}

def test_message_max_char_count(self) -> None:
"""
Template message field cannot exceed 1024.
"""

with pytest.raises(ValidationError) as err_info:
wat = WhatsAppTemplate(
name="wa_title",
message="a" * 1025,
category="UTILITY",
locale=Locale.objects.get(language_code="en"),
)
wat.full_clean()

assert err_info.value.message_dict == {
"message": ["Message cannot exceed 1024 characters"],
}

@override_settings(WHATSAPP_CREATE_TEMPLATES=False)
@responses.activate
def test_template_is_not_submitted_if_template_creation_is_disabled(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions home/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class WhatsAppTemplateAdmin(SnippetViewSet):
"locale",
"status",
"submission_status",
"submission_result",
)
list_filter = ("locale",)

Expand Down
Loading