From 6484d79ef62cf55ffa3a97287d9012c341e682a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bult=C3=A9?= Date: Sat, 19 Jun 2021 21:11:39 +0200 Subject: [PATCH] Handle private datasets smartly --- udata/features/webhooks/triggers.py | 10 +++++++--- udata/tests/features/webhooks/test_webhooks.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/udata/features/webhooks/triggers.py b/udata/features/webhooks/triggers.py index cdc1d43630..d796e3938b 100644 --- a/udata/features/webhooks/triggers.py +++ b/udata/features/webhooks/triggers.py @@ -10,8 +10,8 @@ @Dataset.on_create.connect def on_dataset_create(dataset): - # TODO: emit a private signal somehow (or just rename it to draft) - dispatch('datagouvfr.dataset.created', dataset.to_json()) + if not dataset.private: + dispatch('datagouvfr.dataset.created', dataset.to_json()) @Dataset.on_delete.connect @@ -21,7 +21,11 @@ def on_dataset_delete(dataset): @Dataset.on_update.connect def on_dataset_update(dataset): - dispatch('datagouvfr.dataset.updated', dataset.to_json()) + updates, _ = dataset._delta() + if 'private' in updates and not dataset.private: + dispatch('datagouvfr.dataset.created', dataset.to_json()) + else: + dispatch('datagouvfr.dataset.updated', dataset.to_json()) @on_new_discussion.connect diff --git a/udata/tests/features/webhooks/test_webhooks.py b/udata/tests/features/webhooks/test_webhooks.py index e8a6efdfe2..71d2232249 100644 --- a/udata/tests/features/webhooks/test_webhooks.py +++ b/udata/tests/features/webhooks/test_webhooks.py @@ -113,6 +113,10 @@ def test_dataset_create(self, rmock_pub): assert res['event'] == 'datagouvfr.dataset.created' assert res['payload']['title'] == ds['title'] + def test_dataset_create_private(self, rmock_pub): + DatasetFactory(private=True) + assert not rmock_pub.called + def test_dataset_update(self, rmock_pub): ds = DatasetFactory() ds.title = 'newtitle' @@ -122,6 +126,15 @@ def test_dataset_update(self, rmock_pub): assert res['event'] == 'datagouvfr.dataset.updated' assert res['payload']['title'] == 'newtitle' + def test_dataset_update_to_public(self, rmock_pub): + ds = DatasetFactory(private=True) + ds.private = False + ds.save() + assert rmock_pub.called + res = rmock_pub.last_request.json() + assert res['event'] == 'datagouvfr.dataset.created' + assert res['payload']['title'] == ds.title + def test_dataset_delete(self, rmock_pub): ds = DatasetFactory() ds.deleted = datetime.now()