Skip to content

Commit

Permalink
Re-index datasets belonging to group upon group name changing.
Browse files Browse the repository at this point in the history
Previously, this only occurred when the name was changed through the WUI.
This method works through the synchronous_search plugin.
  • Loading branch information
icmurray committed Oct 4, 2012
1 parent bd6a674 commit 86eb93c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
14 changes: 0 additions & 14 deletions ckan/controllers/group.py
Expand Up @@ -309,17 +309,6 @@ def _save_new(self, context, group_type=None):
error_summary = e.error_summary
return self.new(data_dict, errors, error_summary)

def _force_reindex(self,grp):
""" When the group name has changed, we need to force a reindex
of the datasets within the group, otherwise they will stop
appearing on the read page for the group (as they're connected via
the group name)"""
from ckan.lib.search import rebuild

group = model.Group.get(grp['name'])
for dataset in group.active_packages().all():
rebuild( dataset.name )

def _save_edit(self, id, context):
try:
data_dict = clean_dict(unflatten(
Expand All @@ -328,9 +317,6 @@ def _save_edit(self, id, context):
data_dict['id'] = id
group = get_action('group_update')(context, data_dict)

if id != group["name"]:
self._force_reindex(group)

h.redirect_to('%s_read' % str(group['type']), id=group['name'])
except NotAuthorized:
abort(401, _('Unauthorized to read group %s') % id)
Expand Down
13 changes: 13 additions & 0 deletions ckan/lib/search/__init__.py
Expand Up @@ -105,6 +105,19 @@ class SynchronousSearchPlugin(SingletonPlugin):
implements(IDomainObjectModification, inherit=True)

def notify(self, entity, operation):

if (isinstance(entity, model.Group) and
operation == DomainObjectOperation.changed):
if 'name' in entity.diff():
for dataset in entity.active_packages().all():
dispatch_by_operation(
dataset.__class__.__name__,
get_action('package_show')(
{'model': model, 'ignore_auth': True, 'validate': False},
{'id': dataset.id}),
operation
)

if not isinstance(entity, model.Package):
return
if operation != DomainObjectOperation.deleted:
Expand Down
7 changes: 4 additions & 3 deletions ckan/model/modification.py
Expand Up @@ -11,6 +11,7 @@
from ckan.model.extension import ObserverNotifier
from ckan.model.domain_object import DomainObjectOperation

from ckan.model.group import Group
from ckan.model.package import Package
from ckan.model.resource import ResourceGroup, Resource
from ckan.model.package_extra import PackageExtra
Expand Down Expand Up @@ -41,13 +42,13 @@ def before_commit(self, session):
deleted = obj_cache['deleted']

for obj in set(new):
if isinstance(obj, (Package, Resource)):
if isinstance(obj, (Package, Resource, Group)):
self.notify(obj, DomainObjectOperation.new)
for obj in set(deleted):
if isinstance(obj, (Package, Resource)):
if isinstance(obj, (Package, Resource, Group)):
self.notify(obj, DomainObjectOperation.deleted)
for obj in set(changed):
if isinstance(obj, Resource):
if isinstance(obj, (Resource, Group)):
self.notify(obj, DomainObjectOperation.changed)
if getattr(obj, 'url_changed', False):
for item in PluginImplementations(IResourceUrlChange):
Expand Down

0 comments on commit 86eb93c

Please sign in to comment.