From da45780b33c689463c54f5597c089f59374f7deb Mon Sep 17 00:00:00 2001 From: Noor Syed Date: Tue, 20 Jun 2023 09:26:30 -0400 Subject: [PATCH] RDISCROWD-6042 backwards compatible turkey spelling (#850) * support alternate turkey spelling remove file fix null error * backwards compatible turkey spelling * fix keyerror * bug fix --- pybossa/cache/task_browse_helpers.py | 3 +- pybossa/util.py | 15 ++++-- test/test_util.py | 68 +++++++++++++++++++++------- 3 files changed, 64 insertions(+), 22 deletions(-) diff --git a/pybossa/cache/task_browse_helpers.py b/pybossa/cache/task_browse_helpers.py index df3ae5c18..d5a7aaad5 100644 --- a/pybossa/cache/task_browse_helpers.py +++ b/pybossa/cache/task_browse_helpers.py @@ -350,9 +350,10 @@ def validate_user_preferences(user_pref): valid_languages = valid_user_preferences.get('languages') valid_locations = valid_user_preferences.get('locations') - # Support old spelling of Türkiye + # Support alternate spellings of Türkiye if valid_locations: valid_locations.append('Turkey') + valid_locations.append('Turkiye') lang = user_pref.get('languages') loc = user_pref.get('locations') diff --git a/pybossa/util.py b/pybossa/util.py index b77294857..bc4a4f574 100644 --- a/pybossa/util.py +++ b/pybossa/util.py @@ -1010,13 +1010,19 @@ def get_user_pref_db_clause(user_pref, user_email=None): # expand user preferences as per sql format for jsonb datatype # single user preference with multiple value or # multiple user preferences with single/multiple values - _valid = ((k, v) for k, v in user_pref.items() if isinstance(v, list)) - user_prefs = [{k: [item]} for k, pref_list in _valid - for item in pref_list] assign_key = 'assign_user' location_key = 'locations' language_key = 'languages' + _valid = ((k, v) for k, v in user_pref.items() if isinstance(v, list)) + turkey_spellings = {'Turkey', 'Türkiye', 'Turkiye'} + if location_key in user_pref and \ + any(turkey_spelling in user_pref[location_key] for turkey_spelling in turkey_spellings): + user_pref[location_key] = list(set(user_pref[location_key]).union(turkey_spellings)) + + user_prefs = [{k: [item]} for k, pref_list in _valid + for item in pref_list] + if not user_prefs: user_pref_sql = '''(task.user_pref IS NULL OR task.user_pref = \'{}\' )''' if user_email: @@ -1024,8 +1030,9 @@ def get_user_pref_db_clause(user_pref, user_email=None): AND task.user_pref->\'{}\' IS NOT NULL AND task.user_pref @> :assign_user) '''.format(location_key, language_key, assign_key) else: - sql = ('task.user_pref @> \'{}\''.format(json.dumps(up).lower()) + sql = ('task.user_pref @> \'{}\''.format(json.dumps(up, ensure_ascii=False).lower()) for up in user_prefs) + user_pref_sql = '''( (task.user_pref-> \'{}\' IS NULL AND task.user_pref-> \'{}\' IS NULL) OR ({}) )'''.format(location_key, language_key, ' OR '.join(sql)) if user_email: email_sql = ''' AND (task.user_pref->\'{}\' IS NULL OR task.user_pref @> :assign_user) diff --git a/test/test_util.py b/test/test_util.py index 76a984226..58c1163d6 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -978,7 +978,7 @@ def test_process_tp_components(self): tp_code = """ - +
@@ -989,11 +989,11 @@ def test_process_tp_components(self):
- + - +
- + - +
- +
- +
- +
- +
- +
- +
- + """ user_response = {"all_info": @@ -1137,13 +1137,13 @@ def test_process_table_component_with_initial_data_from_task(self): - +