Skip to content

Commit

Permalink
Handle private datasets smartly
Browse files Browse the repository at this point in the history
  • Loading branch information
abulte committed Jun 19, 2021
1 parent 23146d0 commit 6484d79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions udata/features/webhooks/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions udata/tests/features/webhooks/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand Down

0 comments on commit 6484d79

Please sign in to comment.