Skip to content

Commit

Permalink
Merge pull request #261 from praekeltfoundation/fix-import
Browse files Browse the repository at this point in the history
added non default locale validation
  • Loading branch information
Mudiwa66 authored Feb 26, 2024
2 parents 8b3b4a3 + 3d5b5ec commit c5c2e28
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ Authentication is required to access the API. Session, basic, and token authenti

To create an authentication token, you can do so via the Django Admin (available at the `/django-admin` endpoint), or by `POST`ing the username and password of the user you want to generate a token for to the `/api/v2/token/` endpoint.

### Internationalisation
To create or import pages in other languages, the user must first create the locale and HomePage in the specified language. To create a new locale, go to "Settings" => "Locales" in the admin interface, and click "Add a new locale". Then go to the default(most likely English) homepage, click the kebab menu and select translate. This will copy the whole default tree into the new locale, creating the new homepage with all the required pages. After you press "Save", there should be two "Home" pages in the page explorer.

## Running in Production
There is a [docker image](https://github.com/praekeltfoundation/contentrepo/pkgs/container/contentrepo) that can be used to easily run this service. It uses the following environment variables for configuration:

Expand Down
9 changes: 7 additions & 2 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Any
from uuid import uuid4

from django.core.exceptions import ValidationError # type: ignore
from django.core.exceptions import ObjectDoesNotExist, ValidationError # type: ignore
from openpyxl import load_workbook
from taggit.models import Tag # type: ignore
from treebeard.exceptions import NodeAlreadySaved # type: ignore
Expand Down Expand Up @@ -214,7 +214,12 @@ def delete_existing_content(self) -> None:
ContentPageIndex.objects.all().delete()

def home_page(self, locale: Locale) -> HomePage:
return HomePage.objects.get(locale=locale)
try:
return HomePage.objects.get(locale=locale)
except ObjectDoesNotExist:
raise ImportException(
f"You are trying to add a child page to a '{locale}' HomePage that does not exist. Please create the '{locale}' HomePage first"
)

def default_locale(self) -> Locale:
site = Site.objects.get(is_default_site=True)
Expand Down
15 changes: 15 additions & 0 deletions home/tests/content_without_locale_homepage.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,example_values,variation_title,variation_body,list_items,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages,footer
Menu 1,0,1914,mnch-content-please-dont-include-tests-here,,MNCH CONTENT - please don't include tests here,,,,,,,,,,,,,,,,,,,a4192a58-04fe-4766-9d00-9ec4769ce3d8,,,,English,,,,,,,
Sub 1.1.1,0,1306,stage-based-messages,MNCH CONTENT - please don't include tests here,Push messages,,,,,,,,,,,,,,,,,,,35628191-624e-4a13-93bf-499f4e7de017,,,,English,,,,,,,
Sub 1.1.2,0,1303,appointment-reminders,Push messages,Appointment reminders,,,,,,,,,,,,,,,,,,,8cff04aa-cf08-4120-88b4-e2269b7d5d80,,,,English,,,,,,,
Sub 1.3.1,0,1305,onboarding,Appointment reminders,Onboarding,,,,,,,,,,,,,,,,,,,33cbdea8-4f8e-4461-a8c5-e81c218eb2cc,,,,English,,,,,,,
Menu 2,0,1918,qa-test-content,Onboarding,QA TEST CONTENT,,,,,,,,,,,,,,,,,,,3c34e866-bf12-417b-ba2c-2ff691981339,,,,English,,,,,,,
Sub 2.1.1,0,1787,ma_test,QA TEST CONTENT,MA_TEST,,,,,,,,,,,,,,,,,,,e08351cd-7495-40aa-9c99-b9b731e3ef14,,,,English,,,,,,,
Sub 2.1.1.1,1,1788,ma_export,MA_TEST,MA_EXPORT,,,related pages,this is my content page with related pages. Import,,UTILITY,,,,,,,,,,,,,3e641799-df48-42d5-bdf0-33e332eb3993,,,,English,,[],,,,,
Sub 2.1.2,0,1898,simone_test,QA TEST CONTENT,Simone_Test,,,,,,,,,,,,,,,,,,,abeec5a4-eb18-4551-9291-239d537ab448,,,,English,,,,,,,
Sub 2.1.3,0,1307,functional-copy,QA TEST CONTENT,Multi-media test content,,,,,,,,,,,,,,,,,,,219c1d0c-9b83-4a06-b59f-aa7897a969c9,,,,English,,,,,,,
Menu 3,0,1922,faqs,,FAQs,,,,,,,,,,,,,,,,,,,34f2ee6d-0e17-4b59-899d-e184af5e4863,,,,English,,,,,,,
Menu 4,0,1923,dev-test,,Dev Test,,,,,,,,,,,,,,,,,,,058b1a58-34f7-4589-bc79-a0372860ee17,,,,English,,,,,,,
Menu 1,0,1611,stage-based-messages-por,,Stage-based messages Por,,,,,,,,,,,,,,,,,,,35628191-624e-4a13-93bf-499f4e7de017,,,,Portuguese,,,,,,,
Menu 2,0,1704,helen_test,,Helen test por,,,,,,,,,,,,,,,,,,,51cdf6dc-fb64-4197-bda0-f913b6a72811,,,,Portuguese,,,,,,,
Menu 3,0,1768,hlumi-por-test,,Hlumi por test,,,,,,,,,,,,,,,,,,,743fef92-7384-493e-bcb2-3a7f47ac644f,,,,Portuguese,,,,,,,
14 changes: 14 additions & 0 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,20 @@ def test_multiple_locales_for_name(
== "Multiple codes for language: NotEnglish -> ['en1', 'en2']"
)

def test_locale_HomePage_DNE(self, csv_impexp: ImportExport) -> None:
"""
Importing files with non default locale HomePages that do not exist in the db should raise
an error that results in an error message that gets sent back to the user
"""
pt, _created = Locale.objects.get_or_create(language_code="pt")
with pytest.raises(ImportException) as e:
csv_impexp.import_file("content_without_locale_homepage.csv")
assert e.value.row_num == 13
assert (
e.value.message
== "You are trying to add a child page to a 'Portuguese' HomePage that does not exist. Please create the 'Portuguese' HomePage first"
)

def test_missing_parent(self, csv_impexp: ImportExport) -> None:
"""
If the import file specifies a parent title, but there are no pages with that
Expand Down

0 comments on commit c5c2e28

Please sign in to comment.