Skip to content

Commit

Permalink
prevent submitter duplicity as a reviewer; enforce reviewer uniquenes…
Browse files Browse the repository at this point in the history
…s in model
  • Loading branch information
Jiri Suchan committed Aug 23, 2019
1 parent 23b81d7 commit c5e802e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 9 additions & 2 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='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')
)
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_post_create_form(self):
'title': "Subpage submitted",
'slug': 'subpage-submitted',

'create_review_reviewers-TOTAL_FORMS': 2,
'create_review_reviewers-TOTAL_FORMS': 3,
'create_review_reviewers-INITIAL_FORMS': 0,
'create_review_reviewers-MIN_NUM_FORMS': 0,
'create_review_reviewers-MAX_NUM_FORMS': 1000,
Expand All @@ -145,6 +145,10 @@ def test_post_create_form(self):
'create_review_reviewers-1-email': '',
'create_review_reviewers-1-DELETE': '',

'create_review_reviewers-2-user': self.user.pk,
'create_review_reviewers-2-email': self.user.email,
'create_review_reviewers-2-DELETE': '',

'action-submit-for-review': '1',
})

Expand All @@ -160,3 +164,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)
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')},
),
]
6 changes: 4 additions & 2 deletions wagtail_review/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ def handle_submit_for_review(request, page):
form.save()
reviewer_formset.save()

# create a reviewer record for the current user
review.reviewers.create(user=review.submitter)
reviewers = filter(None, [f.cleaned_data['user'] for f in reviewer_formset.forms])
if review.submitter not in reviewers:
# create a reviewer record for the current user
review.reviewers.create(user=review.submitter)

review.send_request_emails()

Expand Down

0 comments on commit c5e802e

Please sign in to comment.