Skip to content

Commit

Permalink
Merge pull request #1011 from abulte/gh-1008-fix-org-delete-facet
Browse files Browse the repository at this point in the history
Reindex datasets when an organization is purged
  • Loading branch information
abulte committed Jul 4, 2017
2 parents 7df8097 + 2724249 commit f9a147b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
[#1004](https://github.com/opendatateam/udata/pull/1004)
- Fix the `udata search reindex` command
[#1009](https://github.com/opendatateam/udata/issues/1009)
- Reindex datasets when their parent organization is purged
[#1008](https://github.com/opendatateam/udata/issues/1008)

## 1.0.11 (2017-05-25)

Expand Down
8 changes: 7 additions & 1 deletion udata/core/organization/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from udata import mail
from udata.i18n import lazy_gettext as _
from udata.models import Organization, Follow, Activity, Metrics
from udata.models import Organization, Follow, Activity, Metrics, Dataset
from udata.search import reindex
from udata.tasks import job, task, get_logger

log = get_logger(__name__)
Expand All @@ -20,8 +21,13 @@ def purge_organizations(self):
Activity.objects(organization=organization).delete()
# Remove metrics
Metrics.objects(object_id=organization.id).delete()
# Store datasets for later reindexation
d_ids = [d.id for d in Dataset.objects(organization=organization)]
# Remove
organization.delete()
# Reindex the datasets that were linked to the organization
for dataset in Dataset.objects(id__in=d_ids):
reindex(dataset)


@task
Expand Down
1 change: 0 additions & 1 deletion udata/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def reindex(obj):
except:
log.exception('Unable to index %s "%s"',
model.__name__, str(obj.id))
adapter.delete(using=es.client, index=es.index_name)
else:
log.info('Nothing to do for %s (%s)', model.__name__, obj.id)

Expand Down
31 changes: 31 additions & 0 deletions udata/tests/organization/test_organization_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

from .. import TestCase, SearchTestMixin

from udata.models import Dataset, Organization
from udata.core.dataset.factories import DatasetFactory, ResourceFactory
from udata.core.dataset.search import DatasetSearch
from udata.core.organization import tasks
from udata.search import es


class OrganizationTasksTest(SearchTestMixin, TestCase):
def test_purge_organizations(self):
with self.autoindex():
org = Organization.objects.create(
name='delete me', deleted='2016-01-01', description='XXX')
resources = [ResourceFactory() for _ in range(2)]
dataset = DatasetFactory(resources=resources, organization=org)

tasks.purge_organizations()

dataset = Dataset.objects(id=dataset.id).first()
self.assertEqual(dataset.organization, None)

organization = Organization.objects(name='delete me').first()
self.assertEqual(organization, None)

indexed_dataset = DatasetSearch.get(id=dataset.id,
using=es.client,
index=es.index_name)
self.assertEqual(indexed_dataset.organization, '')

0 comments on commit f9a147b

Please sign in to comment.