Skip to content

Commit

Permalink
prevent duplicate reviewers; enforce reviewer uniqueness in model
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Suchan committed Aug 24, 2019
1 parent 23b81d7 commit bb17d7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
18 changes: 15 additions & 3 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestAdminViews(TestCase):
fixtures = ['test.json']

def setUp(self):
User.objects.create_superuser(username='admin', email='[email protected]', password='password')
self.user = User.objects.create_superuser(username='admin', email='[email protected]', password='password')
self.assertTrue(
self.client.login(username='admin', password='password')
)
Expand Down Expand Up @@ -128,11 +128,12 @@ def test_post_edit_form(self):
self.assertEqual(email_recipients, {'[email protected]', '[email protected]'})

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,
Expand All @@ -141,10 +142,18 @@ def test_post_create_form(self):
'create_review_reviewers-0-email': '[email protected]',
'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',
})

Expand All @@ -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, {'[email protected]', '[email protected]'})

review_view = self.client.get('/admin/wagtail_review/reviews/%s/view/' % review.pk)
self.assertEqual(review_view.status_code, 200)
19 changes: 19 additions & 0 deletions wagtail_review/migrations/0004_make_reviewer_unique.py
Original file line number Diff line number Diff line change
@@ -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')},
),
]
10 changes: 9 additions & 1 deletion wagtail_review/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ 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,
Expand Down

0 comments on commit bb17d7e

Please sign in to comment.