Skip to content

Commit

Permalink
Don't change user names to ids in user_id_or_name_exists
Browse files Browse the repository at this point in the history
The user_id_or_name_exists() validator was returning the user id if the
given argument was a user name. Return the user name instead. If the
given argument is a user id, still return the user id as before.

Also added a convert_user_id_or_name_to_id() converter for when you
don't want the id returned if a name is given.
  • Loading branch information
Sean Hammond committed Oct 4, 2012
1 parent 60407fb commit 4c0a22e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ckan/logic/converters.py
Expand Up @@ -82,3 +82,27 @@ def callable(key, data, errors, context):
data[key] = tags
return callable

def convert_user_name_or_id_to_id(user_name_or_id, context):
'''Return the user id for the given user name or id.
The point of this function is to convert user names to ids. If you have
something that may be a user name or a user id you can pass it into this
function and get the user id out either way.
Also validates that a user with the given name or id exists.
:returns: the id of the user with the given user name or id
:rtype: string
:raises: ckan.lib.navl.dictization_functions.Invalid if no user can be
found with the given id or user name
'''
session = context['session']
result = session.query(model.User).filter_by(id=user_name_or_id).first()
if not result:
result = session.query(model.User).filter_by(
name=user_name_or_id).first()
if not result:
raise Invalid('%s: %s' % (_('Not found'), _('User')))
else:
return result.id
8 changes: 7 additions & 1 deletion ckan/logic/validators.py
Expand Up @@ -102,6 +102,12 @@ def user_id_exists(user_id, context):
return user_id

def user_id_or_name_exists(user_id_or_name, context):
'''Return the given user_id_or_name if such a user exists.
:raises: ckan.lib.navl.dictization_functions.Invalid if no user can be
found with the given id or user name
'''
model = context['model']
session = context['session']
result = session.query(model.User).get(user_id_or_name)
Expand All @@ -110,7 +116,7 @@ def user_id_or_name_exists(user_id_or_name, context):
result = session.query(model.User).filter_by(name=user_id_or_name).first()
if not result:
raise Invalid('%s: %s' % (_('Not found'), _('User')))
return result.id
return user_id_or_name

def group_id_exists(group_id, context):
"""Raises Invalid if the given group_id does not exist in the model given
Expand Down

0 comments on commit 4c0a22e

Please sign in to comment.