Skip to content

Commit

Permalink
Merge pull request #2049 from laws-africa/comments
Browse files Browse the repository at this point in the history
Comments in admin area for documents
  • Loading branch information
longhotsummer authored Sep 23, 2024
2 parents 8506c15 + 2fb5d77 commit babe698
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions peachjam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"corsheaders",
"django_htmx",
"django_recaptcha",
"django_comments",
]

MIDDLEWARE = [
Expand Down
9 changes: 9 additions & 0 deletions peachjam/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.contrib.auth.models import Group
from django.db.models import signals
from django.dispatch import receiver
from django_comments.models import Comment
from django_comments.signals import comment_will_be_posted

from peachjam.models import CoreDocument, ExtractedCitation, SourceFile, Work
from peachjam.tasks import update_extracted_citations_for_a_work
Expand Down Expand Up @@ -66,3 +68,10 @@ def add_saved_document_permissions(sender, instance, created, **kwargs):
if created:
all_users_group, _ = Group.objects.get_or_create(name="AllUsers")
instance.groups.add(all_users_group)


@receiver(comment_will_be_posted, sender=Comment)
def before_comment_posted(sender, comment, request, **kwargs):
# prevent unauthorized comments
if not comment.user or not comment.user.is_staff:
return False
16 changes: 16 additions & 0 deletions peachjam/templates/admin/_comments_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% load comments %}
{% get_comment_form for object as comment_form %}
<form action="{% comment_form_target %}"
method="post"
id="comment-form"
hx-post="{% comment_form_target %}"
hx-target="#comments-list">
{% csrf_token %}
<input type="hidden"
name="next"
value="{% url 'comment_form' app_label model_name object.pk %}"/>
{{ comment_form.content_type }}
{{ comment_form.object_pk }}
{{ comment_form.timestamp }}
{{ comment_form.security_hash }}
</form>
2 changes: 2 additions & 0 deletions peachjam/templates/admin/_comments_form_combo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% include 'admin/_comments_list.html' %}
<div hx-swap-oob="comments-form-wrapper">{% include 'admin/_comments_form.html' %}</div>
19 changes: 19 additions & 0 deletions peachjam/templates/admin/_comments_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load comments i18n humanize %}
<ol class="list-unstyled">
{% get_comment_list for object as comment_list %}
{% for comment in comment_list %}
<li class="mb-3">
<div>
<b>{{ comment.user }}</b> {{ comment.submit_date|naturaltime }}:
</div>
<div>{{ comment.comment|linebreaks }}</div>
</li>
{% endfor %}
</ol>
<textarea name="comment"
rows="3"
maxlength="3000"
required
class="form-control mt-3 mb-2"
form="comment-form"></textarea>
<button type="submit" class="btn btn-primary" form="comment-form">{% trans "Add comment" %}</button>
20 changes: 19 additions & 1 deletion peachjam/templates/admin/change_form.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
{% extends "admin/change_form.html" %}
{% load i18n %}
{% load i18n comments humanize static %}
{% block extra_actions %}
{{ block.super }}
{% if PEACHJAM_SETTINGS.editor_help_link and adminform.model_admin.help_topic %}
<a target="_blank"
href="{{ PEACHJAM_SETTINGS.editor_help_link }}{{ adminform.model_admin.help_topic }}"
class="btn btn-block btn-info">{% trans 'Help' %}</a>
{% endif %}
{% block comments %}
{% if original %}
<hr/>
<!-- actual comments, this is updated with hx-swap when a comment is posted -->
<div id="comments-list">{% include 'admin/_comments_list.html' with object=original %}</div>
{% endif %}
{% endblock %}
{% endblock %}
{% block content %}
<script defer src="{% static 'js/app-prod.js' %}"></script>
{{ block.super }}
{% if original %}
<!-- actual comment form, outside of main form; this will be updated with hx-oob-swap when a comment is posted -->
<div id="comments-form-wrapper">
{% include 'admin/_comments_form.html' with object=original app_label=opts.app_label model_name=opts.model_name %}
</div>
{% endif %}
{% endblock %}
14 changes: 14 additions & 0 deletions peachjam/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
UserProfileDetailView,
WorkAutocomplete,
)
from peachjam.views.comments import comment_form_view
from peachjam.views.generic_views import CSRFTokenView
from peachjam.views.metabase_stats import MetabaseStatsView

Expand Down Expand Up @@ -330,6 +331,19 @@
),
# django-markdown-editor
path("martor/", include("martor.urls")),
path(
"comments/",
include(
[
path("", include("django_comments.urls")),
path(
"form/<str:app_label>/<str:model_name>/<int:pk>",
comment_form_view,
name="comment_form",
),
]
),
),
]

if settings.DEBUG:
Expand Down
21 changes: 21 additions & 0 deletions peachjam/views/comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.contrib.contenttypes.models import ContentType
from django.http import Http404
from django.shortcuts import get_object_or_404, render


def comment_form_view(request, app_label, model_name, pk):
"""Renders a list of comments for the admin view, refreshed using htmx after posting a comment."""
if not request.user.is_authenticated or not request.user.is_staff:
raise Http404()

content_type = get_object_or_404(ContentType, app_label=app_label, model=model_name)
obj = get_object_or_404(content_type.model_class(), pk=pk)
return render(
request,
"admin/_comments_form_combo.html",
{
"object": obj,
"model_name": model_name,
"app_label": app_label,
},
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
"django-background-tasks-updated>=1.2.8",
"django-ckeditor>=6.4.2",
"django-compressor>=3.1",
"django-contrib-comments>=2.2.0",
"django-cors-headers>=4.3.1",
"django-countries-plus>=1.3.2",
"django-debug-toolbar>=4.4",
Expand Down

0 comments on commit babe698

Please sign in to comment.