From 98c9c1faee37b8c0edf97370accb1d1635717f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 4 Sep 2024 21:55:22 +0200 Subject: [PATCH] move time-related functionality out of views.general --- common/time.py | 16 +++++++++++++++- games/views/device.py | 4 ++-- games/views/edition.py | 4 ++-- games/views/game.py | 26 +++++++++++++------------- games/views/general.py | 6 ------ games/views/platform.py | 5 +++-- games/views/purchase.py | 13 +++++++------ games/views/session.py | 20 ++++++++++---------- 8 files changed, 52 insertions(+), 42 deletions(-) diff --git a/common/time.py b/common/time.py index 8e2d384..d35d41a 100644 --- a/common/time.py +++ b/common/time.py @@ -1,5 +1,13 @@ import re -from datetime import timedelta +from datetime import datetime, timedelta + +from django.utils import timezone + +dateformat: str = "%d/%m/%Y" +datetimeformat: str = "%d/%m/%Y %H:%M" +timeformat: str = "%H:%M" +durationformat: str = "%2.1H hours" +durationformat_manual: str = "%H hours" def _safe_timedelta(duration: timedelta | int | None): @@ -70,3 +78,9 @@ def format_duration( rf"%\d*\.?\d*{pattern}", replacement, formatted_string ) return formatted_string + + +def local_strftime(datetime: datetime, format: str = datetimeformat) -> str: + return timezone.localtime(datetime).strftime(format) + + diff --git a/games/views/device.py b/games/views/device.py index 3fc1a2b..e55b24b 100644 --- a/games/views/device.py +++ b/games/views/device.py @@ -7,10 +7,10 @@ from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.urls import reverse +from common.time import dateformat, local_strftime from common.utils import A, Button from games.forms import DeviceForm from games.models import Device -from games.views.general import dateformat @login_required @@ -47,7 +47,7 @@ def list_devices(request: HttpRequest) -> HttpResponse: [ device.name, device.get_type_display(), - device.created_at.strftime(dateformat), + local_strftime(device.created_at, dateformat), render_to_string( "cotton/button_group_sm.html", { diff --git a/games/views/edition.py b/games/views/edition.py index a2568cf..616e939 100644 --- a/games/views/edition.py +++ b/games/views/edition.py @@ -7,10 +7,10 @@ from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.urls import reverse +from common.time import dateformat, local_strftime from common.utils import A, Button, truncate_with_popover from games.forms import EditionForm from games.models import Edition, Game -from games.views.general import dateformat @login_required @@ -75,7 +75,7 @@ def list_editions(request: HttpRequest) -> HttpResponse: truncate_with_popover(str(edition.platform)), edition.year_released, edition.wikidata, - edition.created_at.strftime(dateformat), + local_strftime(edition.created_at, dateformat), render_to_string( "cotton/button_group_sm.html", { diff --git a/games/views/game.py b/games/views/game.py index db12442..17efcba 100644 --- a/games/views/game.py +++ b/games/views/game.py @@ -8,18 +8,18 @@ from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.urls import reverse -from common.time import format_duration +from common.time import ( + dateformat, + durationformat, + durationformat_manual, + format_duration, + local_strftime, + timeformat, +) from common.utils import A, Button, safe_division, truncate_with_popover from games.forms import GameForm from games.models import Edition, Game, Purchase, Session -from games.views.general import ( - dateformat, - datetimeformat, - durationformat, - durationformat_manual, - timeformat, - use_custom_redirect, -) +from games.views.general import use_custom_redirect @login_required @@ -75,7 +75,7 @@ def list_games(request: HttpRequest) -> HttpResponse: ), game.year_released, game.wikidata, - game.created_at.strftime(dateformat), + local_strftime(game.created_at, dateformat), render_to_string( "cotton/button_group_sm.html", { @@ -175,9 +175,9 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse: ) if sessions: - playrange_start = sessions.earliest().timestamp_start.strftime("%b %Y") + playrange_start = local_strftime(sessions.earliest().timestamp_start, "%b %Y") latest_session = sessions.latest() - playrange_end = latest_session.timestamp_start.strftime("%b %Y") + playrange_end = local_strftime(latest_session.timestamp_start, "%b %Y") playrange = ( playrange_start @@ -269,7 +269,7 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse: "columns": ["Date", "Duration", "Duration (manual)", "Actions"], "rows": [ [ - f"{session.timestamp_start.strftime(datetimeformat)}{f" — {session.timestamp_end.strftime(timeformat)}" if session.timestamp_end else ""}", + f"{local_strftime(session.timestamp_start)}{f" — {session.timestamp_end.strftime(timeformat)}" if session.timestamp_end else ""}", ( format_duration(session.duration_calculated, durationformat) if session.duration_calculated diff --git a/games/views/general.py b/games/views/general.py index dc0e641..c217087 100644 --- a/games/views/general.py +++ b/games/views/general.py @@ -12,12 +12,6 @@ from common.time import format_duration from common.utils import safe_division from games.models import Edition, Game, Platform, Purchase, Session -dateformat: str = "%d/%m/%Y" -datetimeformat: str = "%d/%m/%Y %H:%M" -timeformat: str = "%H:%M" -durationformat: str = "%2.1H hours" -durationformat_manual: str = "%H hours" - def model_counts(request: HttpRequest) -> dict[str, bool]: return { diff --git a/games/views/platform.py b/games/views/platform.py index 3906510..f6acf30 100644 --- a/games/views/platform.py +++ b/games/views/platform.py @@ -7,10 +7,11 @@ from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.urls import reverse +from common.time import dateformat, local_strftime from common.utils import A, Button from games.forms import PlatformForm from games.models import Platform -from games.views.general import dateformat, use_custom_redirect +from games.views.general import use_custom_redirect @login_required @@ -47,7 +48,7 @@ def list_platforms(request: HttpRequest) -> HttpResponse: [ platform.name, platform.group, - platform.created_at.strftime(dateformat), + local_strftime(platform.created_at, dateformat), render_to_string( "cotton/button_group_sm.html", { diff --git a/games/views/purchase.py b/games/views/purchase.py index 9680948..5407c68 100644 --- a/games/views/purchase.py +++ b/games/views/purchase.py @@ -13,10 +13,11 @@ from django.template.loader import render_to_string from django.urls import reverse from django.utils import timezone +from common.time import dateformat, local_strftime from common.utils import A, Button, truncate_with_popover from games.forms import PurchaseForm from games.models import Edition, Purchase -from games.views.general import dateformat, use_custom_redirect +from games.views.general import use_custom_redirect @login_required @@ -80,23 +81,23 @@ def list_purchases(request: HttpRequest) -> HttpResponse: purchase.price, purchase.price_currency, purchase.infinite, - purchase.date_purchased.strftime(dateformat), + local_strftime(purchase.date_purchased, dateformat), ( - purchase.date_refunded.strftime(dateformat) + local_strftime(purchase.date_refunded, dateformat) if purchase.date_refunded else "-" ), ( - purchase.date_finished.strftime(dateformat) + local_strftime(purchase.date_finished, dateformat) if purchase.date_finished else "-" ), ( - purchase.date_dropped.strftime(dateformat) + local_strftime(purchase.date_dropped, dateformat) if purchase.date_dropped else "-" ), - purchase.created_at.strftime(dateformat), + local_strftime(purchase.created_at, dateformat), render_to_string( "cotton/button_group_sm.html", { diff --git a/games/views/session.py b/games/views/session.py index 0e85639..91049ed 100644 --- a/games/views/session.py +++ b/games/views/session.py @@ -8,18 +8,18 @@ from django.template.loader import render_to_string from django.urls import reverse from django.utils import timezone -from common.time import format_duration +from common.time import ( + dateformat, + durationformat, + durationformat_manual, + format_duration, + local_strftime, + timeformat, +) from common.utils import A, Button, truncate_with_popover from games.forms import SessionForm from games.models import Purchase, Session -from games.views.general import ( - dateformat, - datetimeformat, - durationformat, - durationformat_manual, - timeformat, - use_custom_redirect, -) +from games.views.general import use_custom_redirect @login_required @@ -64,7 +64,7 @@ def list_sessions(request: HttpRequest) -> HttpResponse: args=[session.purchase.edition.game.pk], ), ), - f"{session.timestamp_start.strftime(datetimeformat)}{f" — {session.timestamp_end.strftime(timeformat)}" if session.timestamp_end else ""}", + f"{local_strftime(session.timestamp_start)}{f" — {local_strftime(session.timestamp_end, timeformat)}" if session.timestamp_end else ""}", ( format_duration(session.duration_calculated, durationformat) if session.duration_calculated