Add links to comments admin

This commit is contained in:
Gabriel Augendre 2020-08-18 19:51:54 +02:00
parent 261120275d
commit 0a7a956ae0
3 changed files with 20 additions and 5 deletions

View file

@ -19,7 +19,16 @@ class ArticleManager(models.Manager):
return super().get_queryset().filter(page__isnull=True) return super().get_queryset().filter(page__isnull=True)
class Article(models.Model): class AdminUrlMixin:
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse(
"admin:%s_%s_change" % (content_type.app_label, content_type.model),
args=(self.id,),
)
class Article(AdminUrlMixin, models.Model):
DRAFT = "draft" DRAFT = "draft"
PUBLISHED = "published" PUBLISHED = "published"
STATUS_CHOICES = [ STATUS_CHOICES = [
@ -92,7 +101,7 @@ class Page(Article):
ordering = ["position", "-published_at"] ordering = ["position", "-published_at"]
class Comment(models.Model): class Comment(AdminUrlMixin, models.Model):
username = models.CharField( username = models.CharField(
max_length=255, help_text="Will be displayed with your comment." max_length=255, help_text="Will be displayed with your comment."
) )
@ -116,3 +125,6 @@ class Comment(models.Model):
class Meta: class Meta:
ordering = ["created_at"] ordering = ["created_at"]
def get_absolute_url(self):
return self.article.get_absolute_url() + "#" + str(self.id)

View file

@ -14,12 +14,12 @@
<section class="comments"> <section class="comments">
<h2>Comments</h2> <h2>Comments</h2>
{% for comment in comments %} {% for comment in comments %}
<article class="comment"> <article id="{{ comment.id }}" class="comment">
<p class="metadata"> <p class="metadata">
<span class="username">{{ comment.username }}</span> | <span class="username">{{ comment.username }}</span> |
<time datetime="{{ comment.created_at|date:'c' }}"> <time datetime="{{ comment.created_at|date:'c' }}">
{{ comment.created_at|date:"DATETIME_FORMAT" }} {{ comment.created_at|date:"DATETIME_FORMAT" }}
</time> </time> {% include "articles/admin_link_snippet.html" with article=comment %}
</p> </p>
<p class="content"> <p class="content">
{{ comment.content|linebreaksbr }} {{ comment.content|linebreaksbr }}
@ -28,7 +28,7 @@
{% empty %} {% empty %}
No reaction yet, write your own! No reaction yet, write your own!
{% endfor %} {% endfor %}
<form action="{% url 'create-comment' slug=article.slug %}" method="post"> <form id="comment-form" action="{% url 'create-comment' slug=article.slug %}" method="post">
{% csrf_token %} {% csrf_token %}
<table> <table>
{{ form.as_table }} {{ form.as_table }}

View file

@ -74,6 +74,9 @@ class ArticleDetailView(FormMixin, generic.DetailView):
return self.form_invalid(form) return self.form_invalid(form)
def form_invalid(self, form): def form_invalid(self, form):
messages.error(
self.request, "Your comment couldn't be saved, see the form below."
)
return super().form_invalid(form) return super().form_invalid(form)
def form_valid(self, form): def form_valid(self, form):