diff options
author | Matthew Lemon <y@yulqen.org> | 2024-10-07 15:40:13 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-10-07 15:40:13 +0100 |
commit | 222cd8c0737ea76f33b6a9337316b673f532bcc0 (patch) | |
tree | eddb5ecc6723c541eae6348e0ce1380670d7f566 | |
parent | b821c337590e2653074f3f01c649c180918c1c32 (diff) |
Nice engagement duration text for summary box on detail page
-rw-r--r-- | engagements/templates/engagements/engagement_detail.html | 7 | ||||
-rw-r--r-- | engagements/tests/test_views.py | 33 | ||||
-rw-r--r-- | engagements/utils.py | 14 | ||||
-rw-r--r-- | engagements/views.py | 3 |
4 files changed, 51 insertions, 6 deletions
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 @@ <path stroke-linecap="round" stroke-linejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5A2.25 2.25 0 0 1 21 11.25v7.5m-9-6h.008v.008H12v-.008ZM12 15h.008v.008H12V15Zm0 2.25h.008v.008H12v-.008ZM9.75 15h.008v.008H9.75V15Zm0 2.25h.008v.008H9.75v-.008ZM7.5 15h.008v.008H7.5V15Zm0 2.25h.008v.008H7.5v-.008Zm6.75-4.5h.008v.008h-.008v-.008Zm0 2.25h.008v.008h-.008V15Zm0 2.25h.008v.008h-.008v-.008Zm2.25-4.5h.008v.008H16.5v-.008Zm0 2.25h.008v.008H16.5V15Z"/> </svg> - <span class="text-black">{{ engagement.proposed_start_date|date:'j M Y' }}</span> - {% if engagement.proposed_end_date != engagement.proposed_start_date %} - - {{ engagement.proposed_end_date|date:'j M Y' }} - {% endif %} + <span class="text-black"> + {{ day_duration }} + </span> </div> <!-- Site/Operation --> 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) |