From 222cd8c0737ea76f33b6a9337316b673f532bcc0 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 7 Oct 2024 15:40:13 +0100 Subject: Nice engagement duration text for summary box on detail page --- .../templates/engagements/engagement_detail.html | 7 ++--- engagements/tests/test_views.py | 33 ++++++++++++++++++++-- engagements/utils.py | 14 +++++++++ engagements/views.py | 3 ++ 4 files changed, 51 insertions(+), 6 deletions(-) (limited to 'engagements') diff --git a/engagements/templates/engagements/engagement_detail.html b/engagements/templates/engagements/engagement_detail.html index fbf6735..c122e11 100644 --- a/engagements/templates/engagements/engagement_detail.html +++ b/engagements/templates/engagements/engagement_detail.html @@ -81,10 +81,9 @@ - {{ engagement.proposed_start_date|date:'j M Y' }} - {% if engagement.proposed_end_date != engagement.proposed_start_date %} - - {{ engagement.proposed_end_date|date:'j M Y' }} - {% endif %} + + {{ day_duration }} + diff --git a/engagements/tests/test_views.py b/engagements/tests/test_views.py index 017b96f..01d73c7 100644 --- a/engagements/tests/test_views.py +++ b/engagements/tests/test_views.py @@ -6,12 +6,41 @@ from django.test import RequestFactory from django.urls import reverse from engagements import models, views -from engagements.models import EngagementStrategy, RegulatoryCycle, Organisation -from engagements.utils import populate_database +from engagements.models import EngagementStrategy, Organisation, RegulatoryCycle +from engagements.utils import duration_formatter, populate_database pytestmark = pytest.mark.django_db +def test_single_day_string(): + """test date formatting for the summary box on the detail page""" + d1 = datetime.date(2024, 10, 10) + d2 = datetime.date(2024, 10, 10) + duration_str = duration_formatter(d1, d2) + assert duration_str == "10 October 2024 (1 day)" + +def test_multi_duration_string(): + """test date formatting for the summary box on the detail page""" + d1 = datetime.date(2024, 10, 10) + d2 = datetime.date(2024, 10, 12) + duration_str = duration_formatter(d1, d2) + assert duration_str == "10-12 October 2024 (3 days)" + +def test_multi_duration_string_longer(): + """test date formatting for the summary box on the detail page""" + d1 = datetime.date(2024, 10, 1) + d2 = datetime.date(2024, 10, 12) + duration_str = duration_formatter(d1, d2) + assert duration_str == "01-12 October 2024 (12 days)" + +def test_multi_duration_over_month_boundary_string(): + """test date formatting for the summary box on the detail page""" + d1 = datetime.date(2024, 9, 30) + d2 = datetime.date(2024, 10, 1) + duration_str = duration_formatter(d1, d2) + assert duration_str == "30 September - 01 October 2024 (2 days)" + + @pytest.fixture def test_data(): return populate_database() diff --git a/engagements/utils.py b/engagements/utils.py index 3b414e9..b80e74d 100644 --- a/engagements/utils.py +++ b/engagements/utils.py @@ -18,6 +18,20 @@ from instruments.models import Instrument, SubInstrument from myuser.models import Team, TeamUser +def duration_formatter(d1: datetime.date, d2: datetime.date) -> str: + trip_delta = d2 - d1 + if trip_delta.days == 0: + # this is a single day event + return f"{d1.strftime("%d %B %Y")} (1 day)" + else: + # if months are different... + if d1.month != d2.month: + # we need to format this differently + return f"{d1.strftime("%d %B")} - {d2.strftime("%d %B %Y")} ({trip_delta.days +1} days)" + else: + return f"{d1.strftime("%d")}-{d2.strftime("%d %B %Y")} ({trip_delta.days + 1} days)" + + def populate_database(): out = defaultdict(list) fake = Faker(locale="en_GB") diff --git a/engagements/views.py b/engagements/views.py index 60bec47..86f886d 100644 --- a/engagements/views.py +++ b/engagements/views.py @@ -18,6 +18,7 @@ from .forms import ( EngagementStrategyCreateForm, ) from .models import Engagement, EngagementEffort, EngagementStrategy, EngagementType, Organisation +from .utils import duration_formatter def effort_detail(request, effort_id): @@ -66,6 +67,7 @@ def engagement_detail(request, pk): effort_total = sum(e.effort_total_hours() for e in effort) effort_planned = sum(e.effort_total_planned_hours() for e in effort) effort_actual = sum(e.effort_actual() for e in effort) + day_duration_str = duration_formatter(engagement.proposed_start_date, engagement.proposed_end_date) context = { "engagement": engagement, "subinstruments": subinstruments, @@ -73,6 +75,7 @@ def engagement_detail(request, pk): "effort_total": effort_total, "effort_planned": effort_planned, "effort_actual": effort_actual, + "day_duration": day_duration_str, "dscs": dscs, } return render(request, "engagements/engagement_detail.html", context) -- cgit v1.2.3