Skip to content

Commit 51055f1

Browse files
iampujanCopilotjefftriplettJacobCoffee
authored
Fix #2626: Duplicate scheduled year for single day events (#2934)
* Fix #2626: Duplicate scheduled year for single day events - Removes redundant rendering of next_time.dt_start year block inside time_tag.html for single_day events - Adds unit tests to ensure the DOM output for single day events generates the year precisely once. * Update apps/events/tests/test_templates.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update apps/events/tests/test_templates.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address Copilot PR reviews: timezone-aware tests, robust assertions, Ruff compliance (Issue #2626) * Format test_templates.py with ruff --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Jeff Triplett <jeff.triplett@gmail.com> Co-authored-by: Jacob Coffee <jacob@z7x.org>
1 parent bc43257 commit 51055f1

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎apps/events/templates/events/includes/time_tag.html‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
{{ next_time.dt_start|date:"Y" }}
55
</span>
66

7-
<span id="start-{{ object.id }}"{% if scheduled_end_this_year %} class="say-no-more"{% endif %}>
8-
{{ next_time.dt_start|date:"Y" }}
9-
</span>
10-
117
{% if not next_time.all_day %}
128
{{ next_time.dt_start|date:"fA"|lower }} {{ next_time.dt_start|date:"e" }}
139
{% if next_time.valid_dt_end %} – {{ next_time.dt_end|date:"fA"|lower }}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import datetime
2+
import re
3+
4+
from django.template.loader import render_to_string
5+
from django.test import TestCase
6+
from django.utils import timezone
7+
8+
from apps.events.models import Calendar, Event
9+
10+
11+
class TimeTagTemplateTests(TestCase):
12+
def test_single_day_event_year_rendering(self):
13+
"""
14+
Verify that a single-day event does not render the year twice (Issue #2626).
15+
"""
16+
future_year = timezone.now().date().year + 1
17+
18+
calendar = Calendar.objects.create(
19+
name="Test Calendar",
20+
slug="test-calendar-time-tag-single-day-event",
21+
)
22+
event = Event.objects.create(
23+
title="Single Day Future Event",
24+
description="Test event",
25+
calendar=calendar,
26+
)
27+
28+
# Use timezone-aware datetimes to match the project's USE_TZ = True setting.
29+
class MockTime:
30+
def __init__(self):
31+
self.dt_start = datetime.datetime(future_year, 5, 25, 12, 0, tzinfo=datetime.UTC)
32+
self.dt_end = datetime.datetime(future_year, 5, 25, 14, 0, tzinfo=datetime.UTC)
33+
self.single_day = True
34+
self.all_day = False
35+
self.valid_dt_end = True
36+
37+
context = {
38+
"next_time": MockTime(),
39+
"scheduled_start_this_year": False,
40+
"scheduled_end_this_year": False,
41+
"object": event,
42+
}
43+
44+
rendered = render_to_string("events/includes/time_tag.html", context)
45+
46+
# Count visible year occurrences inside <span> tags using a regex.
47+
# This avoids brittle exact-whitespace matching and ignores the
48+
# year inside the <time datetime="..."> ISO attribute.
49+
year_str = str(future_year)
50+
year_in_span = re.findall(r"<span[^>]*>\s*" + re.escape(year_str) + r"\s*</span>", rendered)
51+
self.assertEqual(
52+
len(year_in_span),
53+
1,
54+
f"Expected the year {year_str} to appear in exactly one <span>, but found {len(year_in_span)}: {rendered}",
55+
)

0 commit comments

Comments
 (0)