Scaffold containerized Django/Wagtail app with core features
This commit is contained in:
0
apps/comments/__init__.py
Normal file
0
apps/comments/__init__.py
Normal file
6
apps/comments/apps.py
Normal file
6
apps/comments/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CommentsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.comments"
|
||||
19
apps/comments/forms.py
Normal file
19
apps/comments/forms.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django import forms
|
||||
|
||||
from apps.comments.models import Comment
|
||||
|
||||
|
||||
class CommentForm(forms.ModelForm):
|
||||
honeypot = forms.CharField(required=False)
|
||||
article_id = forms.IntegerField(widget=forms.HiddenInput)
|
||||
parent_id = forms.IntegerField(required=False, widget=forms.HiddenInput)
|
||||
|
||||
class Meta:
|
||||
model = Comment
|
||||
fields = ["author_name", "author_email", "body"]
|
||||
|
||||
def clean_body(self):
|
||||
body = self.cleaned_data["body"]
|
||||
if not body.strip():
|
||||
raise forms.ValidationError("Comment body is required.")
|
||||
return body
|
||||
30
apps/comments/migrations/0001_initial.py
Normal file
30
apps/comments/migrations/0001_initial.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# Generated by Django 5.2.11 on 2026-02-28 11:42
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('blog', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Comment',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('author_name', models.CharField(max_length=100)),
|
||||
('author_email', models.EmailField(max_length=254)),
|
||||
('body', models.TextField(max_length=2000)),
|
||||
('is_approved', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('ip_address', models.GenericIPAddressField(blank=True, null=True)),
|
||||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.articlepage')),
|
||||
('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='comments.comment')),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
apps/comments/migrations/__init__.py
Normal file
0
apps/comments/migrations/__init__.py
Normal file
25
apps/comments/models.py
Normal file
25
apps/comments/models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
article = models.ForeignKey("blog.ArticlePage", on_delete=models.CASCADE, related_name="comments")
|
||||
parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, related_name="replies")
|
||||
author_name = models.CharField(max_length=100)
|
||||
author_email = models.EmailField()
|
||||
body = models.TextField(max_length=2000)
|
||||
is_approved = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
ip_address = models.GenericIPAddressField(null=True, blank=True)
|
||||
|
||||
def clean(self) -> None:
|
||||
if self.parent and self.parent.parent_id is not None:
|
||||
raise ValidationError("Replies cannot be nested beyond one level.")
|
||||
|
||||
def get_absolute_url(self):
|
||||
return f"{self.article.url}#comment-{self.pk}"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Comment by {self.author_name}"
|
||||
7
apps/comments/urls.py
Normal file
7
apps/comments/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from apps.comments.views import CommentCreateView
|
||||
|
||||
urlpatterns = [
|
||||
path("post/", CommentCreateView.as_view(), name="comment_post"),
|
||||
]
|
||||
42
apps/comments/views.py
Normal file
42
apps/comments/views.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.contrib import messages
|
||||
from django.core.cache import cache
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.views import View
|
||||
|
||||
from apps.blog.models import ArticlePage
|
||||
from apps.comments.forms import CommentForm
|
||||
from apps.comments.models import Comment
|
||||
|
||||
|
||||
class CommentCreateView(View):
|
||||
def post(self, request):
|
||||
ip = (request.META.get("HTTP_X_FORWARDED_FOR") or request.META.get("REMOTE_ADDR", "")).split(",")[0].strip()
|
||||
key = f"comment-rate:{ip}"
|
||||
count = cache.get(key, 0)
|
||||
if count >= 3:
|
||||
return HttpResponse(status=429)
|
||||
cache.set(key, count + 1, timeout=60)
|
||||
|
||||
form = CommentForm(request.POST)
|
||||
article = get_object_or_404(ArticlePage, pk=request.POST.get("article_id"))
|
||||
if not article.comments_enabled:
|
||||
return HttpResponse(status=404)
|
||||
|
||||
if form.is_valid():
|
||||
if form.cleaned_data.get("honeypot"):
|
||||
return redirect(f"{article.url}?commented=1")
|
||||
comment = form.save(commit=False)
|
||||
comment.article = article
|
||||
parent_id = form.cleaned_data.get("parent_id")
|
||||
if parent_id:
|
||||
comment.parent = Comment.objects.filter(pk=parent_id, article=article).first()
|
||||
comment.ip_address = ip or None
|
||||
comment.save()
|
||||
messages.success(request, "Your comment is awaiting moderation")
|
||||
return redirect(f"{article.url}?commented=1")
|
||||
|
||||
messages.error(request, "Please correct the form errors")
|
||||
return redirect(article.url)
|
||||
20
apps/comments/wagtail_hooks.py
Normal file
20
apps/comments/wagtail_hooks.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from wagtail.admin.ui.tables import BooleanColumn
|
||||
from wagtail.snippets.models import register_snippet
|
||||
from wagtail.snippets.views.snippets import SnippetViewSet
|
||||
|
||||
from apps.comments.models import Comment
|
||||
|
||||
|
||||
class CommentViewSet(SnippetViewSet):
|
||||
model = Comment
|
||||
icon = "comment"
|
||||
list_display = ["author_name", "article", BooleanColumn("is_approved"), "created_at"]
|
||||
list_filter = ["is_approved"]
|
||||
search_fields = ["author_name", "body"]
|
||||
add_to_admin_menu = True
|
||||
|
||||
def get_queryset(self, request):
|
||||
return super().get_queryset(request).select_related("article", "parent")
|
||||
|
||||
|
||||
register_snippet(CommentViewSet)
|
||||
Reference in New Issue
Block a user