diff --git a/comments/models.py b/comments/models.py index 82ae4cf7bb8e43c962f9fdf99fd3ead00923e41d..b5d00c911998a27de84ae928a1ded10b955c6a77 100644 --- a/comments/models.py +++ b/comments/models.py @@ -4,6 +4,7 @@ from django.db import models from django.shortcuts import get_object_or_404 from django.utils import timezone from django.utils.functional import cached_property +from django.urls import reverse from guardian.shortcuts import assign_perm @@ -131,6 +132,9 @@ class Comment(TimeStampedModel): def get_absolute_url(self): return self.content_object.get_absolute_url().split('#')[0] + '#comment_id' + str(self.id) + def get_attachment_url(self): + return reverse('comments:attachment', args=(self.id,)) + def grant_permissions(self): # Import here due to circular import errors from submissions.models import Submission diff --git a/comments/templates/comments/_add_comment_form.html b/comments/templates/comments/_add_comment_form.html index b3a51a16b34e172a2155dfb7ff6031e318a541e1..e50ce6528019f236fa9c2e7f8a634d63d86c9ec1 100644 --- a/comments/templates/comments/_add_comment_form.html +++ b/comments/templates/comments/_add_comment_form.html @@ -17,6 +17,7 @@ <div class="row"> <div class="col-md-9"> {{form.comment_text|bootstrap:'12,12'}} + <p> In your comment, you can use LaTeX \$...\$ for in-text equations or \ [ ... \ ] for on-line equations. </p> diff --git a/comments/templates/comments/_single_comment.html b/comments/templates/comments/_single_comment.html index 0759c42ee017a67d2bbf6d345f140b5baf7b29aa..ee94826ec7b15950249cc4f51465cee07fb6b5a4 100644 --- a/comments/templates/comments/_single_comment.html +++ b/comments/templates/comments/_single_comment.html @@ -3,7 +3,6 @@ {% load file_extentions %} <div class="comment"> - {% include 'comments/_comment_identifier.html' with comment=comment %} {% include 'comments/_comment_categories.html' with comment=comment %} @@ -16,9 +15,9 @@ {% if comment.file_attachment %} <h3>Attachment:</h3> <p> - <a target="_blank" href="{{ comment.file_attachment.url }}"> + <a target="_blank" href="{{ comment.get_attachment_url }}"> {% if comment.file_attachment|is_image %} - <img class="attachment attachment-comment" src="{{ comment.file_attachment.url }}"> + <img class="attachment attachment-comment" src="{{ comment.get_attachment_url }}"> {% else %} {{ comment.file_attachment|filename }}<br><small>{{ comment.file_attachment.size|filesizeformat }}</small> {% endif %} diff --git a/comments/templates/comments/_vet_comment_form.html b/comments/templates/comments/_vet_comment_form.html index 204b12d7017b92b141c0e0aeb8c04271136f0e6b..711beb89fe8edd8c63ec00263c7f17a8a4357942 100644 --- a/comments/templates/comments/_vet_comment_form.html +++ b/comments/templates/comments/_vet_comment_form.html @@ -23,9 +23,9 @@ {% if comment.file_attachment %} <h3>Attachment:</h3> <p> - <a target="_blank" href="{{ comment.file_attachment.url }}"> + <a target="_blank" href="{{ comment.get_attachment_url }}"> {% if comment.file_attachment|is_image %} - <img class="attachment attachment-comment" src="{{ comment.file_attachment.url }}"> + <img class="attachment attachment-comment" src="{{ comment.get_attachment_url }}"> {% else %} {{ comment.file_attachment|filename }}<br><small>{{ comment.file_attachment.size|filesizeformat }}</small> {% endif %} diff --git a/comments/templates/comments/add_comment.html b/comments/templates/comments/add_comment.html new file mode 100644 index 0000000000000000000000000000000000000000..780fb54a30d4281d6c02acd41caef0272f14f03a --- /dev/null +++ b/comments/templates/comments/add_comment.html @@ -0,0 +1,22 @@ +{% extends 'scipost/base.html' %} + +{% load bootstrap %} + +{% block pagetitle %}: Add Comment{% endblock pagetitle %} + +{% block content %} + + <h1 class="highlight">Add Comment</h1> + + + {% if user.is_authenticated and perms.scipost.can_submit_comments %} + + <hr> + + <h2 class="highlight" id="contribute_comment">Contribute a Comment:</h2> + + {% include 'comments/_add_comment_form.html' with form=form url='' %} + + {% endif %} + +{% endblock content %} diff --git a/comments/urls.py b/comments/urls.py index 213fbd1ad0d6a5bcfaa152aaf878469adc2ed086..1c7f2a46e6939064148007f742a813b023078b8f 100644 --- a/comments/urls.py +++ b/comments/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ url(r'^vet_submitted$', views.vet_submitted_comments_list, name='vet_submitted_comments_list'), url(r'^new/(?P<type_of_object>[a-z]+)/(?P<object_id>[0-9]+)$', views.new_comment, name='new_comment'), + url(r'^(?P<comment_id>[0-9]+)/attachment$', views.attachment, name='attachment'), url(r'^(?P<comment_id>[0-9]+)/reply$', views.reply_to_comment, name='reply_to_comment'), url(r'^(?P<comment_id>[0-9]+)/vet$', views.vet_submitted_comment, name='vet_submitted_comment'), diff --git a/comments/views.py b/comments/views.py index fba5791ba8e8a1b212eee8660dc77d77b7f32bb0..fb5199bd410e3758df115d1d6ac6486979108c4f 100644 --- a/comments/views.py +++ b/comments/views.py @@ -1,16 +1,18 @@ -from django.utils import timezone -from django.shortcuts import get_object_or_404, render, redirect from django.contrib.auth.decorators import permission_required, login_required from django.contrib import messages from django.core.urlresolvers import reverse from django.db import transaction +from django.http import HttpResponse, Http404 +from django.utils import timezone +from django.shortcuts import get_object_or_404, render, redirect from guardian.shortcuts import get_objects_for_user import strings +from .constants import EXTENTIONS_IMAGES, EXTENTIONS_PDF from .models import Comment from .forms import CommentForm, VetCommentForm -from .utils import CommentUtils +from .utils import CommentUtils, validate_file_extention from theses.models import ThesisLink from submissions.utils import SubmissionUtils @@ -20,7 +22,7 @@ from commentaries.models import Commentary @permission_required('scipost.can_submit_comments', raise_exception=True) def new_comment(request, **kwargs): - form = CommentForm(request.POST or None) + form = CommentForm(request.POST or None, request.FILES or None) if form.is_valid(): object_id = int(kwargs["object_id"]) type_of_object = kwargs["type_of_object"] @@ -41,6 +43,8 @@ def new_comment(request, **kwargs): messages.success(request, strings.acknowledge_submit_comment) return redirect(_object.get_absolute_url()) + context = {'form': form} + return(render(request, 'comments/add_comment.html', context)) @permission_required('scipost.can_vet_comments', raise_exception=True) @@ -178,3 +182,21 @@ def express_opinion(request, comment_id, opinion): comment = get_object_or_404(Comment, pk=comment_id) comment.update_opinions(request.user.contributor.id, opinion) return redirect(comment.get_absolute_url()) + + +def attachment(request, comment_id): + """ + Open/read attachment of Comment if available. + """ + comment = get_object_or_404(Comment.objects.exclude(file_attachment=''), pk=comment_id) + if validate_file_extention(comment.file_attachment, EXTENTIONS_IMAGES): + content_type = 'image/jpeg' + elif validate_file_extention(comment.file_attachment, EXTENTIONS_PDF): + content_type = 'application/pdf' + else: + raise Http404 + + response = HttpResponse(comment.file_attachment.read(), content_type=content_type) + filename = 'comment-attachment-%s' % comment.file_attachment.name + response['Content-Disposition'] = ('filename=' + filename) + return response diff --git a/scipost/static/scipost/assets/css/_form.scss b/scipost/static/scipost/assets/css/_form.scss index 60f2289b07992d78f8c5cb1eed829d31add22677..62172b512ef1273cd62cd0b38255881f6c72d7d5 100644 --- a/scipost/static/scipost/assets/css/_form.scss +++ b/scipost/static/scipost/assets/css/_form.scss @@ -10,6 +10,10 @@ border-color: #d9534f; } +.has-error .help-block { + color: $brand-danger; +} + .has-error .multiple-checkbox .help-block { color: $brand-danger; font-weight: 600;