Fix #2626: Duplicate scheduled year for single day events#2934
Fix #2626: Duplicate scheduled year for single day events#2934iampujan wants to merge 1 commit intopython:mainfrom
Conversation
- 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.
There was a problem hiding this comment.
Pull request overview
Fixes duplicate year rendering for single-day events in the shared events time tag template, and adds a unit test intended to prevent regressions (Issue #2626).
Changes:
- Remove duplicate
dt_start|date:"Y"rendering intime_tag.htmlwhennext_time.single_dayis true. - Add a template-focused unit test to assert single-year rendering for single-day future-year events.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| apps/events/templates/events/includes/time_tag.html | Removes the extra year <span> in the single-day rendering path. |
| apps/events/tests/test_templates.py | Adds a new test to validate the single-day template doesn’t duplicate the year. |
Comments suppressed due to low confidence (2)
apps/events/tests/test_templates.py:32
next_time.dt_start/dt_endare timezone-aware datetimes in normal operation (model fields usetimezone.now()and managers callconvert_dt_to_aware). Here the mock uses naivedatetime.datetime(...), which can lead to inconsistent output depending on timezone settings. Consider usingdjango.utils.timezone.make_aware(...)(ortzinfo=datetime.UTC) for the mock datetimes to match production behavior.
self.dt_start = datetime.datetime(future_year, 5, 25, 12, 0)
self.dt_end = datetime.datetime(future_year, 5, 25, 14, 0)
self.single_day = True
self.all_day = False
self.valid_dt_end = True
apps/events/tests/test_templates.py:51
- The assertion counts year occurrences by splitting on an exact whitespace/indentation pattern. This is brittle (minor template formatting changes will break the test) and doesn’t actually verify “visible” vs attribute text robustly. Prefer parsing the rendered HTML (e.g., BeautifulSoup) and asserting the year appears exactly once as a text node within the
span#start-..., or use a whitespace-tolerant regex like>\s*YEAR\s*</span>.
# The year should only appear visibly once in the output (not counting the datetime ISO tag).
year_str = str(future_year)
# Using string splitting to exclude the `datetime="2027...` occurrence by checking how many times
# it appears wrapped with whitespace or inside a span.
visible_occurrences = rendered.split(">\n " + year_str + "\n </span>")
self.assertEqual(
len(visible_occurrences) - 1,
1,
f"Expected the visible span containing {year_str} to appear exactly once, but it was duplicated: {rendered}"
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from django.template.loader import render_to_string | ||
| from django.test import TestCase | ||
|
|
||
| from ..models import Event, EventLocation, Calendar |
There was a problem hiding this comment.
This test uses a relative import (from ..models ...), but the repo’s Ruff config bans all relative imports (flake8-tidy-imports). This will fail lint; switch to absolute imports (e.g., from apps.events.models import ...). Also EventLocation is imported but unused, which will trigger F401.
| from ..models import Event, EventLocation, Calendar | |
| from apps.events.models import Event, Calendar |
| # Create a single day event in the future to trigger the year rendering condition | ||
| future_year = datetime.date.today().year + 1 | ||
|
|
||
| calendar = Calendar.objects.create(name="Test Calendar") |
There was a problem hiding this comment.
Calendar.slug is a required field (non-null, non-blank). Creating a Calendar with only name will raise an integrity/validation error when the test runs. Provide a unique slug (and optionally creator like other tests do).
| calendar = Calendar.objects.create(name="Test Calendar") | |
| calendar = Calendar.objects.create( | |
| name="Test Calendar", | |
| slug="test-calendar-time-tag-single-day-event", | |
| ) |
Fixes #2626. Removed duplicate render for
dt_start|date:"Y"intime_tag.htmlwhensingle_dayis true. Includes a unit test to enforce single-year rendering for same day events.