SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 2d3600f4 authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Update tests to new Comment construction

parent 167fd66e
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,7 @@ class UnpublishedVettedCommentaryFactory(VettedCommentaryFactory): ...@@ -60,6 +60,7 @@ class UnpublishedVettedCommentaryFactory(VettedCommentaryFactory):
class UnvettedCommentaryFactory(CommentaryFactory): class UnvettedCommentaryFactory(CommentaryFactory):
vetted = False vetted = False
class UnvettedArxivPreprintCommentaryFactory(CommentaryFactory): class UnvettedArxivPreprintCommentaryFactory(CommentaryFactory):
vetted = False vetted = False
pub_DOI = None pub_DOI = None
from django import forms from django import forms
from django.contrib.contenttypes.models import ContentType
from .constants import COMMENT_ACTION_CHOICES, COMMENT_REFUSAL_CHOICES from .constants import COMMENT_ACTION_CHOICES, COMMENT_REFUSAL_CHOICES
from .models import Comment from .models import Comment
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field, Fieldset, HTML, Submit
class CommentForm(forms.ModelForm): class CommentForm(forms.ModelForm):
class Meta: class Meta:
...@@ -21,43 +17,6 @@ class CommentForm(forms.ModelForm): ...@@ -21,43 +17,6 @@ class CommentForm(forms.ModelForm):
{'placeholder': 'NOTE: only serious and meaningful Comments will be accepted.'}) {'placeholder': 'NOTE: only serious and meaningful Comments will be accepted.'})
self.fields['remarks_for_editors'].widget.attrs.update( self.fields['remarks_for_editors'].widget.attrs.update(
{'rows': 3, 'placeholder': '(these remarks will not be publicly visible)'}) {'rows': 3, 'placeholder': '(these remarks will not be publicly visible)'})
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Div(
Field('comment_text'),
HTML('<p>In your comment, you can use LaTeX \$...\$ for in-text '
'equations or \ [ ... \ ] for on-line equations.</p>'),
HTML('<p id="goodCommenter"><i>Be professional. Only serious and '
'meaningful comments will be vetted through.</i></p><br/>'),
Field('remarks_for_editors'),
css_class="col-md-9"),
Div(
Fieldset(
'Specify categorization(s):',
'is_cor', 'is_rem', 'is_que', 'is_ans', 'is_obj',
'is_rep', 'is_val', 'is_lit', 'is_sug',
style="border: 0px; font-size: 90%"),
HTML('<br>'),
Div(
Submit('submit', 'Submit your Comment for vetting',
css_class="submitButton"),
HTML('<p id="goodCommenter"><i>By clicking on Submit, you agree with the '
'<a href="{% url \'scipost:terms_and_conditions\' %}">'
'Terms and Conditions</a>.</i></p>'),
),
css_class="col-md-3"),
css_class="row"),
)
# def save(self, commit=True, to_object=None):
# comment = super().save(commit=False)
# if to_object:
# comment.content_type = ContentType.objects.get_for_model(to_object)
# comment.object_id = to_object.id
# if commit:
# comment.save()
# return comment
class VetCommentForm(forms.Form): class VetCommentForm(forms.Form):
......
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
# Generated by Django 1.10.3 on 2017-07-26 19:17 # Generated by Django 1.10.3 on 2017-07-26 19:17
from __future__ import unicode_literals from __future__ import unicode_literals
from django.contrib.auth import get_user_model
from django.db import migrations from django.db import migrations
from guardian.shortcuts import assign_perm from guardian.shortcuts import assign_perm
from ..models import Comment
def do_nothing(apps, schema_editor): def do_nothing(apps, schema_editor):
return return
...@@ -18,12 +17,15 @@ def update_eic_permissions(apps, schema_editor): ...@@ -18,12 +17,15 @@ def update_eic_permissions(apps, schema_editor):
Grant EIC of submission related to unvetted comment Grant EIC of submission related to unvetted comment
permission to vet his submission's comment. permission to vet his submission's comment.
""" """
# Comment = apps.get_model('comments', 'Comment') -- This doesn't work... Comment = apps.get_model('comments', 'Comment')
User = get_user_model()
count = 0 count = 0
for comment in Comment.objects.filter(status=0): for comment in Comment.objects.filter(status=0):
if comment.submission: if comment.submission:
eic_user = comment.submission.editor_in_charge.user # Grant Permissions
assign_perm('comments.can_vet_comments', eic_user, comment) user = User.objects.get(id=comment.submission.editor_in_charge.user.id)
assign_perm('comments.can_vet_comments', user, comment)
count += 1 count += 1
print('\nGranted permission to %i Editor(s)-in-charge to vet related Comments.' % count) print('\nGranted permission to %i Editor(s)-in-charge to vet related Comments.' % count)
......
...@@ -22,7 +22,6 @@ def create_all_permissions(**kwargs): ...@@ -22,7 +22,6 @@ def create_all_permissions(**kwargs):
from django.apps import apps from django.apps import apps
for app_config in apps.get_app_configs(): for app_config in apps.get_app_configs():
print(app_config)
create_permissions(app_config, **kwargs) create_permissions(app_config, **kwargs)
...@@ -84,27 +83,22 @@ def migrate_comments_to_generic_relations(apps, schema_editor): ...@@ -84,27 +83,22 @@ def migrate_comments_to_generic_relations(apps, schema_editor):
_object = comment.in_reply_to_comment _object = comment.in_reply_to_comment
_object_id = comment.in_reply_to_comment.id _object_id = comment.in_reply_to_comment.id
_object_type = ContentType.objects.get(app_label="comments", model="comment").id _object_type = ContentType.objects.get(app_label="comments", model="comment").id
print('comment', comment.id, comment.in_reply_to_comment.id)
elif comment.in_reply_to_report: elif comment.in_reply_to_report:
_object = comment.in_reply_to_report _object = comment.in_reply_to_report
_object_id = comment.in_reply_to_report.id _object_id = comment.in_reply_to_report.id
_object_type = ContentType.objects.get(app_label="submissions", model="report").id _object_type = ContentType.objects.get(app_label="submissions", model="report").id
print('report', comment.id, comment.in_reply_to_report.id)
elif comment.thesislink: elif comment.thesislink:
_object = comment.thesislink _object = comment.thesislink
_object_id = comment.thesislink.id _object_id = comment.thesislink.id
_object_type = ContentType.objects.get(app_label="theses", model="thesislink").id _object_type = ContentType.objects.get(app_label="theses", model="thesislink").id
print('thesis', comment.id, comment.thesislink.id)
elif comment.submission: elif comment.submission:
_object = comment.submission _object = comment.submission
_object_id = comment.submission.id _object_id = comment.submission.id
_object_type = ContentType.objects.get(app_label="submissions", model="submission").id _object_type = ContentType.objects.get(app_label="submissions", model="submission").id
print('submission', comment.id, comment.submission.id)
elif comment.commentary: elif comment.commentary:
_object = comment.commentary _object = comment.commentary
_object_id = comment.commentary.id _object_id = comment.commentary.id
_object_type = ContentType.objects.get(app_label="commentaries", model="commentary").id _object_type = ContentType.objects.get(app_label="commentaries", model="commentary").id
print('commentary', comment.id, comment.commentary.id)
else: else:
print('\nNo valid relation for Comment: ', comment.id,) print('\nNo valid relation for Comment: ', comment.id,)
comment.content_object = _object comment.content_object = _object
......
from django.test import TestCase, RequestFactory, Client from django.test import TestCase, RequestFactory, Client
from django.urls import reverse, reverse_lazy from django.urls import reverse
from django.contrib.auth.models import Group
from django.contrib.messages.storage.fallback import FallbackStorage from django.contrib.messages.storage.fallback import FallbackStorage
from django.core.exceptions import PermissionDenied from django.http import Http404
from scipost.factories import ContributorFactory from scipost.factories import ContributorFactory
from theses.factories import ThesisLinkFactory from theses.factories import ThesisLinkFactory
...@@ -20,6 +19,7 @@ from common.helpers.test import add_groups_and_permissions ...@@ -20,6 +19,7 @@ from common.helpers.test import add_groups_and_permissions
class TestNewComment(TestCase): class TestNewComment(TestCase):
def setUp(self): def setUp(self):
add_groups_and_permissions() add_groups_and_permissions()
ContributorFactory.create_batch(5)
def install_messages_middleware(self, request): def install_messages_middleware(self, request):
# I don't know what the following three lines do, but they help make a RequestFactory # I don't know what the following three lines do, but they help make a RequestFactory
...@@ -33,7 +33,7 @@ class TestNewComment(TestCase): ...@@ -33,7 +33,7 @@ class TestNewComment(TestCase):
contributor = ContributorFactory() contributor = ContributorFactory()
thesislink = ThesisLinkFactory() thesislink = ThesisLinkFactory()
valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) valid_comment_data = model_form_data(CommentFactory, CommentForm)
target = reverse('comments:new_comment', kwargs={'object_id': thesislink.id, 'type_of_object': 'thesislink'}) target = reverse('comments:new_comment', kwargs={'object_id': thesislink.id, 'type_of_object': 'thesislink'})
comment_count = Comment.objects.filter(author=contributor).count() comment_count = Comment.objects.filter(author=contributor).count()
...@@ -56,7 +56,7 @@ class TestNewComment(TestCase): ...@@ -56,7 +56,7 @@ class TestNewComment(TestCase):
submission = EICassignedSubmissionFactory() submission = EICassignedSubmissionFactory()
submission.open_for_commenting = True submission.open_for_commenting = True
submission.save() submission.save()
valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) valid_comment_data = model_form_data(CommentFactory, CommentForm)
target = reverse( target = reverse(
'comments:new_comment', 'comments:new_comment',
kwargs={'object_id': submission.id, 'type_of_object': 'submission'}, kwargs={'object_id': submission.id, 'type_of_object': 'submission'},
...@@ -80,13 +80,12 @@ class TestNewComment(TestCase): ...@@ -80,13 +80,12 @@ class TestNewComment(TestCase):
) )
self.assertRedirects(response, expected_redirect_link) self.assertRedirects(response, expected_redirect_link)
def test_submitting_comment_on_commentary_creates_comment_and_redirects(self): def test_submitting_comment_on_commentary_creates_comment_and_redirects(self):
""" Valid Comment gets saved """ """ Valid Comment gets saved """
contributor = ContributorFactory() contributor = ContributorFactory()
commentary = UnpublishedVettedCommentaryFactory() commentary = UnpublishedVettedCommentaryFactory()
valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) valid_comment_data = model_form_data(CommentFactory, CommentForm)
target = reverse('comments:new_comment', kwargs={'object_id': commentary.id, 'type_of_object': 'commentary'}) target = reverse('comments:new_comment', kwargs={'object_id': commentary.id, 'type_of_object': 'commentary'})
comment_count = Comment.objects.filter(author=contributor).count() comment_count = Comment.objects.filter(author=contributor).count()
...@@ -97,7 +96,7 @@ class TestNewComment(TestCase): ...@@ -97,7 +96,7 @@ class TestNewComment(TestCase):
request.user = contributor.user request.user = contributor.user
response = new_comment(request, object_id=commentary.id, type_of_object='commentary') response = new_comment(request, object_id=commentary.id, type_of_object='commentary')
comment_count = commentary.comment_set.count() comment_count = commentary.comments.count()
self.assertEqual(comment_count, 1) self.assertEqual(comment_count, 1)
response.client = Client() response.client = Client()
...@@ -105,13 +104,12 @@ class TestNewComment(TestCase): ...@@ -105,13 +104,12 @@ class TestNewComment(TestCase):
'commentaries:commentary', kwargs={'arxiv_or_DOI_string': commentary.arxiv_or_DOI_string}) 'commentaries:commentary', kwargs={'arxiv_or_DOI_string': commentary.arxiv_or_DOI_string})
self.assertRedirects(response, expected_redirect_link) self.assertRedirects(response, expected_redirect_link)
def test_submitting_comment_on_submission_that_is_not_open_for_commenting_should_be_impossible(self): def test_submitting_comment_on_submission_that_is_not_open_for_commenting_should_be_impossible(self):
contributor = ContributorFactory() contributor = ContributorFactory()
submission = EICassignedSubmissionFactory() submission = EICassignedSubmissionFactory()
submission.open_for_commenting = False submission.open_for_commenting = False
submission.save() submission.save()
valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) valid_comment_data = model_form_data(CommentFactory, CommentForm)
target = reverse( target = reverse(
'comments:new_comment', 'comments:new_comment',
kwargs={'object_id': submission.id, 'type_of_object': 'submission'}, kwargs={'object_id': submission.id, 'type_of_object': 'submission'},
...@@ -123,5 +121,5 @@ class TestNewComment(TestCase): ...@@ -123,5 +121,5 @@ class TestNewComment(TestCase):
request = RequestFactory().post(target, valid_comment_data) request = RequestFactory().post(target, valid_comment_data)
self.install_messages_middleware(request) self.install_messages_middleware(request)
request.user = contributor.user request.user = contributor.user
with self.assertRaises(PermissionDenied): with self.assertRaises(Http404):
response = new_comment(request, object_id=submission.id, type_of_object='submission') new_comment(request, object_id=submission.id, type_of_object='submission')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment