diff --git a/comments/templates/comments/new_comment.html b/comments/templates/comments/new_comment.html new file mode 100644 index 0000000000000000000000000000000000000000..572b9fd432b7b0b49c288d8059e04aa26a2c76ba --- /dev/null +++ b/comments/templates/comments/new_comment.html @@ -0,0 +1,27 @@ +{% if user.is_authenticated and thesislink.open_for_commenting and perms.scipost.can_submit_comments %} +<hr /> + +<div class="row"> + <div class="col-12"> + <div class="panel"> + <h2>Contribute a Comment:</h2> + </div> + </div> +</div> +<div class="row"> + <div class="col-12"> + <form enctype="multipart/form-data" + action="{% url 'comments:new_comment' object_id=object_id type_of_object=type_of_object %}" + method="post"> + {% csrf_token %} + {% load crispy_forms_tags %} + {% crispy form %} + </form> + </div> + <div class="col-12"> + <h3>Preview of your comment:</h3> + <p id="preview-comment_text"></p> + </div> +</div> + +{% endif %} diff --git a/comments/test_views.py b/comments/test_views.py new file mode 100644 index 0000000000000000000000000000000000000000..0f382843b92884afda3c6d1a4871690099b99c04 --- /dev/null +++ b/comments/test_views.py @@ -0,0 +1,38 @@ +from django.test import TestCase, RequestFactory, Client +from django.urls import reverse, reverse_lazy +from django.contrib.auth.models import Group + +from scipost.factories import ContributorFactory +from theses.factories import ThesisLinkFactory + +from .factories import CommentFactory +from .forms import CommentForm +from .models import Comment +from .views import new_comment + +from common.helpers import model_form_data + + +class TestNewComment(TestCase): + fixtures = ['groups', 'permissions'] + + def test_submitting_comment_on_thesislink_creates_comment_and_redirects(self): + """ Valid Comment gets saved """ + + contributor = ContributorFactory() + thesislink = ThesisLinkFactory() + valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) + target = reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id}) + + comment_count = Comment.objects.filter(author=contributor).count() + self.assertEqual(comment_count, 0) + + request = RequestFactory().post(target, valid_comment_data) + request.user = contributor.user + response = new_comment(request, object_id=thesislink.id, type_of_object='thesislink') + + comment_count = Comment.objects.filter(author=contributor).count() + self.assertEqual(comment_count, 1) + + response.client = Client() + self.assertRedirects(response, reverse('theses:thesis', kwargs={"thesislink_id":thesislink.id})) diff --git a/comments/urls.py b/comments/urls.py index b417f028aa127ff3d16f325d953216416131e4a9..3d1747ee28e087ff4a52d0da5cd0a794032fc935 100644 --- a/comments/urls.py +++ b/comments/urls.py @@ -10,4 +10,5 @@ urlpatterns = [ url(r'^vet_submitted_comment_ack/(?P<comment_id>[0-9]+)$', views.vet_submitted_comment_ack, name='vet_submitted_comment_ack'), url(r'^express_opinion/(?P<comment_id>[0-9]+)$', views.express_opinion, name='express_opinion'), url(r'^express_opinion/(?P<comment_id>[0-9]+)/(?P<opinion>[AND])$', views.express_opinion, name='express_opinion'), + url(r'^new_comment/(?P<type_of_object>[a-z]+)/(?P<object_id>[0-9]+)$', views.new_comment, name='new_comment') ] diff --git a/comments/views.py b/comments/views.py index 254206ca81d8248a8dadc41049646b1566b4ba8d..f07ed6d76f01f0d040566a5f7d8e76d35fc9852e 100644 --- a/comments/views.py +++ b/comments/views.py @@ -1,17 +1,59 @@ from django.utils import timezone -from django.shortcuts import get_object_or_404, render +from django.shortcuts import get_object_or_404, render, redirect from django.contrib.auth.decorators import permission_required from django.core.mail import EmailMessage from django.core.urlresolvers import reverse -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, Http404 from .models import Comment from .forms import CommentForm, VetCommentForm, comment_refusal_dict from scipost.models import Contributor, title_dict +from theses.models import ThesisLink from submissions.utils import SubmissionUtils from submissions.models import Report +@permission_required('scipost.can_submit_comments', raise_exception=True) +def new_comment(request, **kwargs): + if request.method == "POST": + form = CommentForm(request.POST) + if form.is_valid(): + author = Contributor.objects.get(user=request.user) + object_id = int(kwargs["object_id"]) + type_of_object = kwargs["type_of_object"] + new_comment = Comment( + # thesislink=thesislink, + author=author, + is_rem=form.cleaned_data['is_rem'], + is_que=form.cleaned_data['is_que'], + is_ans=form.cleaned_data['is_ans'], + is_obj=form.cleaned_data['is_obj'], + is_rep=form.cleaned_data['is_rep'], + is_val=form.cleaned_data['is_val'], + is_lit=form.cleaned_data['is_lit'], + is_sug=form.cleaned_data['is_sug'], + comment_text=form.cleaned_data['comment_text'], + remarks_for_editors=form.cleaned_data['remarks_for_editors'], + date_submitted=timezone.now(), + ) + if type_of_object == "thesislink": + thesislink = ThesisLink.objects.get(id=object_id) + new_comment.thesislink = thesislink + elif type_of_object == "submission": + # TODO + 1 + 1 + elif type_of_object == "commentary": + # TODO + 1 + 1 + + new_comment.save() + author.nr_comments = Comment.objects.filter(author=author).count() + author.save() + if type_of_object == "thesislink": + return redirect('theses:thesis', thesislink_id=object_id) + else: + # This view is only accessible by POST request + raise Http404 @permission_required('scipost.can_vet_comments', raise_exception=True) def vet_submitted_comments(request): diff --git a/scipost/factories.py b/scipost/factories.py index 27ebc93ef459df1de461cf65af874680d0ecbb48..e32c408deb837497d49cfbffff1e8e8c49b76b23 100644 --- a/scipost/factories.py +++ b/scipost/factories.py @@ -12,7 +12,7 @@ class ContributorFactory(factory.django.DjangoModelFactory): title = "MR" user = factory.SubFactory('scipost.factories.UserFactory', contributor=None) - status = 1 + status = 1 # normal user vetted_by = factory.SubFactory('scipost.factories.ContributorFactory', vetted_by=None) diff --git a/theses/templates/theses/thesis_detail.html b/theses/templates/theses/thesis_detail.html index f355fc701ff43ee369ebbfe9f8fc5d13e60f7270..8bca24d3e25592ef682c0b42b85427f4dffbda5c 100644 --- a/theses/templates/theses/thesis_detail.html +++ b/theses/templates/theses/thesis_detail.html @@ -50,30 +50,6 @@ {% include 'scipost/comments_block.html' %} -{% if user.is_authenticated and thesislink.open_for_commenting and perms.scipost.can_submit_comments %} -<hr /> - -<div class="row"> - <div class="col-12"> - <div class="panel"> - <h2>Contribute a Comment:</h2> - </div> - </div> -</div> -<div class="row"> - <div class="col-12"> - <form enctype="multipart/form-data" action="{% url 'theses:thesis' thesislink_id=thesislink.id %}" method="post"> - {% csrf_token %} - {% load crispy_forms_tags %} - {% crispy form %} - </form> - </div> - <div class="col-12"> - <h3>Preview of your comment:</h3> - <p id="preview-comment_text"></p> - </div> -</div> - -{% endif %} +{% include 'comments/new_comment.html' with object_id=thesislink.id type_of_object='thesislink' %} {% endblock content %} diff --git a/theses/test_views.py b/theses/test_views.py index b5ee851bcf04cd742cfc21c0f7c33a3976019550..3ee2ccd9e1a80a0c5f24c8c17483ed2fcf16eea8 100644 --- a/theses/test_views.py +++ b/theses/test_views.py @@ -30,23 +30,23 @@ class TestThesisDetail(TestCase): response = client.post(target) self.assertEqual(response.status_code, 200) - def test_submitting_comment_creates_comment(self): - """ Valid Comment gets saved """ - - contributor = ContributorFactory() - thesislink = ThesisLinkFactory() - valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) - target = reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id}) - - comment_count = Comment.objects.filter(author=contributor).count() - self.assertEqual(comment_count, 0) - - request = RequestFactory().post(target, valid_comment_data) - request.user = contributor.user - response = thesis_detail(request, thesislink_id=thesislink.id) - - comment_count = Comment.objects.filter(author=contributor).count() - self.assertEqual(comment_count, 1) + # def test_submitting_comment_creates_comment(self): + # """ Valid Comment gets saved """ + # + # contributor = ContributorFactory() + # thesislink = ThesisLinkFactory() + # valid_comment_data = model_form_data(CommentFactory.build(), CommentForm) + # target = reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id}) + # + # comment_count = Comment.objects.filter(author=contributor).count() + # self.assertEqual(comment_count, 0) + # + # request = RequestFactory().post(target, valid_comment_data) + # request.user = contributor.user + # response = thesis_detail(request, thesislink_id=thesislink.id) + # + # comment_count = Comment.objects.filter(author=contributor).count() + # self.assertEqual(comment_count, 1) class TestRequestThesisLink(TestCase): @@ -186,7 +186,7 @@ class TestTheses(TestCase): response = self.client.get(self.target) search_results = response.context["search_results"] recent_theses = response.context["recent_theses"] - self.assertEqual(search_results.exists(), False) + self.assertEqual(search_results, []) self.assertEqual(recent_theses.exists(), True) def test_search_query_on_author(self): diff --git a/theses/views.py b/theses/views.py index b000a467910850a4be93dde39ab4db7acc7036d6..e11c21f2d42ff2cefa05ef6ebd41275d3fa725a1 100644 --- a/theses/views.py +++ b/theses/views.py @@ -132,46 +132,12 @@ def browse(request, discipline, nrweeksback): def thesis_detail(request, thesislink_id): thesislink = get_object_or_404(ThesisLink, pk=thesislink_id) comments = thesislink.comment_set.all() - if request.method == 'POST': - form = CommentForm(request.POST) - if form.is_valid(): - author = Contributor.objects.get(user=request.user) - new_comment = Comment( - thesislink=thesislink, - author=author, - is_rem=form.cleaned_data['is_rem'], - is_que=form.cleaned_data['is_que'], - is_ans=form.cleaned_data['is_ans'], - is_obj=form.cleaned_data['is_obj'], - is_rep=form.cleaned_data['is_rep'], - is_val=form.cleaned_data['is_val'], - is_lit=form.cleaned_data['is_lit'], - is_sug=form.cleaned_data['is_sug'], - comment_text=form.cleaned_data['comment_text'], - remarks_for_editors=form.cleaned_data['remarks_for_editors'], - date_submitted=timezone.now(), - ) - new_comment.save() - author.nr_comments = Comment.objects.filter(author=author).count() - author.save() - context = { - 'ack_header': 'Thank you for contributing a Comment.', - 'ack_message': 'It will soon be vetted by an Editor.', - 'followup_message': 'Back to the ', - 'followup_link': reverse( - 'theses:thesis', - kwargs={'thesislink_id': new_comment.thesislink.id} - ), - 'followup_link_label': ' Thesis Link page you came from' - } - return render(request, 'scipost/acknowledgement.html', context) - else: - form = CommentForm() - + form = CommentForm() try: author_replies = Comment.objects.filter(thesislink=thesislink, is_author_reply=True) except Comment.DoesNotExist: author_replies = () + # TODO: make manager for horribly obfuscating 'status__gte=1' context = {'thesislink': thesislink, 'comments': comments.filter(status__gte=1).order_by('date_submitted'), 'author_replies': author_replies, 'form': form}