Skip to content

Commit

Permalink
Merge branch 'main' into add-helper-text
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlamallama committed Feb 17, 2025
2 parents 9e2ba0c + 200d70e commit 8079900
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove whatsapp body hidden characters
- Fix for assessment import for multiple languages
- Add locale filtering to assessments API
- Error message when list_items is broken
- Consistent labelling on import forms
- Fix class_name warning: RemovedInWagtail60Warning
### Removed
- Locale field on exports
-->
Expand Down
2 changes: 1 addition & 1 deletion contentrepo/templates/assessment_upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col header-title">
{% block h1 %}
<h1>
{% icon name="openquote" class_name="header-title-icon" %}
{% icon name="openquote" classname="header-title-icon" %}
{% if loading %}
{% trans 'Uploading Content...' %}
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion contentrepo/templates/whatsapptemplate_upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col header-title">
{% block h1 %}
<h1>
{% icon name="openquote" class_name="header-title-icon" %}
{% icon name="openquote" classname="header-title-icon" %}
{% if loading %}
{% trans 'Uploading Content...' %}
{% else %}
Expand Down
64 changes: 38 additions & 26 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,34 +385,46 @@ def add_message_to_shadow_content_page_from_row(

list_items = []
for index, item in enumerate(row.list_items):
if item["type"] == "next_message":
list_items.append(
{
"id": str(uuid4()),
"type": item["type"],
"value": {"title": item["title"]},
}
)
elif item["type"] == "go_to_page":
item["index"] = index
page_gtpli = self.go_to_page_list_items[(row.slug, locale)]
page_gtpli[len(page.whatsapp_body)].append(item)
elif item["type"] == "go_to_form":
try:
form = Assessment.objects.get(slug=item["slug"], locale=locale)
except Assessment.DoesNotExist:
try:
if item["type"] == "next_message":
list_items.append(
{
"id": str(uuid4()),
"type": item["type"],
"value": {"title": item["title"]},
}
)
elif item["type"] == "go_to_page":
item["index"] = index
page_gtpli = self.go_to_page_list_items[(row.slug, locale)]
page_gtpli[len(page.whatsapp_body)].append(item)
elif item["type"] == "go_to_form":
try:
form = Assessment.objects.get(
slug=item["slug"], locale=locale
)
except Assessment.DoesNotExist:
raise ImportException(
f"No form found with slug '{item['slug']}' and locale "
f"'{locale}' for go_to_form list item '{item['title']}' on "
f"page '{row.slug}'"
)
list_items.append(
{
"id": str(uuid4()),
"type": item["type"],
"value": {"title": item["title"], "form": form.id},
}
)
else:
raise ImportException(
f"No form found with slug '{item['slug']}' and locale "
f"'{locale}' for go_to_form list item '{item['title']}' on "
f"page '{row.slug}'"
f"List item with invalid type '{item['type']}'"
)
list_items.append(
{
"id": str(uuid4()),
"type": item["type"],
"value": {"title": item["title"], "form": form.id},
}
)
except ImportException:
raise
except KeyError as e:
raise ImportException(f"List item is missing key {e}")

page.whatsapp_body.append(
ShadowWhatsappBlock(
message=row.whatsapp_body,
Expand Down
17 changes: 17 additions & 0 deletions home/migrations/0089_alter_assessment_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.17 on 2025-02-13 08:45

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("home", "0088_alter_assessment_questions"),
]

operations = [
migrations.AlterModelOptions(
name="assessment",
options={"verbose_name": "CMS Form", "verbose_name_plural": "CMS Forms"},
),
]
5 changes: 5 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,11 @@ class AssessmentTag(TaggedItemBase):


class Assessment(DraftStateMixin, RevisionMixin, index.Indexed, ClusterableModel):

class Meta:
verbose_name = "CMS Form"
verbose_name_plural = "CMS Forms"

title = models.CharField(max_length=255)
slug = models.SlugField(
max_length=255, help_text="A unique identifier for this assessment"
Expand Down
3 changes: 3 additions & 0 deletions home/tests/import-export-data/list_items_with_errors.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,variation_title,variation_body,list_title,list_items,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,next_prompt,buttons,image_link,doc_link,media_link,related_pages,footer,language_code
Menu 1,0,600,main-menu,,Main Menu,,,,,,,,,,,,,,,,,,,35a7f12c-7373-42a6-9ca6-a1caedea5822,,,,,,,,,,,en
Sub 1.1,1,601,ha-menu,Main Menu,HealthAlert menu,,,HealthAlert menu,*Welcome to HealthAlert* WA,,UTILITY,,,List Title,[{}],,,,,HealthAlert menu,Welcome to HealthAlert M,HealthAlert menu,Welcome to HealthAlert V,5ab08854-228b-4f83-ae54-ec2dc6ebaf69,,,,,[],,,,,,en
3 changes: 3 additions & 0 deletions home/tests/import-export-data/list_items_with_type_error.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,variation_title,variation_body,list_title,list_items,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,next_prompt,buttons,image_link,doc_link,media_link,related_pages,footer,language_code
Menu 1,0,600,main-menu,,Main Menu,,,,,,,,,,,,,,,,,,,35a7f12c-7373-42a6-9ca6-a1caedea5822,,,,,,,,,,,en
Sub 1.1,1,601,ha-menu,Main Menu,HealthAlert menu,,,HealthAlert menu,*Welcome to HealthAlert* WA,,UTILITY,,,List Title,"[{""type"":""new_type"",""title"":""Missing"",""slug"":""missing""}]",,,,,HealthAlert menu,Welcome to HealthAlert M,HealthAlert menu,Welcome to HealthAlert V,5ab08854-228b-4f83-ae54-ec2dc6ebaf69,,,,,[],,,,,,en
20 changes: 20 additions & 0 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,26 @@ def test_hidden_characters(self, csv_impexp: ImportExport) -> None:
assert "\u2028\u2028" in csv_bytes.decode("utf-8")
assert "\u2028" not in ContentPage.objects.all().values()[0]["whatsapp_body"]

def test_list_item_descriptive_error_message(
self, csv_impexp: ImportExport
) -> None:
"""
Import failure for list_items should return a descriptive error message
"""
with pytest.raises(ImportException) as e:
csv_impexp.import_file("list_items_with_errors.csv")
assert e.value.row_num == 3
assert e.value.message == ["List item is missing key 'type'"]

def test_list_item_type_error_message(self, csv_impexp: ImportExport) -> None:
"""
Import invalid list item type should return a invalid type error message
"""
with pytest.raises(ImportException) as e:
csv_impexp.import_file("list_items_with_type_error.csv")
assert e.value.row_num == 3
assert e.value.message == ["List item with invalid type 'new_type'"]


@pytest.mark.django_db
class TestExport:
Expand Down
68 changes: 68 additions & 0 deletions home/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,74 @@ def test_ordered_form_has_all_expected_options(self, admin_client):
assert find_options(soup, "purge") == ["No", "Yes"]


class TestAssessmentImportView:
def test_import_assessment(self, admin_client):
"""
The import page can be accessed at the expected url
"""
response = admin_client.get("/admin/import_assessment/")

assert response.status_code == status.HTTP_200_OK
asserts.assertContains(response, "Upload Content")

def test_assessment_form_has_all_expected_options(self, admin_client):
"""
The upload form has all expected options:
- heading
- file upload field
- csv and Excel file as file type options in a drop down
- Yes and No as purge options in a drop down
- All current locale and each locale as an option for the languages
"""
response = admin_client.get("/admin/import_assessment/")
soup = BeautifulSoup(response.content, "html.parser")

heading = soup.find("h1").text.strip()
assert heading == "Upload Content"

form = soup.find("form")
file_upload = form.find("input", type="file", id="id_file")
assert file_upload

assert find_options(form, "file_type") == ["CSV File", "Excel File"]
assert find_options(soup, "purge") == ["No", "Yes"]
assert find_options(form, "locale") == ["Import all languages", "English"]


class TestWhatsAppTemplateImportView:
def test_import_whatsapp_template(self, admin_client):
"""
The import page can be accessed at the expected url
"""
response = admin_client.get("/admin/import_whatsapptemplate/")

assert response.status_code == status.HTTP_200_OK
asserts.assertContains(response, "Upload Content")

def test_whatsapp_template_form_has_all_expected_options(self, admin_client):
"""
The upload form has all expected options:
- heading
- file upload field
- csv and Excel file as file type options in a drop down
- Yes and No as purge options in a drop down
- All current locale and each locale as an option for the languages
"""
response = admin_client.get("/admin/import_whatsapptemplate/")
soup = BeautifulSoup(response.content, "html.parser")

heading = soup.find("h1").text.strip()
assert heading == "Upload Content"

form = soup.find("form")
file_upload = form.find("input", type="file", id="id_file")
assert file_upload

assert find_options(form, "file_type") == ["CSV File", "Excel File"]
assert find_options(soup, "purge") == ["No", "Yes"]
assert find_options(form, "locale") == ["Import all languages", "English"]


@pytest.mark.django_db
class TestPageViewReportView:
def create_content_page(self, number_of_pages):
Expand Down

0 comments on commit 8079900

Please sign in to comment.