Skip to content

Commit

Permalink
fix order_by param in user_list action
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Apr 14, 2020
1 parent c03ab1e commit 739e690
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
29 changes: 23 additions & 6 deletions ckan/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,14 +747,31 @@ def user_list(context, data_dict):
if email:
query = query.filter_by(email=email)

order_by_field = None
if order_by == 'edits':
raise ValidationError('order_by=edits is no longer supported')
query = query.order_by(
_case([(
_or_(model.User.fullname == None,
model.User.fullname == ''),
model.User.name)],
else_=model.User.fullname))
elif order_by == 'number_created_packages':
order_by_field = order_by
elif order_by != 'name':
try:
order_by_field = getattr(model.User, order_by)
except AttributeError:
pass
if order_by == 'name' or order_by_field is None:
query = query.order_by(
_case(
[(
_or_(
model.User.fullname == None,
model.User.fullname == ''
),
model.User.name
)],
else_=model.User.fullname
)
)
else:
query = query.order_by(order_by_field)

# Filter deleted users
query = query.filter(model.User.state != model.State.DELETED)
Expand Down
81 changes: 81 additions & 0 deletions ckan/tests/logic/action/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,87 @@ def test_user_list_filtered_by_email(self):
got_user = got_users[0]
assert got_user == user_a["name"]

def test_user_list_order_by_default(self):
default_user = helpers.call_action('get_site_user', ignore_auth=True)

users = [
factories.User(fullname="Xander Bird", name="bird_x"),
factories.User(fullname="Max Hankins", name="hankins_m"),
factories.User(fullname="", name="morgan_w"),
factories.User(fullname="Kathy Tillman", name="tillman_k"),
]
expected_names = [
u['name'] for u in [
users[3], # Kathy Tillman
users[1], # Max Hankins
users[2], # morgan_w
users[0], # Xander Bird
]
]

got_users = helpers.call_action('user_list')
got_names = [
u['name'] for u in got_users if u['name'] != default_user['name']
]

assert got_names == expected_names

def test_user_list_order_by_fullname_only(self):
default_user = helpers.call_action('get_site_user', ignore_auth=True)

users = [
factories.User(fullname="Xander Bird", name="bird_x"),
factories.User(fullname="Max Hankins", name="hankins_m"),
factories.User(fullname="", name="morgan_w"),
factories.User(fullname="Kathy Tillman", name="tillman_k"),
]
expected_fullnames = sorted([u['fullname'] for u in users])

got_users = helpers.call_action('user_list', order_by='fullname')
got_fullnames = [
u['fullname'] for u in got_users if u['name'] != default_user['name']
]

assert got_fullnames == expected_fullnames

def test_user_list_order_by_created_datasets(self):
default_user = helpers.call_action('get_site_user', ignore_auth=True)

users = [
factories.User(fullname="Xander Bird", name="bird_x"),
factories.User(fullname="Max Hankins", name="hankins_m"),
factories.User(fullname="Kathy Tillman", name="tillman_k"),
]
datasets = [
factories.Dataset(user=users[1]),
factories.Dataset(user=users[1])
]
for dataset in datasets:
dataset["title"] = "Edited title"
helpers.call_action(
'package_update', context={'user': users[1]['name']}, **dataset
)
expected_names = [
u['name'] for u in [
users[0], # 0 packages created
users[2], # 0 packages created
users[1], # 2 packages created
]
]

got_users = helpers.call_action(
'user_list', order_by='number_created_packages'
)
got_names = [
u['name'] for u in got_users if u['name'] != default_user['name']
]

assert got_names == expected_names

def test_user_list_order_by_edits(self):
with pytest.raises(logic.ValidationError):
helpers.call_action('user_list', order_by='edits')


@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestUserShow(object):
Expand Down

0 comments on commit 739e690

Please sign in to comment.