Skip to content

Commit

Permalink
Merge pull request #35061 from dimagi/dm/case-import-defaults
Browse files Browse the repository at this point in the history
Improve case import UX
  • Loading branch information
millerdev authored Sep 11, 2024
2 parents c045bba + 2c90bb9 commit b6f62ef
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ hqDefine('case_importer/js/excel_fields', [
_,
levenshtein
) {
function excelFieldRows(excelFields, caseFieldSpecs) {
function excelFieldRows(excelFields, caseFieldSpecs, systemFields) {
systemFields = _(systemFields);
var self = {
excelFields: excelFields,
caseFieldSpecs: caseFieldSpecs,
Expand All @@ -30,6 +31,9 @@ hqDefine('case_importer/js/excel_fields', [
customCaseField: ko.observable(excelField),
isCustom: ko.observable(false),
};
row.hasValue = ko.computed(function () {
return row.isCustom() || row.selectedCaseField();
});
row.selectedCaseFieldOrBlank = ko.computed({
read: function () {
return row.isCustom() ? '' : row.selectedCaseField();
Expand Down Expand Up @@ -63,12 +67,6 @@ hqDefine('case_importer/js/excel_fields', [
row.valuesHints = ko.computed(function () {
return row.caseFieldSpec().values_hints || [];
});
row.createNewChecked = ko.computed({
read: function () {
return _.isEmpty(row.caseFieldSpec());
},
write: row.isCustom,
});
row.isDeprecated = ko.computed(function () {
return row.caseFieldSpec().deprecated === true;
});
Expand All @@ -81,7 +79,7 @@ hqDefine('case_importer/js/excel_fields', [
row.isCustom(false);
row.selectedCaseField(field);
} else {
row.isCustom(true);
row.isCustom(!systemFields.contains(field));
row.selectedCaseField(null);
}
};
Expand All @@ -102,6 +100,9 @@ hqDefine('case_importer/js/excel_fields', [
});
return _.chain(suggestions).sortBy('distance').pluck('field').value();
});
row.isSystemProperty = ko.computed(function () {
return row.isCustom() && systemFields.contains(row.customCaseField());
});

self.mappingRows.push(row);
};
Expand Down
3 changes: 2 additions & 1 deletion corehq/apps/case_importer/static/case_importer/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ hqDefine("case_importer/js/main", [
var behaviorForExcelMappingPage = function () {
var excelFields = initialPageData.get('excel_fields');
var caseFieldSpecs = initialPageData.get('case_field_specs');
var systemFields = initialPageData.get('system_fields');
if (!excelFields && !caseFieldSpecs) {
// We're not on the excel mapping page
return;
Expand All @@ -78,7 +79,7 @@ hqDefine("case_importer/js/main", [
return;
}

var excelFieldRows = excelFieldsModule.excelFieldRowsModel(excelFields, caseFieldSpecs);
var excelFieldRows = excelFieldsModule.excelFieldRowsModel(excelFields, caseFieldSpecs, systemFields);
$('#excel-field-rows').koApplyBindings(excelFieldRows);

$('#js-add-mapping').click(function (e) {
Expand Down
6 changes: 6 additions & 0 deletions corehq/apps/case_importer/suggested_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from corehq.apps.case_importer.util import RESERVED_FIELDS
from corehq.apps.data_dictionary.util import get_values_hints_dict, get_deprecated_fields
from corehq.apps.export.system_properties import MAIN_CASE_TABLE_PROPERTIES
from corehq.toggles import BULK_UPLOAD_DATE_OPENED


Expand Down Expand Up @@ -115,3 +116,8 @@ def get_special_fields(domain=None):
)
)
return special_fields


def get_non_discoverable_system_properties():
discoverable = {s.field for s in get_special_fields() if s.description and s.discoverable}
return [p.label for p in MAIN_CASE_TABLE_PROPERTIES if p.label not in discoverable]
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<div class="col-md-6">
<select class="form-control hqwebapp-select2" name="search_column" id="search_column" {% if is_bulk_import %}disabled="disabled"{% endif %}>
{% for column in columns %}
<option value="{{column|escape}}">
<option value="{{column|escape}}" {% if column == "caseid" %}selected{% endif %}>
{{column|escape}}
</option>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

{% initial_page_data 'excel_fields' excel_fields %}
{% initial_page_data 'case_field_specs' case_field_specs %}
{% initial_page_data 'system_fields' system_fields %}
{% initial_page_data 'is_bulk_import' is_bulk_import %}

<form action="{% url "excel_commit" domain %}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</td>

<td class="text-center">
<i class="fa fa-arrow-right"></i>
<!-- ko if: hasValue --><i class="fa fa-arrow-right"></i><!--/ko-->
</td>

<td class="col-md-3">
Expand Down Expand Up @@ -58,17 +58,27 @@
</p>
</div>
<!--ko if: caseFieldSuggestions().length-->
<p class="help-block text-warning">
<p class="help-block text-info">
<i class="fa fa-question-circle"></i>
{% blocktrans with '<!--ko foreach: caseFieldSuggestions--><!--ko if: $index() !== 0-->, <!--/ko--><strong data-bind="text: $data"></strong><!--/ko-->' as suggestion %}
Did you mean "{{ suggestion }}" instead?
{% endblocktrans %}
</p>
<!--/ko-->
<!--ko if: isSystemProperty-->
<p class="help-block text-warning">
<i class="fa fa-question-circle"></i>
{% blocktrans %}
Importing a system property may have unexpected results such as
adding a duplicate field to case exports. Consider using a
different property name.
{% endblocktrans %}
</p>
<!--/ko-->
</td>

<td>
<input type="checkbox" class="form-check-input mt-2" class="new_property" data-bind="checked: createNewChecked"/>
<input type="checkbox" class="form-check-input mt-2" class="new_property" data-bind="checked: isCustom"/>
</td>

{% if request|toggle_enabled:"CASE_IMPORT_DATA_DICTIONARY_VALIDATION" %}
Expand Down
24 changes: 24 additions & 0 deletions corehq/apps/case_importer/tests/test_suggested_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from corehq.apps.case_importer.suggested_fields import (
FieldSpec,
get_suggested_case_fields,
get_non_discoverable_system_properties,
)
from corehq.util.test_utils import DocTestMixin

Expand Down Expand Up @@ -68,3 +69,26 @@ def test_basic_inclusion_and_sorting(self):
FieldSpec(field='weight', show_in_menu=True),
]
)


class TestSystemProperties(SimpleTestCase):

def test_get_non_discoverable_system_properties(self):
self.assertEqual(get_non_discoverable_system_properties(), [
'number',
'caseid',
'case_type',
'closed',
'closed_by_user_id',
'closed_by_username',
'closed_date',
'last_modified_by_user_id',
'last_modified_by_user_username',
'last_modified_date',
'opened_by_user_id',
'opened_by_username',
'opened_date',
'server_last_modified_date',
'state',
'case_link',
])
2 changes: 2 additions & 0 deletions corehq/apps/case_importer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from corehq.apps.case_importer.suggested_fields import (
get_suggested_case_fields,
get_non_discoverable_system_properties,
)
from corehq.apps.case_importer.tracking.case_upload_tracker import CaseUpload
from corehq.apps.case_importer.util import (
Expand Down Expand Up @@ -372,6 +373,7 @@ def excel_fields(request, domain):
'columns': columns,
'excel_fields': excel_fields,
'case_field_specs': case_field_specs,
'system_fields': get_non_discoverable_system_properties(),
'domain': domain,
'mirroring_enabled': mirroring_enabled,
'is_bulk_import': request.POST.get('is_bulk_import', 'False') == 'True',
Expand Down

0 comments on commit b6f62ef

Please sign in to comment.