From aca0345389101756b9da7c765480db00b5c85aa6 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Fri, 1 Nov 2024 01:29:53 -0700 Subject: [PATCH 01/20] fetch file content for generate_file --- .../services/translation_processor.py | 17 +++++++++++ core/i18nilize/urls.py | 1 + core/i18nilize/views.py | 16 +++++++++++ i18nilize/src/internationalize/helpers.py | 28 ++++++++----------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/core/i18nilize/services/translation_processor.py b/core/i18nilize/services/translation_processor.py index 405440c..dc5daca 100644 --- a/core/i18nilize/services/translation_processor.py +++ b/core/i18nilize/services/translation_processor.py @@ -156,3 +156,20 @@ def bulk_update_translations(token, updated_translations): except Exception as e: print(e) return False, 0 + +def get_translations_by_language(language, token): + """ + Return all translations for the given language as a dictionary. + """ + translations = Translation.objects.filter(language=language, token=token) + if not translations: + translations = create_default_translations_for_language(language, token) + + translations_dict = { + translation.original_word: translation.translated_word + for translation in translations + } + return translations_dict + +def create_default_translations_for_language(language, token): + # to be implemented by Brian & Angela's ticket? \ No newline at end of file diff --git a/core/i18nilize/urls.py b/core/i18nilize/urls.py index 5048bee..0083e4b 100644 --- a/core/i18nilize/urls.py +++ b/core/i18nilize/urls.py @@ -7,4 +7,5 @@ path('token//', TokenView.as_view(), name='read-token'), path('translation', TranslationView.as_view(), name='translation'), path('translations', views.ProcessTranslationsView.as_view(), name='process-translations') + path('translations/ Date: Sat, 2 Nov 2024 00:25:22 -0700 Subject: [PATCH 02/20] If fetch request is correct, this should work --- i18nilize/src/internationalize/helpers.py | 10 ++++++---- .../{jsonFile => languages}/translations.json | 0 i18nilize/src/internationalize/test.py | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) rename i18nilize/src/internationalize/{jsonFile => languages}/translations.json (100%) create mode 100644 i18nilize/src/internationalize/test.py diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 3f76ba7..63a5c1f 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -28,8 +28,9 @@ def get_token(file_path): # Input: a JSON object # Output: None, but creates a local JSON file containing the object -def create_json(json_object): - with open("src/internationalize/jsonFile/translations.json", "w") as outfile: +def create_json(json_object, language): + file_path = "src/internationalize/languages/" + language + ".json" + with open(file_path, "w") as outfile: outfile.write(json_object) # Input: language @@ -38,15 +39,16 @@ def generate_file(language): url = f'http://localhost:8000/api/translations/{language}/' response = requests.get(url) + file_content = None + if response.status_code == 200: file_content = response.json() else: print(f'Error: {response.status_code}, {response.data['error']}') - file_content = None # transforms the dictionary object above into a JSON object json_object = json.dumps(file_content, indent=4) - create_json(json_object) + create_json(json_object, language) # make hashmap from translations def make_translation_map(data): diff --git a/i18nilize/src/internationalize/jsonFile/translations.json b/i18nilize/src/internationalize/languages/translations.json similarity index 100% rename from i18nilize/src/internationalize/jsonFile/translations.json rename to i18nilize/src/internationalize/languages/translations.json diff --git a/i18nilize/src/internationalize/test.py b/i18nilize/src/internationalize/test.py new file mode 100644 index 0000000..068c988 --- /dev/null +++ b/i18nilize/src/internationalize/test.py @@ -0,0 +1,3 @@ +from helpers import generate_file + +generate_file("Spanish") \ No newline at end of file From aecf66d51cd4b9460c4394dca1a7cc5e483a025a Mon Sep 17 00:00:00 2001 From: Andrew Ahn Date: Sat, 2 Nov 2024 10:00:30 -0700 Subject: [PATCH 03/20] Fixed bugs in url, added error-handling to generat --- core/i18nilize/urls.py | 4 ++-- .../__pycache__/helpers.cpython-311.pyc | Bin 1315 -> 3148 bytes i18nilize/src/internationalize/helpers.py | 16 ++++++++-------- .../languages/translations.json | 15 --------------- 4 files changed, 10 insertions(+), 25 deletions(-) delete mode 100644 i18nilize/src/internationalize/languages/translations.json diff --git a/core/i18nilize/urls.py b/core/i18nilize/urls.py index 0083e4b..859a7f4 100644 --- a/core/i18nilize/urls.py +++ b/core/i18nilize/urls.py @@ -6,6 +6,6 @@ path('token/', TokenView.as_view(), name='create-token'), path('token//', TokenView.as_view(), name='read-token'), path('translation', TranslationView.as_view(), name='translation'), - path('translations', views.ProcessTranslationsView.as_view(), name='process-translations') - path('translations/', views.ProcessTranslationsView.as_view(), name='get-translations') ] diff --git a/i18nilize/src/internationalize/__pycache__/helpers.cpython-311.pyc b/i18nilize/src/internationalize/__pycache__/helpers.cpython-311.pyc index 38dda700d7855bfaf3145e0171fd5786d4734211..dab2cb703032073733ed297165d41cf517536b1a 100644 GIT binary patch literal 3148 zcmaJ@U1%Fe5Z=8X>y*={`lFO4g(j#NRd==J$am`A zxlYZwh|q#4p>acs1RZSqw~Lb5c-pTG#j_wc{~lBGE$I&^XO9JjW1#L&0XRY?lW|W zS9qX;A^;T?5onLn1Jt8wea)E& z!Q4YT<_Z>n3*FvjNvDPt)c7-t1D}g27n9Y%>0DloX}THvP+!)R)$r@*&!u9sYF1Zr z+I;L3Av$^DVXui;+~W#*LDgJg(a?eB^^D^7QVl8HJWb~_L02r0oMyT~s*9;o8{^(n zSF&os%;{QOaJ?VGytzWgTyO>2bp<73W?ZZq0{mwT+LMKuiS)aMN{nAr>V0f z_pzFu%4QUGDVH_UGYvc6CiQ@)s5Po09g}UcIfYt-XgI$^)B-6Ju?%A^GjVU`>iTQq-2B z8v#b;pd&}AP^e9qps!poi?_$2xfzwG%5-an5`kMQ?j?46qA z?@#)n|Es)zI?DeV6`|ji*fdF5Vvj?oyd$l<6JQNnSiy%tuF=*w7P?%@6))(QRV|L$ ze#Lp>2LTcU;)*8a7<2`QA<|37ymWinI5`O4RH1PgND*zw;q|jMdC-vui?elKXgzZ6 zt>US=94fx~7z9wZBX!%5^d)cr_Q1MZko>p#HLeSsIEC*97XT;T;;StPqMDn{Lf!w2 z8942RCk35Hdjk~Z#D&yq6rwz_Gs+YBj5fcVnOBX(syN2-7O%uPLSx(&S4hrOAsJ=L znA1O?N#sh+Da@;`M_)E+g0}Lq;Zr$F)QqV%vStvp9f8OA8b}elyLfiP z>o4Uh;hOib<2?)^FAi5Dbt!n=|Al}3a!nd?q#;`xf-Dkg#%vSFB9UfM=UAJ&qt{5K z{}DB4gp?0X@IMNZp8FV2db)y=Vh#(+f2bJUTC}~jxN~b4TU)rhs#sjR<|zDSstr)u zgxc9`iqI*u7h2P;w74p@W>>odBX?V)-RR%0Di*JZ-}Q8d-(&<%6qm7_IgA(QlxS&}#OGnj6P% zPfk-*&4el;5%r^G)i4d0hs<+j!_1h=#vBwCmC#cKp-yo{WqGM!5E?sikqpowewpOy zY@?uShU)rQjmYYN+?q-HLBgO5LS8pB+dr>rDxq;mMalEPeU^%S4g}(GU-I4=dtb6~ z7iTttk!x0ErWTAj!C3LkAD*D&>8}_MJVUl;hymwVMRJ~c!48bUU6T@yl(3~lT?&+X z{s{M%Pu@650l)a>!@h$zpR1(r9kKhyYklKR-+1wj4I#KLSKg}$qmD3YH*XRFOZE;m z6F6m*gM*8Os?guv6g2hY+1bj)BgX;4$6?0y`2pWDEQ2 zk^b_P8&|4``-vD^n zH@a1q`)#4W;oMG$rlH94cnmsa^k0spWu-d@z%5*A^}vO8l($bsgen8fOu(!0=XA+t z2nJbkY0o3aftiHIpmhd*S?>Q`jy{m1RmpyCswPi6^0X~XGuz`{mI`D9BwbE7SUF=g zilqgMXXZkt^jkQ2f*`}+C(>(6y0V;CCkb^g#U*#>J*Ih^6BD*%F0ERO| pi;VKP7u??@Aszz|w@5(7&upR=8R)@qJZX_WjAt-x(4LeT{4W!^!UO;S delta 715 zcmZuuziSjh6rNw3&1SvJ#oU=F1{VUtM)5#QNFj>FX(6Vui^XNV9m6J@T{Al|CPeQL zkqBv)Es`=IC}JlzVqNyaVe4NA>sf3BWdzhjKpl z$xDLJ8(4pB5Fh$df@Fd10{5>q^nxA<*~y0r~B! A7ytkO diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 63a5c1f..674a7a3 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -1,4 +1,6 @@ import json, requests +import os + # Function to parse json file, given its path def get_json(file_path): @@ -29,7 +31,7 @@ def get_token(file_path): # Input: a JSON object # Output: None, but creates a local JSON file containing the object def create_json(json_object, language): - file_path = "src/internationalize/languages/" + language + ".json" + file_path = f"src/internationalize/languages/{language}.json" with open(file_path, "w") as outfile: outfile.write(json_object) @@ -39,13 +41,11 @@ def generate_file(language): url = f'http://localhost:8000/api/translations/{language}/' response = requests.get(url) - file_content = None - - if response.status_code == 200: - file_content = response.json() - else: - print(f'Error: {response.status_code}, {response.data['error']}') - + if response.status_code != 200: + print(f'Error: {response.status_code}') + return + + file_content = response.json() # transforms the dictionary object above into a JSON object json_object = json.dumps(file_content, indent=4) create_json(json_object, language) diff --git a/i18nilize/src/internationalize/languages/translations.json b/i18nilize/src/internationalize/languages/translations.json deleted file mode 100644 index 9a4373a..0000000 --- a/i18nilize/src/internationalize/languages/translations.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "Token": "85124f79-0829-4b80-8b5c-d52700d86e46", - "translations": [ - { - "language": "French", - "hello": "bonjour", - "No": "Non", - "Why": "pourquoi" - }, - { - "language": "Spanish", - "hello": "Hola" - } - ] -} \ No newline at end of file From 8601a91502096d13d6be9786fbc899f76d1e1862 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 4 Nov 2024 00:19:15 -0800 Subject: [PATCH 04/20] added tests for api --- core/i18nilize/tests.py | 58 ++++++++++++++++++++++++++++++++++++++++- core/i18nilize/views.py | 3 +++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/core/i18nilize/tests.py b/core/i18nilize/tests.py index c973724..c6134cb 100644 --- a/core/i18nilize/tests.py +++ b/core/i18nilize/tests.py @@ -2,7 +2,7 @@ from rest_framework import status from rest_framework.test import APITestCase from .models import Token, Translation -from .services.translation_processor import bulk_create_translations, bulk_update_translations +from .services.translation_processor import bulk_create_translations, bulk_update_translations, get_translations_by_language class TokenViewTests(APITestCase): @@ -327,6 +327,62 @@ def test_update_translations_rollback(self): translations = Translation.objects.all() self.assertEqual(len(translations), 0) + def test_get_translations_no_language_provided(self): + headers = { + 'HTTP_Token': self.TEST_TOKEN + } + response = self.client.get(reverse('get-translations', args=['']), **headers) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(response.data['error'], 'Language is required.') + + def test_get_translations_no_translations_found(self): + translations_data = { + 'translations': [ + { + 'language': 'spanish', + 'hello': 'hola' + } + ] + } + + headers = { + 'HTTP_Token': self.TEST_TOKEN + } + + # create translations + response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get(reverse('get-translations', args=['french']), **headers) + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + self.assertEqual(response.data['error'], 'No translations found for french.') + + def test_get_translations_by_language(self): + translations_data = { + 'translations': [ + { + 'language': 'spanish', + 'hello': 'hola' + } + ] + } + + expected_response_data = { + 'hello': 'hola' + } + + headers = { + 'HTTP_Token': self.TEST_TOKEN + } + + # create translations + response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get(reverse('get-translations', args=['spanish']), **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data, expected_response_data) + class TranslationViewTests(APITestCase): diff --git a/core/i18nilize/views.py b/core/i18nilize/views.py index 31f69dc..0143c21 100644 --- a/core/i18nilize/views.py +++ b/core/i18nilize/views.py @@ -141,6 +141,9 @@ def get(self, request, language): """ token = request.token + if not language: + return Response({'error': 'Language is required.'}, status=status.HTTP_400_BAD_REQUEST) + translations = tp.get_translations_by_language(language, token) if not translations: return Response( From 96b5ec4c8f58a0503ef15fbf256100359d3cbc17 Mon Sep 17 00:00:00 2001 From: Andrew Ahn Date: Mon, 4 Nov 2024 22:37:11 -0800 Subject: [PATCH 05/20] Fixed generate_file --- .../__pycache__/helpers.cpython-311.pyc | Bin 3148 -> 2860 bytes i18nilize/src/internationalize/helpers.py | 20 ++++++++++++------ .../internationalize/languages/spanish.json | 5 +++++ i18nilize/src/internationalize/test.py | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 i18nilize/src/internationalize/languages/spanish.json diff --git a/i18nilize/src/internationalize/__pycache__/helpers.cpython-311.pyc b/i18nilize/src/internationalize/__pycache__/helpers.cpython-311.pyc index dab2cb703032073733ed297165d41cf517536b1a..67816824ac66dff3152701dcf06ed1bc1ee0370e 100644 GIT binary patch delta 693 zcmY*XJ8u&~5Z=9~ALkdu4vGOefP_zgttf&jLNuWO0tFQf*2=dzJGtD2-L*iGBm5s$ z2nsqRq6i~|sQCvJlx90hBnmeaAdr|n#}TY0?Rc!yUikn&CD!NH1Plr55N8gmo;Mx(*XI+hKo=%jI0m0v7X_gd|U z#NmL1l(+Zn))r4qW7Yxc<7&fATLka&&)92kKaSTD_eEHH$Q*4D8<#DP6_UKcd$N&n&@Rwhivw^&jB&Yn+W|1?^*a!Ym_aBFfS z_W*bYxy{u}@D3xo0!J#7>qgCITuNumNX#Q5`Y@UmNUqBp`E&PbnwOo4&M^9DK)K+` zNI28ChgG%FgG}Wx{neCwh?r)kSLjz}Z6^r^5xvf?%5k2QD){0Q9h;^+?OnDs`Y$*? Br5peN delta 1000 zcmZV-O=}ZDbaprUk+jLyTGN&kjkVeYOSVNQrPzj2kRp2W- zFsR?Xl6$AViToCXL)s~R7rNj65?>2r-AtF1P?!;8UmCjK2m02TyyKt+7P zOZX4gnPjsqtx6==zJP+<;wzFRZz=2SDVy!*R0D*!=maVOI;Qq7Ee_8UQkpaj+s&Hx zyo<@?#OUa#VV3d+L8gOkzx~)a6pVxS%op(-2mEPC!Bg^%1s#%9Jq^M8Wrzv(BsNN~ z0~3>!aVG0p5b^auyaS$Wm5U|p4VX)H8≤B@es9qb<2}*Ei2raX$`N05 szdwE^#Qin^;!gfwcc@)2x>nhS(`cM7hfKXg82$m;Xn-DtM*M3303Cnp`~Uy| diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 674a7a3..4178dbc 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -31,21 +31,27 @@ def get_token(file_path): # Input: a JSON object # Output: None, but creates a local JSON file containing the object def create_json(json_object, language): - file_path = f"src/internationalize/languages/{language}.json" + file_path = f"./languages/{language}.json" with open(file_path, "w") as outfile: outfile.write(json_object) # Input: language # Output: None, but creates a local JSON file containing translations def generate_file(language): - url = f'http://localhost:8000/api/translations/{language}/' - response = requests.get(url) + # url = f'http://localhost:8000/api/translations/{language}/' + # response = requests.get(url) - if response.status_code != 200: - print(f'Error: {response.status_code}') - return + # if response.status_code != 200: + # print(f'Error: {response.status_code}') + # return + + # file_content = response.json() + file_content = { + "language": "Spanish", + "hello": "hola", + "thanks": "gracias" + } - file_content = response.json() # transforms the dictionary object above into a JSON object json_object = json.dumps(file_content, indent=4) create_json(json_object, language) diff --git a/i18nilize/src/internationalize/languages/spanish.json b/i18nilize/src/internationalize/languages/spanish.json new file mode 100644 index 0000000..a8270fa --- /dev/null +++ b/i18nilize/src/internationalize/languages/spanish.json @@ -0,0 +1,5 @@ +{ + "language": "Spanish", + "hello": "hola", + "thanks": "gracias" +} \ No newline at end of file diff --git a/i18nilize/src/internationalize/test.py b/i18nilize/src/internationalize/test.py index 068c988..f0d14fa 100644 --- a/i18nilize/src/internationalize/test.py +++ b/i18nilize/src/internationalize/test.py @@ -1,3 +1,3 @@ from helpers import generate_file -generate_file("Spanish") \ No newline at end of file +generate_file("spanish") \ No newline at end of file From cd5db5e75427dbad13b3c36bc071971ab2ec4a29 Mon Sep 17 00:00:00 2001 From: Andrew Ahn Date: Mon, 4 Nov 2024 22:54:08 -0800 Subject: [PATCH 06/20] Removed outdated tests. --- i18nilize/src/internationalize/test.py | 3 --- i18nilize/tests/test_generate_file.py | 29 -------------------------- 2 files changed, 32 deletions(-) delete mode 100644 i18nilize/src/internationalize/test.py delete mode 100644 i18nilize/tests/test_generate_file.py diff --git a/i18nilize/src/internationalize/test.py b/i18nilize/src/internationalize/test.py deleted file mode 100644 index f0d14fa..0000000 --- a/i18nilize/src/internationalize/test.py +++ /dev/null @@ -1,3 +0,0 @@ -from helpers import generate_file - -generate_file("spanish") \ No newline at end of file diff --git a/i18nilize/tests/test_generate_file.py b/i18nilize/tests/test_generate_file.py deleted file mode 100644 index 0ba59a7..0000000 --- a/i18nilize/tests/test_generate_file.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest -import json -from src.internationalize.generate_file import generate_file -from src.internationalize.helpers import get_json - -# run tests using python -m tests.test_generate_file at i18nilize directory level - -class TestGenerateFile(unittest.TestCase): - generate_file() - data = get_json("src/internationalize/jsonFile/translations.json") - - def test_token(self): - self.assertEqual(self.data['Token'], "85124f79-0829-4b80-8b5c-d52700d86e46") - - def test_translations(self): - translations = self.data['translations'] - self.assertEqual(len(translations), 2) - - # French - self.assertEqual(translations[0]['language'], "French") - self.assertEqual(translations[0]['hello'], "bonjour") - self.assertEqual(translations[0]['No'], "Non") - self.assertEqual(translations[0]['Why'], "pourquoi") - - # Spanish - self.assertEqual(translations[1]['language'], "Spanish") - self.assertEqual(translations[1]['hello'], "Hola") - -unittest.main() \ No newline at end of file From 8c4a5244080f3b491fed8a739a8336b7db3412fb Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Tue, 5 Nov 2024 00:16:57 -0800 Subject: [PATCH 07/20] fix api tests --- .../services/translation_processor.py | 7 ++--- core/i18nilize/tests.py | 26 +++++++++---------- core/i18nilize/urls.py | 3 +-- core/i18nilize/views.py | 6 ++--- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/core/i18nilize/services/translation_processor.py b/core/i18nilize/services/translation_processor.py index dc5daca..f7ef3d5 100644 --- a/core/i18nilize/services/translation_processor.py +++ b/core/i18nilize/services/translation_processor.py @@ -162,8 +162,9 @@ def get_translations_by_language(language, token): Return all translations for the given language as a dictionary. """ translations = Translation.objects.filter(language=language, token=token) - if not translations: - translations = create_default_translations_for_language(language, token) + + # if not translations: + # translations = create_default_translations_for_language(language, token) translations_dict = { translation.original_word: translation.translated_word @@ -171,5 +172,5 @@ def get_translations_by_language(language, token): } return translations_dict -def create_default_translations_for_language(language, token): +# def create_default_translations_for_language(language, token): # to be implemented by Brian & Angela's ticket? \ No newline at end of file diff --git a/core/i18nilize/tests.py b/core/i18nilize/tests.py index c6134cb..f0caedb 100644 --- a/core/i18nilize/tests.py +++ b/core/i18nilize/tests.py @@ -327,14 +327,6 @@ def test_update_translations_rollback(self): translations = Translation.objects.all() self.assertEqual(len(translations), 0) - def test_get_translations_no_language_provided(self): - headers = { - 'HTTP_Token': self.TEST_TOKEN - } - response = self.client.get(reverse('get-translations', args=['']), **headers) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(response.data['error'], 'Language is required.') - def test_get_translations_no_translations_found(self): translations_data = { 'translations': [ @@ -344,16 +336,20 @@ def test_get_translations_no_translations_found(self): } ] } + query_params = { + 'language': 'french' + } headers = { 'HTTP_Token': self.TEST_TOKEN } - # create translations + # create spanish translations response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) - - response = self.client.get(reverse('get-translations', args=['french']), **headers) + + # fetch french translations + response = self.client.get(reverse('process-translations'), **headers, query_params=query_params) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.data['error'], 'No translations found for french.') @@ -366,6 +362,9 @@ def test_get_translations_by_language(self): } ] } + query_params = { + 'language': 'spanish' + } expected_response_data = { 'hello': 'hola' @@ -375,11 +374,12 @@ def test_get_translations_by_language(self): 'HTTP_Token': self.TEST_TOKEN } - # create translations + # create spanish translations response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) - response = self.client.get(reverse('get-translations', args=['spanish']), **headers) + # fetch spanish translations + response = self.client.get(reverse('process-translations'), **headers, query_params=query_params) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, expected_response_data) diff --git a/core/i18nilize/urls.py b/core/i18nilize/urls.py index 859a7f4..5048bee 100644 --- a/core/i18nilize/urls.py +++ b/core/i18nilize/urls.py @@ -6,6 +6,5 @@ path('token/', TokenView.as_view(), name='create-token'), path('token//', TokenView.as_view(), name='read-token'), path('translation', TranslationView.as_view(), name='translation'), - path('translations', views.ProcessTranslationsView.as_view(), name='process-translations'), - path('translations/', views.ProcessTranslationsView.as_view(), name='get-translations') + path('translations', views.ProcessTranslationsView.as_view(), name='process-translations') ] diff --git a/core/i18nilize/views.py b/core/i18nilize/views.py index 0143c21..721c61f 100644 --- a/core/i18nilize/views.py +++ b/core/i18nilize/views.py @@ -135,14 +135,12 @@ def patch(self, request): ) @require_valid_token - def get(self, request, language): + def get(self, request): """ Fetch translations for a given language. """ token = request.token - - if not language: - return Response({'error': 'Language is required.'}, status=status.HTTP_400_BAD_REQUEST) + language = request.query_params.get('language') translations = tp.get_translations_by_language(language, token) if not translations: From 0df78d4f3c957c3d4ce4eac22f1bb5a7da6b0dda Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Tue, 5 Nov 2024 00:22:53 -0800 Subject: [PATCH 08/20] edit url in generate_file --- i18nilize/src/internationalize/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 4178dbc..be0f0b9 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -38,7 +38,7 @@ def create_json(json_object, language): # Input: language # Output: None, but creates a local JSON file containing translations def generate_file(language): - # url = f'http://localhost:8000/api/translations/{language}/' + # url = f'http://localhost:8000/api/translations/' # response = requests.get(url) # if response.status_code != 200: From d79119a78e713bd3f0795b710e2c345296bd14d2 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Tue, 5 Nov 2024 00:28:50 -0800 Subject: [PATCH 09/20] remove unused import --- i18nilize/src/internationalize/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index be0f0b9..29369f1 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -1,4 +1,4 @@ -import json, requests +import json import os From e3f018e5b1f1f6bea2b01e08bbe8ce715ac686fe Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sat, 9 Nov 2024 02:03:24 -0800 Subject: [PATCH 10/20] draft test for fetch --- i18nilize/src/internationalize/helpers.py | 22 +++++++--------- i18nilize/tests/test_generate_file.py | 31 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 i18nilize/tests/test_generate_file.py diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 29369f1..6857314 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -1,6 +1,6 @@ import json import os - +import requests # Function to parse json file, given its path def get_json(file_path): @@ -37,20 +37,16 @@ def create_json(json_object, language): # Input: language # Output: None, but creates a local JSON file containing translations -def generate_file(language): - # url = f'http://localhost:8000/api/translations/' - # response = requests.get(url) +def generate_file(language, token): + url = 'http://localhost:8000/api/translations/' + params = {'language': language, 'token': token} + response = requests.get(url, params=params) - # if response.status_code != 200: - # print(f'Error: {response.status_code}') - # return + if response.status_code != 200: + print(f'Error: {response.status_code}') + return - # file_content = response.json() - file_content = { - "language": "Spanish", - "hello": "hola", - "thanks": "gracias" - } + file_content = response.json() # transforms the dictionary object above into a JSON object json_object = json.dumps(file_content, indent=4) diff --git a/i18nilize/tests/test_generate_file.py b/i18nilize/tests/test_generate_file.py new file mode 100644 index 0000000..768a4b5 --- /dev/null +++ b/i18nilize/tests/test_generate_file.py @@ -0,0 +1,31 @@ +from src.internationalize.helpers import generate_file + +class TestGenerateFile(): + def setUp(self): + token = Token.objects.create() + self.TEST_TOKEN = str(token.value) + + translations_data = { + 'translations': [ + { + 'language': 'spanish', + 'hello': 'hola', + 'thanks': 'gracias' + } + ] + } + headers = { + 'HTTP_Token': self.TEST_TOKEN + } + + # create default translations + response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') + + def test_generate_file: + generate_file('spanish', self.TEST_TOKEN) + + + + +if __name__ == '__main__': + unittest.main() From a99c6c28ab8d006d75e8bbdb944ccd11ce258f02 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sat, 9 Nov 2024 23:04:02 -0800 Subject: [PATCH 11/20] add tests for generate_file --- i18nilize/src/internationalize/helpers.py | 5 +- .../internationalize/languages/spanish.json | 1 - i18nilize/tests/test_generate_file.py | 54 ++++++++++++------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 6857314..993deec 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -31,8 +31,9 @@ def get_token(file_path): # Input: a JSON object # Output: None, but creates a local JSON file containing the object def create_json(json_object, language): - file_path = f"./languages/{language}.json" - with open(file_path, "w") as outfile: + base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) + file_path = os.path.join(base_dir, 'languages', f'{language}.json') + with open(file_path, 'w') as outfile: outfile.write(json_object) # Input: language diff --git a/i18nilize/src/internationalize/languages/spanish.json b/i18nilize/src/internationalize/languages/spanish.json index a8270fa..11336ed 100644 --- a/i18nilize/src/internationalize/languages/spanish.json +++ b/i18nilize/src/internationalize/languages/spanish.json @@ -1,5 +1,4 @@ { - "language": "Spanish", "hello": "hola", "thanks": "gracias" } \ No newline at end of file diff --git a/i18nilize/tests/test_generate_file.py b/i18nilize/tests/test_generate_file.py index 768a4b5..d0b8945 100644 --- a/i18nilize/tests/test_generate_file.py +++ b/i18nilize/tests/test_generate_file.py @@ -1,31 +1,45 @@ +import unittest +from unittest.mock import patch, Mock +import json +import os from src.internationalize.helpers import generate_file -class TestGenerateFile(): +# run tests using python -m tests.test_generate_file at i18nilize directory level + +class TestGenerateFile(unittest.TestCase): def setUp(self): - token = Token.objects.create() - self.TEST_TOKEN = str(token.value) - - translations_data = { - 'translations': [ - { - 'language': 'spanish', - 'hello': 'hola', - 'thanks': 'gracias' - } - ] - } - headers = { - 'HTTP_Token': self.TEST_TOKEN - } + self.TEST_TOKEN = '85124f79-0829-4b80-8b5c-d52700d86e46' - # create default translations - response = self.client.post(reverse('process-translations'), translations_data, **headers, format='json') + @patch('src.internationalize.helpers.requests.get') + def test_generate_file_success(self, mock_get): + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = { + 'hello': 'hola', + 'thanks': 'gracias' + } + mock_get.return_value = mock_response - def test_generate_file: generate_file('spanish', self.TEST_TOKEN) + + expected_file_path = './src/internationalize/languages/spanish.json' + self.assertTrue(os.path.exists(expected_file_path)) + with open (expected_file_path, 'r') as file: + content = file.read() + expected_content = json.dumps(mock_response.json.return_value, indent = 4) + self.assertEqual(content, expected_content) + + @patch('src.internationalize.helpers.requests.get') + def test_generate_file_error(self, mock_get): + mock_response = Mock() + mock_response.status_code = 404 + mock_get.return_value = mock_response - + generate_file('french', self.TEST_TOKEN) + + expected_file_path = './src/internationalize/languages/french.json' + self.assertFalse(os.path.exists(expected_file_path)) if __name__ == '__main__': unittest.main() From cfeabb2754bd34054cff5e1d31f4b9ca3950b818 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sat, 9 Nov 2024 23:42:42 -0800 Subject: [PATCH 12/20] fix url in generate_file --- i18nilize/src/internationalize/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 993deec..65232ac 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -39,7 +39,7 @@ def create_json(json_object, language): # Input: language # Output: None, but creates a local JSON file containing translations def generate_file(language, token): - url = 'http://localhost:8000/api/translations/' + url = 'http://localhost:8000/api/process-translations/' params = {'language': language, 'token': token} response = requests.get(url, params=params) From c613a6d4cf6f1c3894c26cef8b70a60022c1af50 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sat, 9 Nov 2024 23:43:45 -0800 Subject: [PATCH 13/20] start writing integration test --- i18nilize/integration_test.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 i18nilize/integration_test.py diff --git a/i18nilize/integration_test.py b/i18nilize/integration_test.py new file mode 100644 index 0000000..1870626 --- /dev/null +++ b/i18nilize/integration_test.py @@ -0,0 +1,34 @@ +# to be removed + +from src.internationalize.helpers import generate_file +from core.i18nilize.models import Token +import requests + +def integration_test(): + # create token + token_response = requests.post('http://localhost:8000/api/create-token/') + token_data = token_response.json() + token = token_data['value'] + + # create translations + translations_data = { + 'translations': [ + { + 'language': 'french', + 'hello': 'bonjour', + 'thanks': 'merci' + } + ] + } + headers = { + 'Authorization': f'Token {token}' + } + translations_response = requests.post( + 'http://localhost:8000/api/process-translations/', + data=translations_data, + headers=headers + ) + + generate_file('french', token) + +integration_test() \ No newline at end of file From 15fc171e392b97d98ef46177aafcd7783622f16f Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sun, 10 Nov 2024 15:32:27 -0800 Subject: [PATCH 14/20] update get request --- i18nilize/integration_test.py | 34 ----------------------- i18nilize/src/internationalize/helpers.py | 7 +++-- 2 files changed, 4 insertions(+), 37 deletions(-) delete mode 100644 i18nilize/integration_test.py diff --git a/i18nilize/integration_test.py b/i18nilize/integration_test.py deleted file mode 100644 index 1870626..0000000 --- a/i18nilize/integration_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# to be removed - -from src.internationalize.helpers import generate_file -from core.i18nilize.models import Token -import requests - -def integration_test(): - # create token - token_response = requests.post('http://localhost:8000/api/create-token/') - token_data = token_response.json() - token = token_data['value'] - - # create translations - translations_data = { - 'translations': [ - { - 'language': 'french', - 'hello': 'bonjour', - 'thanks': 'merci' - } - ] - } - headers = { - 'Authorization': f'Token {token}' - } - translations_response = requests.post( - 'http://localhost:8000/api/process-translations/', - data=translations_data, - headers=headers - ) - - generate_file('french', token) - -integration_test() \ No newline at end of file diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 65232ac..5b96439 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -39,9 +39,10 @@ def create_json(json_object, language): # Input: language # Output: None, but creates a local JSON file containing translations def generate_file(language, token): - url = 'http://localhost:8000/api/process-translations/' - params = {'language': language, 'token': token} - response = requests.get(url, params=params) + url = 'http://localhost:8000/api/translations' + params = {'language': language} + headers = {'token': token} + response = requests.get(url, params=params, headers=headers) if response.status_code != 200: print(f'Error: {response.status_code}') From 169df75d2dd09faae38f2202c294300bd3586b35 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sun, 10 Nov 2024 16:39:06 -0800 Subject: [PATCH 15/20] add requirements.txt --- i18nilize/requirements.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 i18nilize/requirements.txt diff --git a/i18nilize/requirements.txt b/i18nilize/requirements.txt new file mode 100644 index 0000000..a68bff6 --- /dev/null +++ b/i18nilize/requirements.txt @@ -0,0 +1,11 @@ +asgiref==3.8.1 +certifi==2024.8.30 +charset-normalizer==3.4.0 +Django==5.1.1 +djangorestframework==3.15.2 +e==1.4.5 +-e git+https://github.com/ubclaunchpad/localization.git@15fc171e392b97d98ef46177aafcd7783622f16f#egg=i18nilize +idna==3.10 +requests==2.32.3 +sqlparse==0.5.1 +urllib3==2.2.3 From 95eb25fdd02a1fae900fcc536abe4b8a76fd8f03 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sun, 10 Nov 2024 23:49:44 -0800 Subject: [PATCH 16/20] update core requirements.txt --- core/requirements.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/requirements.txt b/core/requirements.txt index ae0dc58..efc5e1f 100644 --- a/core/requirements.txt +++ b/core/requirements.txt @@ -1,4 +1,11 @@ asgiref==3.8.1 +certifi==2024.8.30 +charset-normalizer==3.4.0 Django==5.1.1 djangorestframework==3.15.2 +e==1.4.5 +-e git+https://github.com/ubclaunchpad/localization.git@169df75d2dd09faae38f2202c294300bd3586b35#egg=i18nilize +idna==3.10 +requests==2.32.3 sqlparse==0.5.1 +urllib3==2.2.3 From cd75714c68517526e417cc0c7721c1c8d832e18d Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sun, 10 Nov 2024 23:52:14 -0800 Subject: [PATCH 17/20] remove i18nilize from requirments.txt --- core/requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/requirements.txt b/core/requirements.txt index efc5e1f..5254a41 100644 --- a/core/requirements.txt +++ b/core/requirements.txt @@ -3,8 +3,6 @@ certifi==2024.8.30 charset-normalizer==3.4.0 Django==5.1.1 djangorestframework==3.15.2 -e==1.4.5 --e git+https://github.com/ubclaunchpad/localization.git@169df75d2dd09faae38f2202c294300bd3586b35#egg=i18nilize idna==3.10 requests==2.32.3 sqlparse==0.5.1 From 7874a7d01f8534abf3412d0544910d5819259667 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Sun, 10 Nov 2024 23:53:40 -0800 Subject: [PATCH 18/20] remove i18nilize requirements.txt --- i18nilize/requirements.txt | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 i18nilize/requirements.txt diff --git a/i18nilize/requirements.txt b/i18nilize/requirements.txt deleted file mode 100644 index a68bff6..0000000 --- a/i18nilize/requirements.txt +++ /dev/null @@ -1,11 +0,0 @@ -asgiref==3.8.1 -certifi==2024.8.30 -charset-normalizer==3.4.0 -Django==5.1.1 -djangorestframework==3.15.2 -e==1.4.5 --e git+https://github.com/ubclaunchpad/localization.git@15fc171e392b97d98ef46177aafcd7783622f16f#egg=i18nilize -idna==3.10 -requests==2.32.3 -sqlparse==0.5.1 -urllib3==2.2.3 From 30c83e916e351097f64ffb502e2a785512c973a3 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 11 Nov 2024 00:13:17 -0800 Subject: [PATCH 19/20] update error printing --- core/db.sqlite3 | Bin 147456 -> 147456 bytes core/i18nilize/views.py | 2 +- i18nilize/src/internationalize/helpers.py | 2 +- .../internationalize/languages/french.json | 4 ++++ 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 i18nilize/src/internationalize/languages/french.json diff --git a/core/db.sqlite3 b/core/db.sqlite3 index e51d55acc38681fb49a70a52b7bf449676ece8c6..60c13c56aa78b1ff1fbd5d00c98e8cbba06f7f4f 100644 GIT binary patch delta 746 zcmaiyJ8u&~6oq#jSbHrz7o>>93KFrPp@==tot+gj;Ze|}O`VyYT^UGhyi zqi~1BA3#C@xuI~MCR7m;R1}DciV6v_izpxIBlTjV zgMw$KGmq*Ib~}^Z4tjLBSa0MWOd*F=@bDDpcoln%X(QinExX33TR-8gU1gj*&75L{ zSwRwMBbP}-voJAS>5LcmVujKbN|qUq8I37zOC|_o-JtX*LMun4b=D<9a29D2hN__2 z)M%;A-+rQE!P`;^rAMq=D*;6FD74NHVys9NwUVWVr!?W#Mj1aAfXJBZwhFaivRf_v znmDaPp6V=-f~3L}lUbC7MIe`jh8+taVxilbGI1^9UB3hrBdp6bk&IFiGGiSRQdtv8 z&Zte?KLAeKMP?F3lPRv;K=XCH=i&Ew4=%t{`~$Y&1AaZhrz`jI=;GWOn$>W<6wRtJ zC`GfXs+FQY`-uNnNVQbWTG}7qJYRWxHh`7~pWzVRz;k#AcVQhaL#y~dRglfV_ZF6# zsF!YT4(y`w#UG?+@t@;}6{r*Rv5|!4H?$KLRO{cPsS( diff --git a/core/i18nilize/views.py b/core/i18nilize/views.py index 721c61f..8adc567 100644 --- a/core/i18nilize/views.py +++ b/core/i18nilize/views.py @@ -145,7 +145,7 @@ def get(self, request): translations = tp.get_translations_by_language(language, token) if not translations: return Response( - {"error": f"No translations found for {language}."}, + {'error': f'No translations found for {language}.'}, status=status.HTTP_404_NOT_FOUND ) diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index 5b96439..948c72e 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -45,7 +45,7 @@ def generate_file(language, token): response = requests.get(url, params=params, headers=headers) if response.status_code != 200: - print(f'Error: {response.status_code}') + print(f'Error: {response.status_code}.', response.json()['error']) return file_content = response.json() diff --git a/i18nilize/src/internationalize/languages/french.json b/i18nilize/src/internationalize/languages/french.json new file mode 100644 index 0000000..78b6885 --- /dev/null +++ b/i18nilize/src/internationalize/languages/french.json @@ -0,0 +1,4 @@ +{ + "thanks": "merci", + "hello": "bonjour" +} \ No newline at end of file From 9d208bc1877b1213d29f3afc53688256576031fd Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 11 Nov 2024 17:32:04 -0800 Subject: [PATCH 20/20] remove comments --- core/i18nilize/services/translation_processor.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/core/i18nilize/services/translation_processor.py b/core/i18nilize/services/translation_processor.py index f7ef3d5..6c8d60b 100644 --- a/core/i18nilize/services/translation_processor.py +++ b/core/i18nilize/services/translation_processor.py @@ -162,15 +162,9 @@ def get_translations_by_language(language, token): Return all translations for the given language as a dictionary. """ translations = Translation.objects.filter(language=language, token=token) - - # if not translations: - # translations = create_default_translations_for_language(language, token) translations_dict = { translation.original_word: translation.translated_word for translation in translations } - return translations_dict - -# def create_default_translations_for_language(language, token): - # to be implemented by Brian & Angela's ticket? \ No newline at end of file + return translations_dict \ No newline at end of file