Skip to content

Commit

Permalink
Added support for specifying the sort direction
Browse files Browse the repository at this point in the history
Previously we only allowed the sort direction to be asc for groups names
and desc for packages (package counts).  Now users can specify a sort_direction
to have a little more control.
  • Loading branch information
rossjones authored and tobes committed Jul 25, 2012
1 parent 40b6cca commit 37e0261
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
16 changes: 7 additions & 9 deletions ckan/logic/action/get.py
Expand Up @@ -287,6 +287,10 @@ def group_list(context, data_dict):
raise logic.ParameterError('"order_by" value %r not implemented.' % order_by)
all_fields = data_dict.get('all_fields',None)

# We want the default for packages to be highest first
default_order_dir = ("asc", "desc")[order_by == 'packages']
order_direction = data_dict.get('order_direction', default_order_dir)

_check_access('group_list', context, data_dict)

query = model.Session.query(model.Group).join(model.GroupRevision)
Expand All @@ -295,16 +299,10 @@ def group_list(context, data_dict):
if groups:
query = query.filter(model.GroupRevision.name.in_(groups))

if order_by == 'name':
sort_by, reverse = 'name', False

groups = query.all()

if order_by == 'packages':
sort_by, reverse = 'packages', True

group_list = model_dictize.group_list_dictize(groups, context,
lambda x:x[sort_by], reverse)
lambda x:x[order_by],
order_direction == 'desc')

if not all_fields:
group_list = [group[ref_group_by] for group in group_list]
Expand Down Expand Up @@ -1009,7 +1007,7 @@ def package_search(context, data_dict):
returned datasets should begin.
:type start: int
:param qf: the dismax query fields to search within, including boosts. See
the `Solr Dismax Documentation
the `Solr Dismax Documentation
<http://wiki.apache.org/solr/DisMaxQParserPlugin#qf_.28Query_Fields.29>`_
for further details.
:type qf: string
Expand Down
57 changes: 56 additions & 1 deletion ckan/tests/functional/test_group.py
Expand Up @@ -7,7 +7,7 @@
from ckan import plugins
import ckan.model as model
from ckan.lib.create_test_data import CreateTestData
from ckan.logic import check_access, NotAuthorized
from ckan.logic import check_access, NotAuthorized, get_action

from pylons import config

Expand Down Expand Up @@ -99,6 +99,61 @@ def test_children(self):
parent = model.Group.by_name("parent_group")
assert_equal(len(parent.get_children_groups()), 0)

def test_sorting(self):
model.repo.rebuild_db()

pkg1 = model.Package(name="pkg1")
pkg2 = model.Package(name="pkg2")
model.Session.add(pkg1)
model.Session.add(pkg2)

CreateTestData.create_groups([{'name': "alpha", 'packages': []},
{'name': "beta",
'packages': ["pkg1", "pkg2"]},
{'name': "delta",
'packages': ["pkg1"]},
{'name': "gamma", 'packages': []}],
admin_user_name='russianfan')

context = {'model': model, 'session': model.Session,
'user': 'russianfan', 'for_view': True,
'with_private': False}
data_dict = {'all_fields': True}
results = get_action('group_list')(context, data_dict)
assert results[0]['name'] == u'alpha', results[0]['name']
assert results[-1]['name'] == u'gamma', results[-1]['name']

# Test name reverse
data_dict = {'all_fields': True, 'order_by': 'name', 'order_direction': 'desc'}
results = get_action('group_list')(context, data_dict)
assert results[0]['name'] == u'gamma', results[0]['name']
assert results[-1]['name'] == u'alpha', results[-1]['name']

# Test default (name) reverse
data_dict = {'all_fields': True, 'order_direction': 'desc'}
results = get_action('group_list')(context, data_dict)
assert results[0]['name'] == u'gamma', results[0]['name']
assert results[-1]['name'] == u'alpha', results[-1]['name']

# Test packages reversed
data_dict = {'all_fields': True, 'order_by': 'packages', 'order_direction': 'desc'}
results = get_action('group_list')(context, data_dict)
assert results[0]['name'] == u'beta', results[0]['name']
assert results[1]['name'] == u'delta', results[1]['name']

# Test packages forward
data_dict = {'all_fields': True, 'order_by': 'packages', 'order_direction': 'asc'}
results = get_action('group_list')(context, data_dict)
assert results[-2]['name'] == u'delta', results[-2]['name']
assert results[-1]['name'] == u'beta', results[-1]['name']

# Default ordering for packages
data_dict = {'all_fields': True, 'order_by': 'packages'}
results = get_action('group_list')(context, data_dict)
assert results[0]['name'] == u'beta', results[0]['name']
assert results[1]['name'] == u'delta', results[1]['name']


def test_mainmenu(self):
# the home page does a package search so have to skip this test if
# search is not supported
Expand Down

0 comments on commit 37e0261

Please sign in to comment.