From 72ea41f9c7e9b2dd6b0e8d79d88c4a0d3bc95eb2 Mon Sep 17 00:00:00 2001 From: Andy Armstrong Date: Tue, 8 Sep 2015 18:26:12 -0400 Subject: [PATCH 1/2] Don't localize search indexes for teams TNL-3239 --- lms/djangoapps/teams/search_indexes.py | 15 +++++++++------ lms/djangoapps/teams/tests/test_views.py | 22 +++++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lms/djangoapps/teams/search_indexes.py b/lms/djangoapps/teams/search_indexes.py index 7532b48ac6..cc743401b6 100644 --- a/lms/djangoapps/teams/search_indexes.py +++ b/lms/djangoapps/teams/search_indexes.py @@ -3,6 +3,7 @@ from django.conf import settings from django.db.models.signals import post_save from django.dispatch import receiver +from django.utils import translation from search.search_engine_base import SearchEngine @@ -45,12 +46,14 @@ class CourseTeamIndexer(object): """ Generate the text field used for general search. """ - return "{name}\n{description}\n{country}\n{language}".format( - name=self.course_team.name.encode('utf-8'), - description=self.course_team.description.encode('utf-8'), - country=self.course_team.country.name.format(), - language=self._language_name() - ) + # Always use the English version of any localizable strings (see TNL-3239) + with translation.override('en'): + return "{name}\n{description}\n{country}\n{language}".format( + name=self.course_team.name.encode('utf-8'), + description=self.course_team.description.encode('utf-8'), + country=self.course_team.country.name.format(), + language=self._language_name() + ) def _language_name(self): """ diff --git a/lms/djangoapps/teams/tests/test_views.py b/lms/djangoapps/teams/tests/test_views.py index 3d7490bba9..85a980e630 100644 --- a/lms/djangoapps/teams/tests/test_views.py +++ b/lms/djangoapps/teams/tests/test_views.py @@ -9,6 +9,7 @@ import ddt from django.core.urlresolvers import reverse from django.conf import settings from django.db.models.signals import post_save +from django.utils import translation from nose.plugins.attrib import attr from rest_framework.test import APITestCase, APIClient @@ -519,12 +520,13 @@ class TestListTeamsAPI(EventTestMixin, TeamAPITestCase): ) @ddt.unpack def test_text_search(self, text_search, expected_team_names): - # clear out the teams search index before reindexing - CourseTeamIndexer.engine().destroy() - - for team in self.test_team_name_id_map.values(): - CourseTeamIndexer.index(team) + def reset_search_index(): + """Clear out the search index and reindex the teams.""" + CourseTeamIndexer.engine().destroy() + for team in self.test_team_name_id_map.values(): + CourseTeamIndexer.index(team) + reset_search_index() self.verify_names( {'course_id': self.test_course_2.id, 'text_search': text_search}, 200, @@ -540,6 +542,16 @@ class TestListTeamsAPI(EventTestMixin, TeamAPITestCase): number_of_results=len(expected_team_names) ) + # Verify that the searches still work for a user from a different locale + with translation.override('ar'): + reset_search_index() + self.verify_names( + {'course_id': self.test_course_2.id, 'text_search': text_search}, + 200, + expected_team_names, + user='student_enrolled_public_profile' + ) + @ddt.ddt class TestCreateTeamAPI(EventTestMixin, TeamAPITestCase): From 321a0ea6a0d20e68c872e179cf83a1b1da96331e Mon Sep 17 00:00:00 2001 From: Andy Armstrong Date: Wed, 9 Sep 2015 14:16:05 -0400 Subject: [PATCH 2/2] Fix searching with Chinese characters TNL-3228 --- lms/djangoapps/teams/search_indexes.py | 6 +++--- lms/djangoapps/teams/tests/test_views.py | 12 +++++++++++- lms/djangoapps/teams/views.py | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/teams/search_indexes.py b/lms/djangoapps/teams/search_indexes.py index cc743401b6..9299f31d1b 100644 --- a/lms/djangoapps/teams/search_indexes.py +++ b/lms/djangoapps/teams/search_indexes.py @@ -48,9 +48,9 @@ class CourseTeamIndexer(object): """ # Always use the English version of any localizable strings (see TNL-3239) with translation.override('en'): - return "{name}\n{description}\n{country}\n{language}".format( - name=self.course_team.name.encode('utf-8'), - description=self.course_team.description.encode('utf-8'), + return u"{name}\n{description}\n{country}\n{language}".format( + name=self.course_team.name, + description=self.course_team.description, country=self.course_team.country.name.format(), language=self._language_name() ) diff --git a/lms/djangoapps/teams/tests/test_views.py b/lms/djangoapps/teams/tests/test_views.py index 85a980e630..8c0e0cd3e1 100644 --- a/lms/djangoapps/teams/tests/test_views.py +++ b/lms/djangoapps/teams/tests/test_views.py @@ -224,6 +224,14 @@ class TeamAPITestCase(APITestCase, SharedModuleStoreTestCase): course_id=self.test_course_2.id, topic_id='topic_7' ) + self.chinese_team = CourseTeamFactory.create( + name=u'著文企臺個', + description=u'共樣地面較,件展冷不護者這與民教過住意,國制銀產物助音是勢一友', + country='CN', + language='zh_HANS', + course_id=self.test_course_2.id, + topic_id='topic_7' + ) self.test_team_name_id_map = {team.name: team for team in ( self.solar_team, @@ -232,6 +240,7 @@ class TeamAPITestCase(APITestCase, SharedModuleStoreTestCase): self.another_team, self.public_profile_team, self.search_team, + self.chinese_team, )} for user, course in [('staff', self.test_course_1), ('course_staff', self.test_course_1)]: @@ -438,7 +447,7 @@ class TestListTeamsAPI(EventTestMixin, TeamAPITestCase): self.verify_names( {'course_id': self.test_course_2.id}, 200, - ['Another Team', 'Public Profile Team', 'Search'], + ['Another Team', 'Public Profile Team', 'Search', u'著文企臺個'], user='staff' ) @@ -517,6 +526,7 @@ class TestListTeamsAPI(EventTestMixin, TeamAPITestCase): ('Island', ['Search']), ('not-a-query', []), ('team', ['Another Team', 'Public Profile Team']), + (u'著文企臺個', [u'著文企臺個']), ) @ddt.unpack def test_text_search(self, text_search, expected_team_names): diff --git a/lms/djangoapps/teams/views.py b/lms/djangoapps/teams/views.py index 9a88a2526c..cdf20060a9 100644 --- a/lms/djangoapps/teams/views.py +++ b/lms/djangoapps/teams/views.py @@ -333,7 +333,7 @@ class TeamsListView(ExpandableFieldViewMixin, GenericAPIView): result_filter.update({'course_id': course_id_string}) search_results = search_engine.search( - query_string=text_search.encode('utf-8'), + query_string=text_search, field_dictionary=result_filter, size=MAXIMUM_SEARCH_SIZE, )