From 52f9d079166205d27f4082c4f3aa5ef6d115bb4e Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Sun, 30 Jul 2017 11:37:40 +0200 Subject: [PATCH] Give comment attachments mature url --- comments/models.py | 4 +++ .../templates/comments/_add_comment_form.html | 1 + .../templates/comments/_single_comment.html | 5 ++-- .../templates/comments/_vet_comment_form.html | 4 +-- comments/templates/comments/add_comment.html | 22 ++++++++++++++ comments/urls.py | 1 + comments/views.py | 30 ++++++++++++++++--- scipost/static/scipost/assets/css/_form.scss | 4 +++ 8 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 comments/templates/comments/add_comment.html diff --git a/comments/models.py b/comments/models.py index 82ae4cf7b..b5d00c911 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 b3a51a16b..e50ce6528 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 0759c42ee..ee94826ec 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 204b12d70..711beb89f 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 000000000..780fb54a3 --- /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 213fbd1ad..1c7f2a46e 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 fba5791ba..fb5199bd4 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 60f2289b0..62172b512 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; -- GitLab