From 4a5f0f63ee1f3be220862afdb256f1d72a428891 Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Thu, 16 Jul 2026 09:34:08 +1000 Subject: [PATCH] fix(pricemon): load i18n in _alerts partial and pass alerts to include AlertsPartialView (htmx-polled every 30s from the dashboard) returned 500 because _alerts.html used {% trans %} without {% load i18n %}. Added the load tag so the partial renders correctly. The dashboard's inline {% include 'pricemon/_alerts.html' %} also relied on an undefined 'alerts' context var (DashboardView sets 'active_alerts'), so the alerts panel rendered empty even when alerts existed. Pass alerts=active_alerts explicitly via the include. Add regression tests covering both the dashboard render with an alert and the alerts partial endpoint. --- templates/pricemon/_alerts.html | 1 + templates/pricemon/dashboard.html | 2 +- tests/test_pricemon.py | 32 ++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/templates/pricemon/_alerts.html b/templates/pricemon/_alerts.html index 7d9efdb..4b07962 100644 --- a/templates/pricemon/_alerts.html +++ b/templates/pricemon/_alerts.html @@ -1,3 +1,4 @@ +{% load i18n %} {% if alerts %}

diff --git a/templates/pricemon/dashboard.html b/templates/pricemon/dashboard.html index 633585c..1977ff2 100644 --- a/templates/pricemon/dashboard.html +++ b/templates/pricemon/dashboard.html @@ -45,7 +45,7 @@ hx-get="{% url 'pricemon-alerts-partial' %}" hx-trigger="every 30s" hx-swap="innerHTML"> - {% include 'pricemon/_alerts.html' %} + {% include 'pricemon/_alerts.html' with alerts=active_alerts %}

{% endif %} diff --git a/tests/test_pricemon.py b/tests/test_pricemon.py index ee2bad1..74c8e10 100644 --- a/tests/test_pricemon.py +++ b/tests/test_pricemon.py @@ -3,7 +3,7 @@ from unittest.mock import patch import pytest from django.urls import reverse -from pricemon.models import PriceWatcher +from pricemon.models import PriceAlert, PriceWatcher @pytest.fixture @@ -16,6 +16,16 @@ def watcher(): ) +@pytest.fixture +def alert(watcher): + return PriceAlert.objects.create( + watcher=watcher, + old_price='100.00', + new_price='80.00', + drop_pct='20.00', + ) + + @pytest.mark.django_db class TestPriceMonitorToggle: def test_dashboard_shows_disable_action_for_enabled_watcher(self, client, watcher): @@ -53,3 +63,23 @@ class TestPriceMonitorToggle: response = client.get(reverse('pricemon-toggle-enabled', args=[watcher.pk])) assert response.status_code == 405 + + +@pytest.mark.django_db +class TestPriceMonitorAlertsPartial: + def test_dashboard_renders_with_active_alert(self, client, watcher, alert): + response = client.get(reverse('pricemon-dashboard')) + + assert response.status_code == 200 + body = response.content.decode() + assert 'alert-{}'.format(alert.pk) in body + assert 'Price Drop Alerts' in body + + def test_alerts_partial_renders_i18n_tags(self, client, watcher, alert): + response = client.get(reverse('pricemon-alerts-partial')) + + assert response.status_code == 200 + body = response.content.decode() + assert 'alert-{}'.format(alert.pk) in body + assert 'View' in body and 'Dismiss' in body + assert reverse('pricemon-dismiss-alert', args=[alert.pk]) in body