Skip to content

Commit

Permalink
Merge branch 'master' of github.com:okfn/ckan into 3028-dashboard-act…
Browse files Browse the repository at this point in the history
…ivity-stream-filtering
  • Loading branch information
Sean Hammond committed Jan 23, 2013
2 parents 0c56ee6 + bc1d99c commit 15c4ec2
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 88 deletions.
124 changes: 52 additions & 72 deletions ckan/lib/helpers.py
Expand Up @@ -38,6 +38,7 @@
import ckan.lib.fanstatic_resources as fanstatic_resources
import ckan.model as model
import ckan.lib.formatters as formatters
import ckan.lib.maintain as maintain
import ckan.lib.datapreview as datapreview

get_available_locales = i18n.get_available_locales
Expand Down Expand Up @@ -294,32 +295,6 @@ def are_there_flash_messages():
return flash.are_there_messages()


def nav_link(text, controller, **kwargs):
'''
params
class_: pass extra class(s) to add to the <a> tag
icon: name of ckan icon to use within the link
condition: if False then no link is returned
'''
kwargs['controller'] = controller
if kwargs.get('inner_span'):
text = literal('<span>') + text + literal('</span>')
icon = kwargs.pop('icon', None)
if icon:
text = literal('<i class="icon-large icon-%s"></i> ' % icon) + text
no_active = kwargs.pop('suppress_active_class', False)
class_ = _link_class(kwargs, no_active)
if kwargs.pop('condition', True):
link = link_to(
text,
url_for(**kwargs),
class_=class_
)
else:
link = ''
return link


def _link_active(kwargs):
''' creates classes for the link_to calls '''
highlight_actions = kwargs.get('highlight_actions',
Expand All @@ -328,50 +303,70 @@ def _link_active(kwargs):
and c.action in highlight_actions)


def _link_class(kwargs, suppress_active_class=False):
''' creates classes for the link_to calls '''
if not suppress_active_class and _link_active(kwargs):
active = ' active'
else:
active = ''
kwargs.pop('highlight_actions', '')
return kwargs.pop('class_', '') + active or None
def _link_to(text, *args, **kwargs):
'''Common link making code for several helper functions'''
assert len(args)<2, 'Too many unnamed arguments'

def _link_class(kwargs):
''' creates classes for the link_to calls '''
suppress_active_class = kwargs.pop('suppress_active_class', False)
if not suppress_active_class and _link_active(kwargs):
active = ' active'
else:
active = ''
kwargs.pop('highlight_actions', '')
return kwargs.pop('class_', '') + active or None

def _create_link_text(text, **kwargs):
''' Update link text to add a icon or span if specified in the
kwargs '''
if kwargs.pop('inner_span', None):
text = literal('<span>') + text + literal('</span>')
icon = kwargs.pop('icon', None)
if icon:
text = literal('<i class="icon-large icon-%s"></i> ' % icon) + text
return text

def nav_named_link(text, name, **kwargs):
class_ = _link_class(kwargs)

icon = kwargs.pop('icon', None)
if icon:
text = literal('<i class="icon-large icon-%s"></i> ' % icon) + text

return link_to(
text,
url_for(name, **kwargs),
_create_link_text(text, **kwargs),
url_for(*args, **kwargs),
class_=class_
)


def nav_link(text, controller, **kwargs):
'''
params
class_: pass extra class(s) to add to the <a> tag
icon: name of ckan icon to use within the link
condition: if False then no link is returned
'''
if kwargs.pop('condition', True):
kwargs['controller'] = controller
link = _link_to(text, **kwargs)
else:
link = ''
return link


def nav_named_link(text, name, **kwargs):
'''Create a link for a named route.'''
return _link_to(text, name, **kwargs)


def subnav_link(text, action, **kwargs):
'''Create a link for a named route.'''
kwargs['action'] = action
class_ = _link_class(kwargs)
return link_to(
text,
url_for(**kwargs),
class_=class_
)
return _link_to(text, **kwargs)


@maintain.deprecated('h.subnav_named_route is deprecated please '
'use h.nav_named_link')
def subnav_named_route(text, routename, **kwargs):
""" Generate a subnav element based on a named route """
# FIXME this is the same as _nav_named_link
# they should be combined
class_ = _link_class(kwargs)
return link_to(
text,
url_for(str(routename), **kwargs),
class_=class_
)
'''Generate a subnav element based on a named route
Deprecated in ckan 2.0 '''
return nav_named_link(text, routename, **kwargs)


def build_nav_main(*args):
Expand Down Expand Up @@ -461,21 +456,6 @@ def default_group_type():
return str(config.get('ckan.default.group_type', 'group'))


_menu_items = {
'add dataset': dict(controller='package', action='new'),
'search': dict(controller='package',
action='search',
highlight_actions='index search'),
'default_group': dict(name='%s_index' % default_group_type(),
controller='group',
highlight_actions='index search'),
'about': dict(controller='home', action='about'),
'login': dict(controller='user', action='login'),
'register': dict(controller='user', action='register'),
'organizations': dict(action='index', controller='organization'),
}


def get_facet_items_dict(facet, limit=10, exclude_active=False):
'''Return the list of unselected facet items for the given facet, sorted
by count.
Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/action/create.py
Expand Up @@ -148,6 +148,7 @@ def package_create(context, data_dict):
model.setup_default_user_roles(pkg, admins)
# Needed to let extensions know the package id
model.Session.flush()
data['id'] = pkg.id

context_org_update = context.copy()
context_org_update['ignore_auth'] = True
Expand All @@ -159,6 +160,8 @@ def package_create(context, data_dict):
for item in plugins.PluginImplementations(plugins.IPackageController):
item.create(pkg)

item.after_create(context, data)

if not context.get('defer_commit'):
model.repo.commit()

Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/action/delete.py
Expand Up @@ -41,6 +41,9 @@ def package_delete(context, data_dict):

for item in plugins.PluginImplementations(plugins.IPackageController):
item.delete(entity)

item.after_delete(context, data_dict)

entity.delete()
model.repo.commit()

Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -736,6 +736,9 @@ def package_show(context, data_dict):
if schema and context.get('validate', True):
package_dict, errors = _validate(package_dict, schema, context=context)

for item in plugins.PluginImplementations(plugins.IPackageController):
item.after_show(context, package_dict)

return package_dict

def resource_show(context, data_dict):
Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -276,6 +276,9 @@ def package_update(context, data_dict):

for item in plugins.PluginImplementations(plugins.IPackageController):
item.edit(pkg)

item.after_update(context, data)

if not context.get('defer_commit'):
model.repo.commit()

Expand Down
9 changes: 8 additions & 1 deletion ckan/logic/auth/update.py
Expand Up @@ -15,9 +15,16 @@ def package_update(context, data_dict):
package = get_package_object(context, data_dict)

if package.owner_org:
# if there is an owner org then we must have update_dataset
# premission for that organization
check1 = new_authz.has_user_permission_for_group_or_org(package.owner_org, user, 'update_dataset')
else:
check1 = new_authz.check_config_permission('create_dataset_if_not_in_organization')
# If dataset is not owned then we can edit if config permissions allow
if new_authz.auth_is_registered_user():
check1 = new_authz.check_config_permission(
'create_dataset_if_not_in_organization')
else:
check1 = new_authz.check_config_permission('anon_create_dataset')
if not check1:
return {'success': False, 'msg': _('User %s not authorized to edit package %s') % (str(user), package.id)}
else:
Expand Down
10 changes: 7 additions & 3 deletions ckan/model/group.py
Expand Up @@ -221,13 +221,17 @@ def packages(self, with_private=False, limit=None,
return query.all()

@classmethod
def search_by_name_or_title(cls, text_query, group_type=None):
def search_by_name_or_title(cls, text_query, group_type=None, is_org=False):
text_query = text_query.strip().lower()
q = meta.Session.query(cls) \
.filter(or_(cls.name.contains(text_query),
cls.title.ilike('%' + text_query + '%')))
if group_type:
q = q.filter(cls.type == group_type)
if is_org:
q = q.filter(cls.type == 'organization')
else:
q = q.filter(cls.type != 'organization')
if group_type:
q = q.filter(cls.type == group_type)
return q.order_by(cls.title)

def add_package_by_name(self, package_name):
Expand Down
32 changes: 32 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -335,6 +335,38 @@ def authz_remove_role(self, object_role):
def delete(self, entity):
pass

def after_create(self, context, pkg_dict):
'''
Extensions will receive the validated data dict after the package
has been created (Note that the create method will return a package
domain object, which may not include all fields). Also the newly
created package id will be added to the dict.
'''
pass

def after_update(self, context, pkg_dict):
'''
Extensions will receive the validated data dict after the package
has been updated (Note that the edit method will return a package
domain object, which may not include all fields).
'''
pass

def after_delete(self, context, pkg_dict):
'''
Extensions will receive the data dict (tipically containing
just the package id) after the package has been deleted.
'''
pass

def after_show(self, context, pkg_dict):
'''
Extensions will receive the validated data dict after the package
is ready for display (Note that the read method will return a
package domain object, which may not include all fields).
'''
pass

def before_search(self, search_params):
'''
Extensions will receive a dictionary with the query parameters,
Expand Down

0 comments on commit 15c4ec2

Please sign in to comment.