Skip to content

Commit

Permalink
Merge pull request #33881 from dimagi/smh/form-model-changes
Browse files Browse the repository at this point in the history
Form model changes for text translations
  • Loading branch information
stephherbers committed Dec 20, 2023
2 parents 917985d + 5fca1e9 commit f3c04ed
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 68 deletions.
4 changes: 4 additions & 0 deletions corehq/apps/app_manager/app_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ def _create_forms_app_strings(
clean_trans(custom_assertion.text, langs)
)

yield id_strings.form_submit_label_locale(form), form.get_submit_label(lang)
if form.get_submit_notification_label(lang):
yield id_strings.form_submit_notification_label_locale(form), form.get_submit_notification_label(lang)


def _create_case_list_form_app_strings(
app,
Expand Down
16 changes: 16 additions & 0 deletions corehq/apps/app_manager/id_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,22 @@ def form_custom_icon_locale(form, icon_form):
)


@pattern('forms.m%df%d.submit_label')
def form_submit_label_locale(form):
return "forms.m{module.id}f{form.id}.submit_label".format(
module=form.get_module(),
form=form
)


@pattern('forms.m%df%d.submit_notification_label')
def form_submit_notification_label_locale(form):
return "forms.m{module.id}f{form.id}.submit_notification_label".format(
module=form.get_module(),
form=form
)


@pattern('case_list_form.m%d.icon')
def case_list_form_icon_locale(module):
return "case_list_form.m{module.id}.icon".format(module=module)
Expand Down
13 changes: 13 additions & 0 deletions corehq/apps/app_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ class FormBase(DocumentSchema):
# computed datums IDs that are allowed in endpoints
function_datum_endpoints = StringListProperty()

submit_label = LabelProperty(default={})
submit_notification_label = LabelProperty(default={})

def __repr__(self):
return f"{self.doc_type}(id='{self.id}', name='{self.default_name()}', unique_id='{self.unique_id}')"

Expand Down Expand Up @@ -1339,6 +1342,16 @@ def get_save_to_case_updates(self):
updates_by_case_type[case_type].update(save_to_case_update.properties)
return updates_by_case_type

def get_submit_label(self, lang):
if lang in self.submit_label:
return self.submit_label[lang]
return 'Submit'

def get_submit_notification_label(self, lang):
if self.submit_notification_label and lang in self.submit_notification_label:
return self.submit_notification_label[lang]
return ''


class IndexedFormBase(FormBase, IndexedSchema, CommentMixin):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ cchq.reports_last_updated_on=Reports last updated on
en=English
es=Español
forms.m0f0=Yes/No
forms.m0f0.submit_label=Submit
forms.m0f1=PIL
forms.m0f1.submit_label=Submit
homescreen.title=Yes / No
lang.current=en
modules.m0=Untitled Module
Expand Down
2 changes: 2 additions & 0 deletions corehq/apps/app_manager/tests/data/yesno_en_app_strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ cchq.report_menu=Reports
cchq.report_name_header=Report Name
cchq.reports_last_updated_on=Reports last updated on
forms.m0f0=Yes/No
forms.m0f0.submit_label=Submit
forms.m0f1=PIL
forms.m0f1.submit_label=Submit
homescreen.title=Yes / No
lang.current=en
modules.m0=Untitled Module
Expand Down
2 changes: 2 additions & 0 deletions corehq/apps/app_manager/tests/data/yesno_es_app_strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ cchq.report_menu=Reports
cchq.report_name_header=Report Name
cchq.reports_last_updated_on=Reports last updated on
forms.m0f0=Yes/No
forms.m0f0.submit_label=Submit
forms.m0f1=PIL
forms.m0f1.submit_label=Submit
homescreen.title=Yes / No
lang.current=es
modules.m0=Untitled Module in Spanish
25 changes: 25 additions & 0 deletions corehq/apps/app_manager/tests/test_app_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,28 @@ def test_no_items_text_app_strings(self):
en_app_strings = self._generate_app_strings(app, 'default', build_profile_id='en')
except AttributeError:
self.fail("_generate_app_strings raised AttributeError unexpectedly")

def test_form_submit_label(self):
factory = AppFactory(build_version='2.40.0')
factory.app.langs = ['en', 'es']
module, form = factory.new_basic_module('my_module', 'cases')
form.submit_label = {
'en': 'Submit Button',
'es': 'Botón de Enviar',
}
form.submit_notification_label = {
'en': 'You submitted the form!',
'es': '¡Enviaste el formulario!',
}
en_strings = self._generate_app_strings(factory.app, 'en')
self.assertEqual(en_strings['forms.m0f0.submit_label'], form.submit_label['en'])
self.assertEqual(en_strings['forms.m0f0.submit_notification_label'], form.submit_notification_label['en'])

es_strings = self._generate_app_strings(factory.app, 'es')
self.assertEqual(es_strings['forms.m0f0.submit_label'], form.submit_label['es'])
self.assertEqual(es_strings['forms.m0f0.submit_notification_label'], form.submit_notification_label['es'])

default_strings = self._generate_app_strings(factory.app, 'default')
self.assertEqual(default_strings['forms.m0f0.submit_label'], form.submit_label['en'])
self.assertEqual(default_strings['forms.m0f0.submit_notification_label'],
form.submit_notification_label['en'])
14 changes: 13 additions & 1 deletion corehq/apps/cloudcare/static/cloudcare/js/form_entry/form_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,23 @@ hqDefine("cloudcare/js/form_entry/form_ui", function () {
return !self.isSubmitting() && self.erroredQuestions().length === 0;
});

self.getSubmitTranslation = function () {
var translations = self.translations;
if (translations) {
const result = Object.entries(translations).find(([k]) => k.includes("submit_label"));
if (result) {
const key = result[0];
return ko.toJS(translations[key]);
}
}
return gettext("Submit");
};

self.submitText = ko.computed(function () {
if (self.isSubmitting()) {
return gettext('Submitting...');
}
return gettext('Submit');
return self.getSubmitTranslation();
});

self.forceRequiredVisible = ko.observable(false);
Expand Down
9 changes: 9 additions & 0 deletions corehq/apps/translations/app_translations/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ def get_form_question_label_name_media(langs, form):
value += part
itext_items[text_id][(lang, value_form)] = value

itext_items['submit_label'] = {}
itext_items['submit_notification_label'] = {}
for lang in langs:
itext_items['submit_label'][(lang, 'default')] = form.get_submit_label(lang)
itext_items['submit_notification_label'][(lang, 'default')] = form.get_submit_notification_label(lang)

app = form.get_app()
for text_id, values in itext_items.items():
row = [text_id]
Expand All @@ -396,5 +402,8 @@ def get_form_question_label_name_media(langs, form):
# Don't add empty rows:
if any(row[1:]):
rows.append(row)
# allow empty for submit_notification_label row
if row[0] == 'submit_notification_label' and not any(row[1:]):
rows.append(row)

return rows
17 changes: 17 additions & 0 deletions corehq/apps/translations/app_translations/upload_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ def update(self, rows):
for row in rows:
if row['label'] in label_ids_to_skip:
continue
if row['label'] == 'submit_label':
try:
self.form.submit_label[lang] = row[self._get_col_key('default', lang)]
except KeyError:
pass
continue
if row['label'] == 'submit_notification_label':
notification_value = ''
try:
notification_value = row[self._get_col_key('default', lang)]
except KeyError:
pass
if notification_value:
self.form.submit_notification_label[lang] = notification_value
continue
try:
self._add_or_remove_translations(lang, row)
except BulkAppTranslationsException as e:
Expand Down Expand Up @@ -156,6 +171,8 @@ def _get_label_ids_to_skip(self, rows):
if not self._has_translation(row):
label_ids_to_skip.add(row['label'])
for label in label_ids_to_skip:
if label == 'submit_notification_label':
continue
self.msgs.append((
messages.error,
_("You must provide at least one translation for the label '{}'.").format(label)))
Expand Down

0 comments on commit f3c04ed

Please sign in to comment.