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()