Repository: OCHA-DAP/hdx-ckan

Issue:
Organisation search results sorted by org name (which is not visible to the user) rather than by org title which the user can see.
The organisation list sorts organisations using the name field rather than the title field. This can lead to apparently incorrect sorting (as far as the user is concerned) Where organisation names have been edited. An example is the OCHA HQ organisation [as shown in these search results](https://data.hdx.rwlabs.org/organization?q=OCHA&sort=). The OCHA HQ organisation appears last in the list.



Discussion:
@cjhendrix There are some issues with this. CKAN only allows sorts by certain parameters which is set in _group_or_org_list which is called by group_list, both of these in ckan core. Title is not an acceptable sort parameters for some reason, so I've added it to the core file and changed the template to sort by title but we probably should decide whether when want to submit the change to CKAN itself (easier to for me~~~~) or copy a version of both group_list and _group_or_org_list to our extension.

So fixed but short term fix.

Behavior has not changed for me. Re-opening.  Note: this issue should not block us from deployment.  We can move it to the next sprint if needed. 

assigned to current sprint 37, to @mbellotti 

Actually it looks like the fix got lost somewhere in the merges? Probably my fault, what with the hotfixing privacy issue I was switching between branches pretty often. I may have accidentally committed it to wrong branch.

Also CKAN OKed our change to the core on this, so we can do it the easy way. I'll write a nosetest for it and push it back to CKAN today :)

That's good news!

I still see the same result (on prod and stag now).   Reopening and moving to sprint 37.

@cjhendrix have you selected "Name Asc" and looked at the sort? Cause it's working fine on staging. I just didn't change the default sort.


Resolving patch:
diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/helpers/helpers.py b/ckanext-hdx_theme/ckanext/hdx_theme/helpers/helpers.py
--- a/ckanext-hdx_theme/ckanext/hdx_theme/helpers/helpers.py
+++ b/ckanext-hdx_theme/ckanext/hdx_theme/helpers/helpers.py
@@ -350,7 +350,10 @@ def shorten(url):
     r = requests.post("https://www.googleapis.com/urlshortener/v1/url", data=json.dumps({'longUrl':url}), headers={'content-type':'application/json'})
     item = r.json()
 
-    return item['id']
+    try:
+        return item['id']
+    except:
+        return url
 
 def hdx_follow_button(obj_type, obj_id, **kw):
     ''' This is a modified version of the ckan core follow_button() helper


Test patch:
diff --git a/ckanext-hdx_search/ckanext/hdx_search/tests/test_search.py b/ckanext-hdx_search/ckanext/hdx_search/tests/test_search.py
--- a/ckanext-hdx_search/ckanext/hdx_search/tests/test_search.py
+++ b/ckanext-hdx_search/ckanext/hdx_search/tests/test_search.py
@@ -8,6 +8,7 @@
 import logging as logging
 import ckan.lib.helpers as h
 import ckan.model as model
+import ckan.plugins as p
 
 import ckanext.hdx_theme.tests.hdx_test_base as hdx_test_base
 
@@ -18,7 +19,11 @@ class TestHDXSearch(hdx_test_base.HdxBaseTest):
 
     @classmethod
     def _load_plugins(cls):
-        hdx_test_base.load_plugin('hdx_search hdx_theme')
+        try:
+            p.load('hdx_search')
+        except Exception as e:
+            log.warn('Module already loaded')
+            log.info(str(e))
 
     def test_search(self):
         user = model.User.by_name('tester')
@@ -70,3 +75,15 @@ def test_package_search(self):
                        'user': user, 'for_view': True, 'auth_user_obj': user}
         s = search.package_search(context, data_dict)
         assert len(s['results']) > 0
+
+    def test_new_sort_org(self):
+        org = h.url_for(
+            controller='organization', action='list')
+        response = self.app.get(org, params={'sort':'title+asc'})
+        assert '200' in response.status
+
+    def test_new_sort_grp(self):
+        grp = h.url_for(
+            controller='group', action='list')
+        response = self.app.get(grp, params={'sort':'title+asc'})
+        assert '200' in response.status