Private
Public Access
Add hardening and deployment: admin dashboard, security headers, install docs
Adds an admin-only dashboard (apps/core) surfacing feeds that are failing to fetch, with error messages, subscriber counts, and a one-click retry that resets backoff and re-queues the fetch. Hardens config/settings/prod.py with the remaining standard Django security settings (HSTS preload, nosniff, referrer-policy, frame-options, SameSite cookies) plus a Content-Security-Policy header via django-csp, as a second layer behind nh3's HTML sanitization of feed content. Adds docs/install.md (pip + gunicorn + systemd + nginx, SQLite and Postgres both covered) plus the systemd units and nginx snippet it references, LICENSE (AGPL-3.0-or-later, matching pyproject.toml), and an updated README. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7577110238
commit
3f6474ce94
16 files changed
+1226
-6
No files matched your search
+60
-1
@@ -1 +1,60 @@
|
||||
# Create your tests here.
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
|
||||
from apps.feeds.models import Feed, Subscription
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_requires_staff(client, django_user_model):
|
||||
django_user_model.objects.create_user("regular", password="pw-regular-123")
|
||||
client.login(username="regular", password="pw-regular-123")
|
||||
|
||||
response = client.get(reverse("core:dashboard"))
|
||||
assert response.status_code == 302
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_lists_erroring_feeds(client, django_user_model):
|
||||
admin = django_user_model.objects.create_user("admin", password="pw-admin-123", is_staff=True)
|
||||
client.login(username="admin", password="pw-admin-123")
|
||||
|
||||
ok_feed = Feed.objects.create(feed_url="https://ok.example.com/feed", title="OK Feed")
|
||||
broken_feed = Feed.objects.create(
|
||||
feed_url="https://broken.example.com/feed",
|
||||
title="Broken Feed",
|
||||
last_fetch_status=Feed.STATUS_ERROR,
|
||||
fetch_error_count=3,
|
||||
last_error_message="Connection timed out",
|
||||
)
|
||||
Subscription.objects.create(user=admin, feed=ok_feed)
|
||||
Subscription.objects.create(user=admin, feed=broken_feed)
|
||||
|
||||
response = client.get(reverse("core:dashboard"))
|
||||
assert response.status_code == 200
|
||||
assert b"Broken Feed" in response.content
|
||||
assert b"Connection timed out" in response.content
|
||||
assert b"OK Feed" not in response.content
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_retry_feed_resets_backoff_and_requeues(client, django_user_model):
|
||||
django_user_model.objects.create_user("admin", password="pw-admin-123", is_staff=True)
|
||||
client.login(username="admin", password="pw-admin-123")
|
||||
|
||||
feed = Feed.objects.create(
|
||||
feed_url="https://broken.example.com/feed",
|
||||
fetch_error_count=5,
|
||||
last_fetch_status=Feed.STATUS_ERROR,
|
||||
)
|
||||
|
||||
with (
|
||||
patch("apps.core.views.reset_feed_backoff.delay") as mock_reset,
|
||||
patch("apps.core.views.fetch_feed.delay") as mock_fetch,
|
||||
):
|
||||
response = client.post(reverse("core:retry_feed", args=[feed.id]))
|
||||
|
||||
assert response.status_code == 302
|
||||
mock_reset.assert_called_once_with(feed.id)
|
||||
mock_fetch.assert_called_once_with(feed.id)
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "core"
|
||||
|
||||
urlpatterns = [
|
||||
path("dashboard/", views.dashboard, name="dashboard"),
|
||||
path("dashboard/feeds/<int:feed_id>/retry/", views.retry_feed, name="retry_feed"),
|
||||
]
|
||||
+43
-1
@@ -1 +1,43 @@
|
||||
# Create your views here.
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.db.models import Count, Q
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from apps.articles.models import Article
|
||||
from apps.feeds.models import Feed, Subscription
|
||||
from apps.feeds.tasks import fetch_feed, reset_feed_backoff
|
||||
|
||||
is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff)
|
||||
|
||||
|
||||
@is_admin
|
||||
def dashboard(request):
|
||||
feeds = Feed.objects.annotate(subscriber_count=Count("subscriptions"))
|
||||
erroring_feeds = feeds.filter(
|
||||
Q(last_fetch_status=Feed.STATUS_ERROR) | Q(fetch_error_count__gt=0)
|
||||
).order_by("-fetch_error_count")
|
||||
|
||||
stats = {
|
||||
"feed_count": feeds.count(),
|
||||
"active_feed_count": feeds.filter(is_active=True).count(),
|
||||
"error_feed_count": erroring_feeds.count(),
|
||||
"subscription_count": Subscription.objects.count(),
|
||||
"article_count": Article.objects.count(),
|
||||
}
|
||||
|
||||
return render(
|
||||
request,
|
||||
"core/dashboard.html",
|
||||
{"stats": stats, "erroring_feeds": erroring_feeds},
|
||||
)
|
||||
|
||||
|
||||
@is_admin
|
||||
@require_POST
|
||||
def retry_feed(request, feed_id):
|
||||
feed = get_object_or_404(Feed, pk=feed_id)
|
||||
reset_feed_backoff.delay(feed.id)
|
||||
fetch_feed.delay(feed.id)
|
||||
messages.success(request, f"Queued an immediate retry for '{feed.title or feed.feed_url}'.")
|
||||
return redirect("core:dashboard")
|
||||
Reference in new issue
Block a user