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 submission status #354

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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,
),
),
]
34 changes: 23 additions & 11 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be overwriting the whole history of a template whenever we create a submission.

If I'm understanding correctly, the reason the updated status doesn't appear in the list, is that the list only shows the latest published value, not the latest revision. In that case, simply adding the publishing should've fixed this (although publishing on save also doesn't seem correct to me)

return revision

def clean(self):
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