Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#943] Add tutorial section on error handling in extensions
- Loading branch information
Sean Hammond
committed
Jun 28, 2013
1 parent
5b7b0b4
commit cccf5d2
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import ckan.plugins as plugins | ||
import ckan.plugins.toolkit as toolkit | ||
|
||
# FIXME: Shouldn't have to import thise here (it's needed for the | ||
# Invalid exception class below). | ||
import ckan.lib.navl.dictization_functions as df | ||
|
||
|
||
def group_create(context, data_dict=None): | ||
|
||
# Get the user name of the logged-in user. | ||
user_name = context['user'] | ||
|
||
# Get a list of the members of the 'curators' group. | ||
try: | ||
members = toolkit.get_action('member_list')( | ||
data_dict={'id': 'curators', 'object_type': 'user'}) | ||
except toolkit.ObjectNotFound: | ||
# The curators group doesn't exist. | ||
return {'success': False, | ||
'msg': "The curators groups doesn't exist, so only sysadmins " | ||
"are authorized to create groups."} | ||
|
||
# 'members' is a list of (user_id, object_type, capacity) tuples, we're | ||
# only interested in the user_ids. | ||
member_ids = [member_tuple[0] for member_tuple in members] | ||
|
||
# FIXME: An extension shouldn't have to do this. | ||
context['session'] = context['model'].Session | ||
|
||
# We have the logged-in user's user name, get their user id. | ||
convert_user_name_or_id_to_id = toolkit.get_converter( | ||
'convert_user_name_or_id_to_id') | ||
try: | ||
user_id = convert_user_name_or_id_to_id(user_name, context) | ||
except df.Invalid: | ||
# The user doesn't exist (e.g. they're not logged-in). | ||
return {'success': False, | ||
'msg': 'You must be logged-in as a member of the curators ' | ||
'group to create new groups.'} | ||
|
||
# Finally, we can test whether the user is a member of the curators group. | ||
if user_id in member_ids: | ||
return {'success': True} | ||
else: | ||
return {'success': False, | ||
'msg': 'Only curators are allowed to create groups'} | ||
|
||
|
||
class ExampleIAuthFunctionsPlugin(plugins.SingletonPlugin): | ||
plugins.implements(plugins.IAuthFunctions, inherit=False) | ||
|
||
def get_auth_functions(self): | ||
return {'group_create': group_create} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters