Skip to content

Commit

Permalink
When topic is deleted, discussions are purged (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
quaxsze committed Jan 16, 2024
1 parent 0115449 commit 6a23664
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Defining a contact point for a dataset is done throught a form field
- Allow wildcards in redirect_uris for Oauth2Client [#2935](https://github.com/opendatateam/udata/pull/2935)
- Allow for being one day late on update fulfilled in time [#2941](https://github.com/opendatateam/udata/pull/2941)
- When a topic is deleted, corresponding discussions are purged [#2944](https://github.com/opendatateam/udata/pull/2944)

## 7.0.1 (2023-12-06)

Expand Down
3 changes: 3 additions & 0 deletions udata/core/topic/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from udata.api import api, fields, API
from udata.core.dataset.api_fields import dataset_fields
from udata.core.discussions.models import Discussion
from udata.core.organization.api_fields import org_ref_fields
from udata.core.reuse.api_fields import reuse_fields
from udata.core.topic.permissions import TopicEditPermission
Expand Down Expand Up @@ -121,5 +122,7 @@ def delete(self, topic):
'''Delete a given topic'''
if not TopicEditPermission(topic).can():
api.abort(403, 'Forbidden')
# Remove discussions linked to the topic
Discussion.objects(subject=topic).delete()
topic.delete()
return '', 204
19 changes: 18 additions & 1 deletion udata/tests/api/test_topics_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from udata.core.topic.models import Topic
from udata.core.topic.factories import TopicFactory
from udata.core.user.factories import UserFactory
from udata.models import Member
from udata.models import Member, Discussion

from . import APITestCase

Expand Down Expand Up @@ -122,10 +122,27 @@ def test_topic_api_delete(self):
'''It should delete a topic from the API'''
owner = self.login()
topic = TopicFactory(owner=owner)

with self.api_user():
response = self.post(url_for('api.discussions'), {
'title': 'test title',
'comment': 'bla bla',
'subject': {
'class': 'Topic',
'id': topic.id,
}
})
self.assert201(response)

discussions = Discussion.objects(subject=topic)
self.assertEqual(len(discussions), 1)

with self.api_user():
response = self.delete(url_for('api.topic', topic=topic))
self.assertStatus(response, 204)

self.assertEqual(Topic.objects.count(), 0)
self.assertEqual(Discussion.objects.count(), 0)

def test_topic_api_delete_perm(self):
'''It should not delete a topic from the API'''
Expand Down

0 comments on commit 6a23664

Please sign in to comment.