From 32346cf9a3675f8d627c00701827397b98c835c9 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Tue, 19 Jan 2021 22:30:51 -0800 Subject: [PATCH] Cascade-delete progress updates Add a warning about it, and update test to confirm it works --- bookwyrm/models/readthrough.py | 2 +- bookwyrm/templates/snippets/components/modal.html | 7 +++---- .../templates/snippets/delete_readthrough_modal.html | 6 +++++- bookwyrm/tests/actions/test_readthrough.py | 9 +++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/bookwyrm/models/readthrough.py b/bookwyrm/models/readthrough.py index 3afde306..8ec3c9c6 100644 --- a/bookwyrm/models/readthrough.py +++ b/bookwyrm/models/readthrough.py @@ -42,7 +42,7 @@ class ReadThrough(BookWyrmModel): class ProgressUpdate(BookWyrmModel): ''' Store progress through a book in the database. ''' user = models.ForeignKey('User', on_delete=models.PROTECT) - readthrough = models.ForeignKey('ReadThrough', on_delete=models.PROTECT) + readthrough = models.ForeignKey('ReadThrough', on_delete=models.CASCADE) progress = models.IntegerField() mode = models.CharField( max_length=3, diff --git a/bookwyrm/templates/snippets/components/modal.html b/bookwyrm/templates/snippets/components/modal.html index 3eec9efa..72402914 100644 --- a/bookwyrm/templates/snippets/components/modal.html +++ b/bookwyrm/templates/snippets/components/modal.html @@ -7,11 +7,10 @@ {% include 'snippets/toggle/toggle_button.html' with label="close" class="delete" nonbutton=True %} - {% block modal-form-open %}{% endblock %} - - {% block modal-body %}{% endblock %} - + diff --git a/bookwyrm/templates/snippets/delete_readthrough_modal.html b/bookwyrm/templates/snippets/delete_readthrough_modal.html index 2155afb3..c04a1d90 100644 --- a/bookwyrm/templates/snippets/delete_readthrough_modal.html +++ b/bookwyrm/templates/snippets/delete_readthrough_modal.html @@ -1,6 +1,10 @@ {% extends 'snippets/components/modal.html' %} {% block modal-title %}Delete these read dates?{% endblock %} - +{% block modal-body %} +{% if readthrough.progress_updates|length > 0 %} +You are deleting this readthrough and its {{ readthrough.progress_updates|length }} associated progress updates. +{% endif %} +{% endblock %} {% block modal-footer %}
{% csrf_token %} diff --git a/bookwyrm/tests/actions/test_readthrough.py b/bookwyrm/tests/actions/test_readthrough.py index bd24cec2..41d2eaa5 100644 --- a/bookwyrm/tests/actions/test_readthrough.py +++ b/bookwyrm/tests/actions/test_readthrough.py @@ -77,3 +77,12 @@ class ReadThrough(TestCase): self.assertEqual(progress_updates[1].mode, models.ProgressMode.PAGE) self.assertEqual(progress_updates[1].progress, 100) self.assertEqual(delay_mock.call_count, 1) # Edit doesn't publish anything + + self.client.post('/delete-readthrough', { + 'id': readthroughs[0].id, + }) + + readthroughs = self.edition.readthrough_set.all() + updates = self.user.progressupdate_set.all() + self.assertEqual(len(readthroughs), 0) + self.assertEqual(len(updates), 0)