|
7 | 7 | from django.contrib.auth.models import Permission |
8 | 8 | from django.contrib.contenttypes.models import ContentType |
9 | 9 | from django.core import management |
| 10 | +from django.test import TestCase |
10 | 11 | from rest_framework import status |
11 | 12 |
|
12 | 13 | from api.models import Country, DisasterType, District, Region, RegionName |
|
27 | 28 | DrefOperationalUpdate, |
28 | 29 | ProposedAction, |
29 | 30 | ) |
30 | | -from dref.tasks import send_dref_email |
| 31 | +from dref.tasks import send_dref_email, translate_fields_to_english |
31 | 32 | from main.test_case import APITestCase |
32 | 33 |
|
33 | 34 |
|
@@ -883,6 +884,158 @@ def test_dref_latest_update(self): |
883 | 884 | # Title should be latest since modified_at is greater than modified_at in database |
884 | 885 | self.assertEqual(response.data["title"], "New title") |
885 | 886 |
|
| 887 | + def test_dref_create_and_update_in_local_language( |
| 888 | + self, |
| 889 | + ): |
| 890 | + national_society = Country.objects.create(name="Test country xzz") |
| 891 | + disaster_type = DisasterType.objects.create(name="Test country abc") |
| 892 | + |
| 893 | + data = { |
| 894 | + "title": "Prueba de título Dref", |
| 895 | + "type_of_onset": Dref.OnsetType.SLOW.value, |
| 896 | + "disaster_category": Dref.DisasterCategory.YELLOW.value, |
| 897 | + "status": Dref.Status.DRAFT.value, |
| 898 | + "num_assisted": 5666, |
| 899 | + "num_affected": 23, |
| 900 | + "amount_requested": 127771111, |
| 901 | + "emergency_appeal_planned": False, |
| 902 | + "event_date": "2021-08-01", |
| 903 | + "ns_respond_date": "2021-08-01", |
| 904 | + "event_text": "Texto de prueba para la respuesta", |
| 905 | + "did_ns_request_fund": False, |
| 906 | + "lessons_learned": "Texto de prueba para lecciones aprendidas", |
| 907 | + "complete_child_safeguarding_risk": True, |
| 908 | + "child_safeguarding_risk_level": "Muy alto", |
| 909 | + "event_description": "Texto de prueba para descripción del evento", |
| 910 | + "anticipatory_actions": "Texto de prueba para acciones anticipatorias", |
| 911 | + "event_scope": "Texto de prueba para alcance del evento", |
| 912 | + "government_requested_assistance": False, |
| 913 | + "government_requested_assistance_date": "2021-08-01", |
| 914 | + "national_authorities": "Texto de prueba para autoridades nacionales", |
| 915 | + "icrc": "Texto de prueba para lecciones aprendidas", |
| 916 | + "un_or_other_actor": "Texto de prueba para lecciones aprendidas", |
| 917 | + "major_coordination_mechanism": "Texto de prueba para lecciones aprendidas", |
| 918 | + "identified_gaps": "Texto de prueba para lecciones aprendidas", |
| 919 | + "people_assisted": "Texto de prueba para lecciones aprendidas", |
| 920 | + "selection_criteria": "Texto de prueba para lecciones aprendidas", |
| 921 | + "entity_affected": "Texto de prueba para lecciones aprendidas", |
| 922 | + "community_involved": "Texto de prueba para lecciones aprendidas", |
| 923 | + "women": 344444, |
| 924 | + "men": 5666, |
| 925 | + "girls": 22, |
| 926 | + "boys": 344, |
| 927 | + "disability_people_per": "12.45", |
| 928 | + "people_per": "10.35", |
| 929 | + "displaced_people": 234243, |
| 930 | + "operation_objective": "Texto de prueba para objetivo de operación", |
| 931 | + "response_strategy": "Texto de prueba para estrategia de respuesta", |
| 932 | + "secretariat_service": "Texto de prueba para servicio de secretaría", |
| 933 | + "national_society_strengthening": "", |
| 934 | + "ns_request_date": "2021-07-01", |
| 935 | + "submission_to_geneva": "2021-07-01", |
| 936 | + "date_of_approval": "2021-07-01", |
| 937 | + "end_date": "2021-07-05", |
| 938 | + "publishing_date": "2021-08-01", |
| 939 | + "operation_timeframe": 4, |
| 940 | + "appeal_code": "J7876", |
| 941 | + "glide_code": "ER878", |
| 942 | + "appeal_manager_name": "Nombre de prueba", |
| 943 | + "appeal_manager_email": "[email protected]", |
| 944 | + "project_manager_name": "Nombre de prueba", |
| 945 | + "project_manager_email": "[email protected]", |
| 946 | + "national_society_contact_name": "Nombre de prueba", |
| 947 | + "national_society_contact_email": "[email protected]", |
| 948 | + "media_contact_name": "Nombre de prueba", |
| 949 | + "media_contact_email": "[email protected]", |
| 950 | + "ifrc_emergency_name": "Nombre de prueba", |
| 951 | + "ifrc_emergency_email": "[email protected]", |
| 952 | + "originator_name": "Nombre de prueba", |
| 953 | + "originator_email": "[email protected]", |
| 954 | + "national_society": national_society.id, |
| 955 | + "disaster_type": disaster_type.id, |
| 956 | + "needs_identified": [{"title": "shelter_housing_and_settlements", "description": "hola"}], |
| 957 | + "planned_interventions": [ |
| 958 | + { |
| 959 | + "title": "shelter_housing_and_settlements", |
| 960 | + "description": "matriz", |
| 961 | + "budget": 23444, |
| 962 | + "male": 12222, |
| 963 | + "female": 2255, |
| 964 | + "indicators": [ |
| 965 | + { |
| 966 | + "title": "título de prueba", |
| 967 | + "actual": 21232, |
| 968 | + "target": 44444, |
| 969 | + } |
| 970 | + ], |
| 971 | + } |
| 972 | + ], |
| 973 | + } |
| 974 | + |
| 975 | + url = "/api/v2/dref/" |
| 976 | + |
| 977 | + self.client.force_authenticate(self.user) |
| 978 | + response = self.client.post(url, data, format="json", HTTP_ACCEPT_LANGUAGE="es") |
| 979 | + self.assertEqual(response.status_code, 201) |
| 980 | + self.assertEqual(response.data["original_language"], "es") |
| 981 | + self.assertEqual(response.data["translation_module_original_language"], "es") |
| 982 | + self.assertEqual(response.data["title"], "Prueba de título Dref") |
| 983 | + # Test update |
| 984 | + dref_id = response.data["id"] |
| 985 | + url = f"/api/v2/dref/{dref_id}/" |
| 986 | + # update in French |
| 987 | + data_fr = {"title": "Titre en français", "modified_at": datetime.now()} |
| 988 | + response = self.client.patch(url, data=data_fr, format="json", HTTP_ACCEPT_LANGUAGE="fr") |
| 989 | + self.assert_400(response) |
| 990 | + |
| 991 | + # update in Arabic |
| 992 | + data_ar = {"title": "العنوان بالعربية", "modified_at": datetime.now()} |
| 993 | + response = self.client.patch(url, data=data_ar, format="json", HTTP_ACCEPT_LANGUAGE="ar") |
| 994 | + self.assert_400(response) |
| 995 | + |
| 996 | + # update in English |
| 997 | + data_en = {"title": "Updated title in English", "modified_at": datetime.now()} |
| 998 | + response = self.client.patch(url, data=data_en, format="json", HTTP_ACCEPT_LANGUAGE="en") |
| 999 | + self.assert_400(response) |
| 1000 | + |
| 1001 | + @mock.patch("dref.tasks.translate_fields_to_english.delay") |
| 1002 | + def test_update_and_finalize_dref(self, mock_translate): |
| 1003 | + dref = DrefFactory.create( |
| 1004 | + title="Título original en español", |
| 1005 | + type_of_dref=Dref.DrefType.IMMINENT, |
| 1006 | + created_by=self.user, |
| 1007 | + status=Dref.Status.DRAFT, |
| 1008 | + translation_module_original_language="es", |
| 1009 | + ) |
| 1010 | + |
| 1011 | + url = f"/api/v2/dref/{dref.id}/" |
| 1012 | + self.client.force_authenticate(self.user) |
| 1013 | + # update in Spanish |
| 1014 | + data_es = {"title": "en español", "modified_at": datetime.now()} |
| 1015 | + response = self.client.patch(url, data=data_es, HTTP_ACCEPT_LANGUAGE="es") |
| 1016 | + self.assert_200(response) |
| 1017 | + self.assertEqual(response.data["title"], "en español") |
| 1018 | + # update in French |
| 1019 | + data_fr = {"title": "Titre en français", "modified_at": datetime.now()} |
| 1020 | + response = self.client.patch(url, data=data_fr, format="json", HTTP_ACCEPT_LANGUAGE="fr") |
| 1021 | + self.assert_400(response) |
| 1022 | + # update in Arabic |
| 1023 | + data_ar = {"title": "العنوان بالعربية", "modified_at": datetime.now()} |
| 1024 | + response = self.client.patch(url, data=data_ar, format="json", HTTP_ACCEPT_LANGUAGE="ar") |
| 1025 | + self.assert_400(response) |
| 1026 | + # update in English |
| 1027 | + data_en = {"title": "Updated title in English", "modified_at": datetime.now()} |
| 1028 | + response = self.client.patch(url, data=data_en, HTTP_ACCEPT_LANGUAGE="en") |
| 1029 | + self.assert_400(response) |
| 1030 | + |
| 1031 | + # Finalize DREF |
| 1032 | + with self.capture_on_commit_callbacks(execute=True): |
| 1033 | + finalize_url = f"/api/v2/dref/{dref.id}/finalize/" |
| 1034 | + response = self.client.post(finalize_url) |
| 1035 | + self.assert_200(response) |
| 1036 | + self.assertEqual(response.data["status"], Dref.Status.FINALIZING) |
| 1037 | + mock_translate.assert_called_once_with("dref.Dref", dref.id) |
| 1038 | + |
886 | 1039 | def test_dref_op_update_locking(self): |
887 | 1040 | user1, _ = UserFactory.create_batch(2) |
888 | 1041 | dref = DrefFactory.create( |
@@ -1852,6 +2005,28 @@ def test_dref_imminent_v2_final_report(self): |
1852 | 2005 | ) |
1853 | 2006 |
|
1854 | 2007 |
|
| 2008 | +class TranslateFieldsToEnglishTaskTest(TestCase): |
| 2009 | + |
| 2010 | + def test_translate_fields_to_english_task(self): |
| 2011 | + dref = DrefFactory.create( |
| 2012 | + title="Titre en français", |
| 2013 | + type_of_dref=Dref.DrefType.IMMINENT, |
| 2014 | + status=Dref.Status.DRAFT, |
| 2015 | + translation_module_original_language="fr", |
| 2016 | + ) |
| 2017 | + |
| 2018 | + with mock.patch("dref.tasks.ModelTranslator.translate_model_fields_to_english") as mock_translate: |
| 2019 | + mock_translate.return_value = None |
| 2020 | + |
| 2021 | + # Call the task |
| 2022 | + translate_fields_to_english("dref.Dref", dref.pk) |
| 2023 | + # Reload object from DB |
| 2024 | + dref.refresh_from_db() |
| 2025 | + mock_translate.assert_called_once() |
| 2026 | + self.assertEqual(dref.status, Dref.Status.FINALIZED) |
| 2027 | + self.assertEqual(dref.translation_module_original_language, "en") |
| 2028 | + |
| 2029 | + |
1855 | 2030 | User = get_user_model() |
1856 | 2031 |
|
1857 | 2032 |
|
|
0 commit comments