Skip to content

Commit

Permalink
Merge pull request #35049 from dimagi/jt/use-locations-display-name
Browse files Browse the repository at this point in the history
Use Locations Display Name
  • Loading branch information
Jtang-1 authored Aug 28, 2024
2 parents 15f9eb1 + 173730a commit 705f87e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
7 changes: 5 additions & 2 deletions corehq/apps/locations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from dimagi.utils.couch.database import iter_docs

from corehq import toggles
from corehq.feature_previews import USE_LOCATION_DISPLAY_NAME
from corehq.apps.custom_data_fields.edit_entity import (
CUSTOM_DATA_FIELD_PREFIX,
CustomDataEditor,
Expand All @@ -32,6 +33,7 @@
from corehq.apps.users.models import CommCareUser
from corehq.apps.users.util import user_display_string
from corehq.util.quickcache import quickcache
from corehq.util.global_request import get_request_domain

from .models import (
LocationFixtureConfiguration,
Expand Down Expand Up @@ -62,10 +64,11 @@ def render(self, name, value, attrs=None, renderer=None):
location_ids = to_list(value) if value else []
locations = list(SQLLocation.active_objects
.filter(domain=self.domain, location_id__in=location_ids))

use_location_display_name = USE_LOCATION_DISPLAY_NAME.enabled(get_request_domain())
initial_data = [{
'id': loc.location_id,
'text': loc.get_path_display(),
'text': loc.display_name if use_location_display_name else loc.get_path_display(),
'title': loc.get_path_display() if use_location_display_name else loc.display_name,
} for loc in locations]

return get_template(self.template).render({
Expand Down
12 changes: 9 additions & 3 deletions corehq/apps/locations/static/locations/js/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ hqDefine("locations/js/widgets", [
_.each($source.select2('data'), function (result) {
const fullLengthName = result.text || result.name;
const truncatedName = truncateLocationName(fullLengthName, $select);
$select.append(new Option(truncatedName, result.id));
var $option = new Option(truncatedName, result.id);
$option.setAttribute('title', result.title);
$select.append($option);
});
}

Expand Down Expand Up @@ -124,7 +126,9 @@ hqDefine("locations/js/widgets", [
initial = [initial];
}
_.each(initial, function (result) {
$select.append(new Option(result.text, result.id));
var $option = new Option(result.text, result.id);
$option.setAttribute('title', result.title);
$select.append($option);
});
$select.val(_.pluck(initial, 'id')).trigger('change');
}
Expand All @@ -151,7 +155,9 @@ hqDefine("locations/js/widgets", [
// This custom event is fired in autocomplete_select_widget.html
if ($source.hasClass("select2-hidden-accessible")) {
updateSelect2($source, $select);
$select.append(new Option(value.text, value.id));
var $option = new Option(value.text, value.id);
$option.setAttribute('title', value.title);
$select.append($option);
$select.val(value.id).trigger("change");
} else {
$source.on('select-ready', function () {
Expand Down
3 changes: 2 additions & 1 deletion corehq/apps/reports/filters/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def get_options(self, show_more=False):
)
results = [
{'id': entry[0], 'text': entry[1]} if len(entry) == 2 else
{'id': entry[0], 'text': entry[1], 'is_active': entry[2]} for entry
{'id': entry[0], 'text': entry[1], 'is_active': entry[2]} if len(entry) == 3 else
{'id': entry[0], 'text': entry[1], 'is_active': entry[2], 'title': entry[3]} for entry
in options
]

Expand Down
12 changes: 8 additions & 4 deletions corehq/apps/reports/filters/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from memoized import memoized

from corehq import toggles
from corehq.feature_previews import USE_LOCATION_DISPLAY_NAME
from corehq.apps.domain.models import Domain
from corehq.apps.enterprise.models import EnterprisePermissions
from corehq.apps.es import filters
Expand All @@ -20,6 +21,7 @@
from corehq.apps.users.models import CommCareUser, UserHistory, WebUser
from corehq.apps.users.util import cached_user_id_to_user_display
from corehq.const import USER_DATETIME_FORMAT
from corehq.util.global_request import get_request_domain
from corehq.util.timezones.conversions import ServerTime
from corehq.util.timezones.utils import get_timezone_for_user

Expand Down Expand Up @@ -138,11 +140,13 @@ def user_type_tuple(self, t):

def location_tuple(self, location):
location_id = location.location_id
text = location.get_path_display()
use_location_display_name = USE_LOCATION_DISPLAY_NAME.enabled(get_request_domain())
text = location.display_name if use_location_display_name else location.get_path_display()
tooltip = location.get_path_display() if use_location_display_name else location.display_name
if self.namespace_locations:
location_id = f'l__{location_id}'
text = f'{text} [location]'
return (location_id, text)
return (location_id, text, None, tooltip)

@property
@memoized
Expand Down Expand Up @@ -280,8 +284,8 @@ def get_default_selections(self):
def selected(self):
selected_ids = self.request.GET.getlist(self.slug)
if not selected_ids:
return [{'id': url_id, 'text': text}
for url_id, text in self.get_default_selections()]
return [{'id': selection_tuple[0], 'text': selection_tuple[1]}
for selection_tuple in self.get_default_selections()]

selected = (self.selected_static_options(selected_ids)
+ self._selected_user_entries(selected_ids)
Expand Down
34 changes: 32 additions & 2 deletions corehq/apps/reports/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from corehq.apps.domain.utils import clear_domain_names
from corehq.apps.es.tests.utils import es_test
from corehq.apps.es.users import user_adapter
from corehq.apps.locations.models import LocationType
from corehq.apps.locations.models import LocationType, SQLLocation
from corehq.apps.locations.tests.util import make_loc
from corehq.apps.reports.filters.case_list import CaseListFilter
from corehq.apps.reports.filters.controllers import paginate_options
Expand Down Expand Up @@ -165,7 +165,7 @@ def setUp(self):
self.user_assigned_locations = [
make_loc('root', domain=self.domain.name, type=self.location_type.code).sql_location
]
self.request = RequestFactory()
self.request = RequestFactory().get('/a/{self.domain}/')
self.request.couch_user = WebUser()
self.request.domain = self.domain

Expand All @@ -189,6 +189,36 @@ def test_default_selections_for_restricted_access(self, assigned_locations_patch
emwf.get_default_selections()
assert assigned_locations_patch.called

@patch('corehq.apps.users.models.WebUser.get_sql_locations')
def test_selections_for_restricted_access(self, assigned_locations_patch):
self.request.can_access_all_locations = False
self.request.project = self.domain
emwf = ExpandedMobileWorkerFilter(self.request)
usa = SQLLocation(
domain=self.domain.name,
name='The United States of America',
site_code='usa',
location_type=self.location_type,
location_id='1',
)
usa.save()
india = SQLLocation(
domain=self.domain.name,
name='India',
site_code='in',
location_type=self.location_type,
location_id='2',
)
india.save()
assigned_locations_patch.return_value = [usa, india]
self.assertEqual(
emwf.selected,
[
{'id': 'l__1', 'text': 'The United States of America [location]'},
{'id': 'l__2', 'text': 'India [location]'}
]
)


class TestCaseListFilter(TestCase):
def setUp(self):
Expand Down
6 changes: 5 additions & 1 deletion corehq/apps/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@
from corehq.apps.user_importer.helpers import UserChangeLogger
from corehq.const import LOADTEST_HARD_LIMIT, USER_CHANGE_VIA_WEB
from corehq.pillows.utils import MOBILE_USER_TYPE, WEB_USER_TYPE
from corehq.feature_previews import USE_LOCATION_DISPLAY_NAME
from corehq.toggles import (
TWO_STAGE_USER_PROVISIONING,
TWO_STAGE_USER_PROVISIONING_BY_SMS,
)
from corehq.util.global_request import get_request_domain

from ..hqwebapp.signals import clear_login_attempts
from .audit.change_messages import UserChangeMessage
Expand Down Expand Up @@ -1124,9 +1126,11 @@ def render(self, name, value, attrs=None, renderer=None):
if value:
try:
loc = SQLLocation.objects.get(location_id=value)
use_location_display_name = USE_LOCATION_DISPLAY_NAME.enabled(get_request_domain())
initial_data = {
'id': loc.location_id,
'text': loc.get_path_display(),
'text': loc.display_name if use_location_display_name else loc.get_path_display(),
'title': loc.get_path_display() if use_location_display_name else loc.display_name,
}
except SQLLocation.DoesNotExist:
pass
Expand Down
9 changes: 9 additions & 0 deletions corehq/feature_previews.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ def previews_enabled_for_domain(domain):
)
)

USE_LOCATION_DISPLAY_NAME = FeaturePreview(
slug='use_location_display_name',
label=_('Use location name'),
description=_(
"This setting changes the location dropdown to display location name instead of "
"the full location path."
)
)


def enable_callcenter(domain_name, checked):
from corehq.apps.domain.models import Domain
Expand Down

0 comments on commit 705f87e

Please sign in to comment.