def test_use_default_media_ignore_lang(self): # When use_default_media is true and there's media in a non-default language but not the default language self.app.langs = ['en', 'hin'] self.form.use_default_image_for_all = True self.form.use_default_audio_for_all = True self.form.set_icon('en', '') self.form.set_audio('en', '') self.form.set_icon('hin', 'jr://file/commcare/case_list_image_hin.jpg') self.form.set_audio('hin', 'jr://file/commcare/case_list_audio_hin.mp3') en_app_strings = commcare_translations.loads(self.app.create_app_strings('en')) hin_app_strings = commcare_translations.loads(self.app.create_app_strings('hin')) self.assertFalse('forms.m0f0.icon' in en_app_strings) self.assertFalse('forms.m0f0.icon' in hin_app_strings) self.assertFalse('forms.m0f0.audio' in en_app_strings) self.assertFalse('forms.m0f0.audio' in hin_app_strings) self.assertXmlPartialEqual( self.XML_without_media("forms.m0f0"), self.app.create_suite(), "./entry/command[@id='m0-f0']/" )
def test_use_default_media(self): self.module.use_default_image_for_all = True self.module.use_default_audio_for_all = True self.module.set_icon('en', self.image_path) self.module.set_audio('en', self.audio_path) self.module.set_icon('hin', 'jr://file/commcare/case_list_image_hin.jpg') self.module.set_audio('hin', 'jr://file/commcare/case_list_audio_hin.mp3') with flag_enabled('LANGUAGE_LINKED_MULTIMEDIA'): en_app_strings = commcare_translations.loads(self.app.create_app_strings('en')) hin_app_strings = commcare_translations.loads(self.app.create_app_strings('hin')) self.assertEqual(en_app_strings['modules.m0.icon'], hin_app_strings['modules.m0.icon']) self.assertEqual(en_app_strings['modules.m0.audio'], hin_app_strings['modules.m0.audio'])
def test_use_default_media(self): self.app.langs = ['en', 'hin'] self.module.use_default_image_for_all = True self.module.use_default_audio_for_all = True self.module.set_icon('en', self.image_path) self.module.set_audio('en', self.audio_path) self.module.set_icon('hin', 'jr://file/commcare/case_list_image_hin.jpg') self.module.set_audio('hin', 'jr://file/commcare/case_list_audio_hin.mp3') en_app_strings = commcare_translations.loads(self.app.create_app_strings('en')) hin_app_strings = commcare_translations.loads(self.app.create_app_strings('hin')) self.assertEqual(en_app_strings['modules.m0.icon'], hin_app_strings['modules.m0.icon']) self.assertEqual(en_app_strings['modules.m0.audio'], hin_app_strings['modules.m0.audio'])
def test_case_detail_icon_mapping(self): app = Application.new_app('domain', 'Untitled Application') module = app.add_module(Module.new_module('Untitled Module', None)) module.case_type = 'patient' module.case_details.short.columns = [ DetailColumn( header={'en': 'Age range'}, model='case', field='age', format='enum-image', enum=[ MappingItem(key='10', value={'en': 'jr://icons/10-year-old.png'}), MappingItem(key='age > 50', value={'en': 'jr://icons/old-icon.png'}), ], ), ] key1_varname = '10' key2_varname = hashlib.md5('age > 50').hexdigest()[:8] icon_mapping_spec = """ <partial> <template form="image" width="13%"> <text> <xpath function="if(age = '10', $k{key1_varname}, if(age > 50, $h{key2_varname}, ''))"> <variable name="h{key2_varname}"> <locale id="m0.case_short.case_age_1.enum.h{key2_varname}"/> </variable> <variable name="k{key1_varname}"> <locale id="m0.case_short.case_age_1.enum.k{key1_varname}"/> </variable> </xpath> </text> </template> </partial> """.format( key1_varname=key1_varname, key2_varname=key2_varname, ) # check correct suite is generated self.assertXmlPartialEqual( icon_mapping_spec, app.create_suite(), './detail/field/template[@form="image"]' ) # check icons map correctly app_strings = commcare_translations.loads(app.create_app_strings('en')) self.assertEqual( app_strings['m0.case_short.case_age_1.enum.h{key2_varname}'.format(key2_varname=key2_varname,)], 'jr://icons/old-icon.png' ) self.assertEqual( app_strings['m0.case_short.case_age_1.enum.k{key1_varname}'.format(key1_varname=key1_varname,)], 'jr://icons/10-year-old.png' )
def create_default_app_strings(self, app, build_profile_id=None): messages = {} langs = app.get_build_langs(build_profile_id) for lc in reversed(langs): if lc == "default": continue new_messages = commcare_translations.loads( self.create_app_strings(app, lc, for_default=True) ) for key, val in new_messages.items(): # do not overwrite a real trans with a blank trans if not (val == '' and key in messages): messages[key] = val if 'case_sharing.exactly_one_group' not in messages: messages['case_sharing.exactly_one_group'] = \ (u'The case sharing settings for your user are incorrect. ' u'This user must be in exactly one case sharing group. Please contact your supervisor.') if 'case_autoload.fixture.exactly_one_fixture' not in messages: messages['case_autoload.fixture.exactly_one_fixture'] = \ (u'The lookup table settings for your user are incorrect. ' u'This user must have access to exactly one lookup table row for the table: ${0}') from corehq.apps.app_manager.models import ( AUTO_SELECT_CASE, AUTO_SELECT_FIXTURE, AUTO_SELECT_USER, AUTO_SELECT_LOCATION, AUTO_SELECT_USERCASE, AUTO_SELECT_RAW ) mode_text = { AUTO_SELECT_FIXTURE: u'lookup table field', AUTO_SELECT_USER: u'user data key', AUTO_SELECT_CASE: u'case index', AUTO_SELECT_USERCASE: u'user case', AUTO_SELECT_RAW: u'custom xpath expression', } for mode, text in mode_text.items(): key = 'case_autoload.{0}.property_missing'.format(mode) if key not in messages: messages[key] = (u'The {} specified for case auto-selecting ' u'could not be found: ${{0}}').format(text) key = 'case_autoload.{0}.case_missing'.format(mode) if key not in messages: messages[key] = u'Unable to find case referenced by auto-select case ID.' key = 'case_autoload.{0}.property_missing'.format(AUTO_SELECT_LOCATION) messages[key] = (u"This form requires access to the user's location, " "but none was found.") key = 'case_autoload.{0}.case_missing'.format(AUTO_SELECT_LOCATION) messages[key] = (u"This form requires the user's location to be " "marked as 'Tracks Stock'.") return commcare_translations.dumps(messages).encode('utf-8')
def test_language_names(self): factory = AppFactory(build_version='2.40.0') factory.app.langs = ['en', 'fra', 'hin', 'pol'] factory.app.create_suite() app_strings = factory.app.create_app_strings('default') app_strings_dict = commcare_translations.loads(app_strings) self.assertEqual(app_strings_dict['en'], 'English') self.assertEqual(app_strings_dict['fra'], 'Français') self.assertEqual(app_strings_dict['hin'], 'हिंदी') self.assertEqual(app_strings_dict['pol'], 'polski')
def assertHasAllStrings(self, app, strings): et = etree.XML(app) locale_elems = et.findall(".//locale/[@id]") locale_strings = [elem.attrib['id'] for elem in locale_elems] app_strings = commcare_translations.loads(strings) for string in locale_strings: if string not in app_strings.keys(): raise AssertionError("App strings did not contain %s" % string)
def create_default_app_strings(self, app, build_profile_id=None): messages = {} langs = app.get_build_langs(build_profile_id) for lc in reversed(langs): if lc == "default": continue new_messages = commcare_translations.loads( self.create_app_strings(app, lc, for_default=True)) for key, val in new_messages.items(): # do not overwrite a real trans with a blank trans if not (val == '' and key in messages): messages[key] = val if 'case_sharing.exactly_one_group' not in messages: messages['case_sharing.exactly_one_group'] = \ (u'The case sharing settings for your user are incorrect. ' u'This user must be in exactly one case sharing group. Please contact your supervisor.') if 'case_autoload.fixture.exactly_one_fixture' not in messages: messages['case_autoload.fixture.exactly_one_fixture'] = \ (u'The lookup table settings for your user are incorrect. ' u'This user must have access to exactly one lookup table row for the table: ${0}') from corehq.apps.app_manager.models import ( AUTO_SELECT_CASE, AUTO_SELECT_FIXTURE, AUTO_SELECT_USER, AUTO_SELECT_LOCATION, AUTO_SELECT_USERCASE, AUTO_SELECT_RAW) mode_text = { AUTO_SELECT_FIXTURE: u'lookup table field', AUTO_SELECT_USER: u'user data key', AUTO_SELECT_CASE: u'case index', AUTO_SELECT_USERCASE: u'user case', AUTO_SELECT_RAW: u'custom xpath expression', } for mode, text in mode_text.items(): key = 'case_autoload.{0}.property_missing'.format(mode) if key not in messages: messages[key] = (u'The {} specified for case auto-selecting ' u'could not be found: ${{0}}').format(text) key = 'case_autoload.{0}.case_missing'.format(mode) if key not in messages: messages[ key] = u'Unable to find case referenced by auto-select case ID.' key = 'case_autoload.{0}.property_missing'.format(AUTO_SELECT_LOCATION) messages[key] = (u"This form requires access to the user's location, " "but none was found.") key = 'case_autoload.{0}.case_missing'.format(AUTO_SELECT_LOCATION) messages[key] = (u"This form requires the user's location to be " "marked as 'Tracks Stock'.") return commcare_translations.dumps(messages).encode('utf-8')
def _assert_app_strings_available(self, app, lang): et = etree.XML(app.create_suite()) locale_elems = et.findall(".//locale/[@id]") locale_strings = [elem.attrib['id'] for elem in locale_elems] app_strings = commcare_translations.loads(app.create_app_strings(lang)) for string in locale_strings: if string not in app_strings: raise AssertionError("App strings did not contain %s" % string) if not app_strings.get(string, '').strip(): raise AssertionError("App strings has blank entry for %s" % string)
def test_use_default_media(self): self.module.use_default_image_for_all = True self.module.use_default_audio_for_all = True self.module.set_icon('en', self.image_path) self.module.set_audio('en', self.audio_path) self.module.set_icon('hin', 'jr://file/commcare/case_list_image_hin.jpg') self.module.set_audio('hin', 'jr://file/commcare/case_list_audio_hin.mp3') with flag_enabled('LANGUAGE_LINKED_MULTIMEDIA'): en_app_strings = commcare_translations.loads( self.app.create_app_strings('en')) hin_app_strings = commcare_translations.loads( self.app.create_app_strings('hin')) self.assertEqual(en_app_strings['modules.m0.icon'], hin_app_strings['modules.m0.icon']) self.assertEqual(en_app_strings['modules.m0.audio'], hin_app_strings['modules.m0.audio'])
def test_language_names(self): app_json = self.get_json('app') app_json['langs'] = ['en', 'fra', 'hin', 'pol'] app = Application.wrap(app_json) app.create_suite() app_strings = app.create_app_strings('default') app_strings_dict = commcare_translations.loads(app_strings) self.assertEqual(app_strings_dict['en'], 'English') self.assertEqual(app_strings_dict['fra'], u'Français') self.assertEqual(app_strings_dict['hin'], u'हिंदी') self.assertEqual(app_strings_dict['pol'], 'polski')
def _assert_app_strings_available(self, app, lang): et = etree.XML(app.create_suite()) locale_elems = et.findall(".//locale/[@id]") locale_strings = [elem.attrib["id"] for elem in locale_elems] app_strings = commcare_translations.loads(app.create_app_strings(lang)) for string in locale_strings: if string not in app_strings: raise AssertionError("App strings did not contain %s" % string) if not app_strings.get(string, "").strip(): raise AssertionError("App strings has blank entry for %s" % string)
def test_case_detail_icon_mapping(self): app = Application.new_app('domain', 'Untitled Application') module = app.add_module(Module.new_module('Untitled Module', None)) module.case_type = 'patient' module.case_details.short.columns = [ DetailColumn( header={'en': 'Age range'}, model='case', field='age', format='enum-image', enum=[ MappingItem(key='10', value={'en': 'jr://icons/10-year-old.png'}), MappingItem(key='age > 50', value={'en': 'jr://icons/old-icon.png'}), ], ), ] key1_varname = '10' key2_varname = hashlib.md5('age > 50').hexdigest()[:8] icon_mapping_spec = """ <partial> <template form="image" width="13%"> <text> <xpath function="if(age = '10', $k{key1_varname}, if(age > 50, $h{key2_varname}, ''))"> <variable name="h{key2_varname}"> <locale id="m0.case_short.case_age_1.enum.h{key2_varname}"/> </variable> <variable name="k{key1_varname}"> <locale id="m0.case_short.case_age_1.enum.k{key1_varname}"/> </variable> </xpath> </text> </template> </partial> """.format( key1_varname=key1_varname, key2_varname=key2_varname, ) # check correct suite is generated self.assertXmlPartialEqual(icon_mapping_spec, app.create_suite(), './detail/field/template[@form="image"]') # check icons map correctly app_strings = commcare_translations.loads(app.create_app_strings('en')) self.assertEqual( app_strings['m0.case_short.case_age_1.enum.h{key2_varname}'.format( key2_varname=key2_varname, )], 'jr://icons/old-icon.png') self.assertEqual( app_strings['m0.case_short.case_age_1.enum.k{key1_varname}'.format( key1_varname=key1_varname, )], 'jr://icons/10-year-old.png')
def assertHasAllStrings(self, app, strings): et = etree.XML(app) locale_elems = et.findall(".//locale/[@id]") locale_strings = [elem.attrib["id"] for elem in locale_elems] app_strings = commcare_translations.loads(strings) for string in locale_strings: if string not in app_strings: raise AssertionError("App strings did not contain %s" % string) if not app_strings.get(string, "").strip(): raise AssertionError("App strings has blank entry for %s" % string)
def test_use_default_media(self): self.app.langs = ['en', 'hin'] self.module.use_default_image_for_all = True self.module.use_default_audio_for_all = True self.module.set_icon('en', self.image_path) self.module.set_audio('en', self.audio_path) self.module.set_icon('hin', 'jr://file/commcare/case_list_image_hin.jpg') self.module.set_audio('hin', 'jr://file/commcare/case_list_audio_hin.mp3') en_app_strings = commcare_translations.loads( self.app.create_app_strings('en')) hin_app_strings = commcare_translations.loads( self.app.create_app_strings('hin')) self.assertEqual(en_app_strings['modules.m0.icon'], hin_app_strings['modules.m0.icon']) self.assertEqual(en_app_strings['modules.m0.audio'], hin_app_strings['modules.m0.audio'])
def test_case_detail_icon_mapping(self): app = Application.new_app('domain', 'Untitled Application', application_version=APP_V2) module = app.add_module(Module.new_module('Untitled Module', None)) module.case_type = 'patient' module.case_details.short.columns = [ DetailColumn( header={'en': 'c'}, model='case', field='gender', format='enum-image', enum=[ MappingItem(key='male', value={'en': 'jr://icons/male-icon.png'}), MappingItem(key='female', value={'en': 'jr://icons/female-icon.png'}), ], case_tile_field='sex' ), ] icon_mapping_spec = """ <partial> <template form="image" width="13%"> <text> <xpath function="if(gender = 'male', $kmale, if(gender = 'female', $kfemale, ''))"> <variable name="kfemale"> <locale id="m0.case_short.case_gender_1.enum.kfemale"/> </variable> <variable name="kmale"> <locale id="m0.case_short.case_gender_1.enum.kmale"/> </variable> </xpath> </text> </template> </partial> """ # check correct suite is generated self.assertXmlPartialEqual( icon_mapping_spec, app.create_suite(), './detail/field/template[@form="image"]' ) # check icons map correctly app_strings = commcare_translations.loads(app.create_app_strings('en')) self.assertEqual( app_strings['m0.case_short.case_gender_1.enum.kfemale'], 'jr://icons/female-icon.png' ) self.assertEqual( app_strings['m0.case_short.case_gender_1.enum.kmale'], 'jr://icons/male-icon.png' )
def assertHasAllStrings(self, app, strings): et = etree.XML(app) locale_elems = et.findall(".//locale/[@id]") locale_strings = [elem.attrib['id'] for elem in locale_elems] app_strings = commcare_translations.loads(strings) for string in locale_strings: if string not in app_strings: raise AssertionError("App strings did not contain %s" % string) if not app_strings.get(string, '').strip(): raise AssertionError("App strings has blank entry for %s" % string)
def create_from_txt(cls, lang, txt=None): """ from corehq.apps.translations.models import * TranslationDoc.create_from_txt("pt") """ if txt: dct = commcare_translations.loads(txt) else: dct = commcare_translations.load_translations(lang) t = cls(translations={lang: dct}) t.save() return t
def test_case_detail_calculated_conditional_enum(self, *args): app = Application.new_app('domain', 'Untitled Application') module = app.add_module(Module.new_module('Unititled Module', None)) module.case_type = 'patient' module.case_details.short.columns = [ DetailColumn( header={'en': 'Gender'}, model='case', field="if(gender = 'male', 'boy', 'girl')", format='enum', enum=[ MappingItem(key="boy", value={'en': 'Boy'}), MappingItem(key="girl", value={'en': 'Girl'}), ], ), ] icon_mapping_spec = """ <partial> <template> <text> <xpath function="replace(join(' ', if(selected(if(gender = 'male', 'boy', 'girl'), 'boy'), $kboy, ''), if(selected(if(gender = 'male', 'boy', 'girl'), 'girl'), $kgirl, '')), '\\s+', ' ')"> <variable name="kboy"> <locale id="m0.case_short.case_if(gender 'male', 'boy', 'girl')_1.enum.kboy"/> </variable> <variable name="kgirl"> <locale id="m0.case_short.case_if(gender 'male', 'boy', 'girl')_1.enum.kgirl"/> </variable> </xpath> </text> </template> </partial> """ # noqa: #501 # check correct suite is generated self.assertXmlPartialEqual( icon_mapping_spec, app.create_suite(), './detail[@id="m0_case_short"]/field/template') # check app strings mapped correctly app_strings = commcare_translations.loads(app.create_app_strings('en')) self.assertEqual( app_strings[ "m0.case_short.case_if(gender 'male', 'boy', 'girl')_1.enum.kboy"], 'Boy') self.assertEqual( app_strings[ "m0.case_short.case_if(gender 'male', 'boy', 'girl')_1.enum.kgirl"], 'Girl')
def create_default_app_strings(self, app): messages = {} for lc in reversed(app.langs): if lc == "default": continue new_messages = commcare_translations.loads( self.create_app_strings(app, lc, for_default=True) ) for key, val in new_messages.items(): # do not overwrite a real trans with a blank trans if not (val == '' and key in messages): messages[key] = val if 'case_sharing.exactly_one_group' not in messages: messages['case_sharing.exactly_one_group'] = \ (u'The case sharing settings for your user are incorrect. ' u'This user must be in exactly one case sharing group. Please contact your supervisor.') if 'case_autoload.fixture.exactly_one_fixture' not in messages: messages['case_autoload.fixture.exactly_one_fixture'] = \ (u'The lookup table settings for your user are incorrect. ' u'This user must have access to exactly one lookup table row for the table: ${0}') from corehq.apps.app_manager.models import AUTO_SELECT_CASE, AUTO_SELECT_FIXTURE, AUTO_SELECT_USER mode_text = { AUTO_SELECT_FIXTURE: u'lookup table field', AUTO_SELECT_USER: u'user data key', AUTO_SELECT_CASE: u'case index' } for mode in [AUTO_SELECT_FIXTURE, AUTO_SELECT_CASE, AUTO_SELECT_USER]: key = 'case_autoload.{0}.property_missing'.format(mode) if key not in messages: messages[key] = (u'The {} specified for case auto-selecting ' u'could not be found: ${{0}}').format(mode_text[mode]) for mode in [AUTO_SELECT_FIXTURE, AUTO_SELECT_CASE, AUTO_SELECT_USER]: key = 'case_autoload.{0}.case_missing'.format(mode) if key not in messages: messages[key] = u'Unable to find case referenced by auto-select case ID.' return commcare_translations.dumps(messages).encode('utf-8')
def create_default_app_strings(self, app): messages = {} for lc in reversed(app.langs): if lc == "default": continue new_messages = commcare_translations.loads( self.create_app_strings(app, lc, for_default=True) ) for key, val in new_messages.items(): # do not overwrite a real trans with a blank trans if not (val == '' and key in messages): messages[key] = val if 'case_sharing.exactly_one_group' not in messages: messages['case_sharing.exactly_one_group'] = \ u'Your phone is not set up properly for case sharing. Please contact your supervisor.' return commcare_translations.dumps(messages).encode('utf-8')
def create_default_app_strings(self, app): messages = {} for lc in reversed(app.langs): if lc == "default": continue new_messages = commcare_translations.loads( self.create_app_strings(app, lc, for_default=True) ) for key, val in new_messages.items(): # do not overwrite a real trans with a blank trans if not (val == '' and key in messages): messages[key] = val if 'case_sharing.exactly_one_group' not in messages: messages['case_sharing.exactly_one_group'] = \ (u'The case sharing settings for your user are incorrect. ' u'This user must be in exactly one case sharing group. Please contact your supervisor.') return commcare_translations.dumps(messages).encode('utf-8')
def _assert_valid_media_translation(self, app, lang, media_locale_id, media_path): # assert that <lang>/app_strings.txt contains media_locale_id=media_path app_strings = commcare_translations.loads(app.create_app_strings(lang)) self.assertEqual(app_strings[media_locale_id], media_path)
def test_case_detail_conditional_enum(self, *args): app = Application.new_app('domain', 'Untitled Application') module = app.add_module(Module.new_module('Unititled Module', None)) module.case_type = 'patient' module.case_details.short.columns = [ DetailColumn( header={'en': 'Gender'}, model='case', field='gender', format='conditional-enum', enum=[ MappingItem(key="gender = 'male' and age <= 21", value={'en': 'Boy'}), MappingItem(key="gender = 'female' and age <= 21", value={'en': 'Girl'}), MappingItem(key="gender = 'male' and age > 21", value={'en': 'Man'}), MappingItem(key="gender = 'female' and age > 21", value={'en': 'Woman'}), ], ), ] key1_varname = hashlib.md5( "gender = 'male' and age <= 21".encode('utf-8')).hexdigest()[:8] key2_varname = hashlib.md5( "gender = 'female' and age <= 21".encode('utf-8')).hexdigest()[:8] key3_varname = hashlib.md5( "gender = 'male' and age > 21".encode('utf-8')).hexdigest()[:8] key4_varname = hashlib.md5( "gender = 'female' and age > 21".encode('utf-8')).hexdigest()[:8] icon_mapping_spec = """ <partial> <template> <text> <xpath function="if(gender = 'male' and age <= 21, $h{key1_varname}, if(gender = 'female' and age <= 21, $h{key2_varname}, if(gender = 'male' and age > 21, $h{key3_varname}, if(gender = 'female' and age > 21, $h{key4_varname}, ''))))"> <variable name="h{key4_varname}"> <locale id="m0.case_short.case_gender_1.enum.h{key4_varname}"/> </variable> <variable name="h{key2_varname}"> <locale id="m0.case_short.case_gender_1.enum.h{key2_varname}"/> </variable> <variable name="h{key3_varname}"> <locale id="m0.case_short.case_gender_1.enum.h{key3_varname}"/> </variable> <variable name="h{key1_varname}"> <locale id="m0.case_short.case_gender_1.enum.h{key1_varname}"/> </variable> </xpath> </text> </template> </partial> """.format( # noqa: #501 key1_varname=key1_varname, key2_varname=key2_varname, key3_varname=key3_varname, key4_varname=key4_varname, ) # check correct suite is generated self.assertXmlPartialEqual( icon_mapping_spec, app.create_suite(), './detail[@id="m0_case_short"]/field/template') # check app strings mapped correctly app_strings = commcare_translations.loads(app.create_app_strings('en')) self.assertEqual( app_strings[ 'm0.case_short.case_gender_1.enum.h{key1_varname}'.format( key1_varname=key1_varname, )], 'Boy') self.assertEqual( app_strings[ 'm0.case_short.case_gender_1.enum.h{key2_varname}'.format( key2_varname=key2_varname, )], 'Girl') self.assertEqual( app_strings[ 'm0.case_short.case_gender_1.enum.h{key3_varname}'.format( key3_varname=key3_varname, )], 'Man') self.assertEqual( app_strings[ 'm0.case_short.case_gender_1.enum.h{key4_varname}'.format( key4_varname=key4_varname, )], 'Woman')