diff --git a/tests/test_admin.py b/tests/test_admin.py index 2a02b78..76213dd 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -13,7 +13,7 @@ class TestAdminViews(TestCase): fixtures = ['test.json'] def setUp(self): - User.objects.create_superuser(username='admin', email='admin@example.com', password='password') + self.user = User.objects.create_superuser(username='admin', email='admin@example.com', password='password') self.assertTrue( self.client.login(username='admin', password='password') ) @@ -128,11 +128,12 @@ def test_post_edit_form(self): self.assertEqual(email_recipients, {'someone@example.com', 'spongebob@example.com'}) def test_post_create_form(self): + review_user = User.objects.get(username='spongebob') response = self.client.post('/admin/pages/add/tests/simplepage/2/', { 'title': "Subpage submitted", 'slug': 'subpage-submitted', - 'create_review_reviewers-TOTAL_FORMS': 2, + 'create_review_reviewers-TOTAL_FORMS': 4, 'create_review_reviewers-INITIAL_FORMS': 0, 'create_review_reviewers-MIN_NUM_FORMS': 0, 'create_review_reviewers-MAX_NUM_FORMS': 1000, @@ -141,10 +142,18 @@ def test_post_create_form(self): 'create_review_reviewers-0-email': 'someone@example.com', 'create_review_reviewers-0-DELETE': '', - 'create_review_reviewers-1-user': User.objects.get(username='spongebob').pk, + 'create_review_reviewers-1-user': review_user.pk, 'create_review_reviewers-1-email': '', 'create_review_reviewers-1-DELETE': '', + 'create_review_reviewers-2-user': review_user.pk, + 'create_review_reviewers-2-email': '', + 'create_review_reviewers-2-DELETE': '', + + 'create_review_reviewers-3-user': self.user.pk, + 'create_review_reviewers-3-email': self.user.email, + 'create_review_reviewers-3-DELETE': '', + 'action-submit-for-review': '1', }) @@ -160,3 +169,6 @@ def test_post_create_form(self): self.assertEqual(len(mail.outbox), 2) email_recipients = set(email.to[0] for email in mail.outbox) self.assertEqual(email_recipients, {'someone@example.com', 'spongebob@example.com'}) + + review_view = self.client.get('/admin/wagtail_review/reviews/%s/view/' % review.pk) + self.assertEqual(review_view.status_code, 200) diff --git a/wagtail_review/migrations/0004_make_reviewer_unique.py b/wagtail_review/migrations/0004_make_reviewer_unique.py new file mode 100644 index 0000000..6565c03 --- /dev/null +++ b/wagtail_review/migrations/0004_make_reviewer_unique.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.4 on 2019-08-23 16:37 + +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('wagtail_review', '0003_response'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='reviewer', + unique_together={('review', 'user')}, + ), + ] diff --git a/wagtail_review/wagtail_hooks.py b/wagtail_review/wagtail_hooks.py index 0e2d7e1..711c438 100644 --- a/wagtail_review/wagtail_hooks.py +++ b/wagtail_review/wagtail_hooks.py @@ -97,11 +97,17 @@ def handle_submit_for_review(request, page): raise Exception("Reviewer formset failed validation") form.save() - reviewer_formset.save() - - # create a reviewer record for the current user review.reviewers.create(user=review.submitter) + review_users = {review.submitter} + instances = reviewer_formset.save(commit=False) + for instance in instances: + if instance.user in review_users: + continue + if instance.user: + review_users.add(instance.user) + instance.save() + review.send_request_emails() # clear original confirmation message as set by the create/edit view,