move time-related functionality out of views.general
Django CI/CD / test (push) Successful in 58s Details
Django CI/CD / build-and-push (push) Successful in 1m52s Details

This commit is contained in:
Lukáš Kucharczyk 2024-09-04 21:55:22 +02:00
parent 645ffa0dad
commit 98c9c1faee
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
8 changed files with 52 additions and 42 deletions

View File

@ -1,5 +1,13 @@
import re 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): def _safe_timedelta(duration: timedelta | int | None):
@ -70,3 +78,9 @@ def format_duration(
rf"%\d*\.?\d*{pattern}", replacement, formatted_string rf"%\d*\.?\d*{pattern}", replacement, formatted_string
) )
return formatted_string return formatted_string
def local_strftime(datetime: datetime, format: str = datetimeformat) -> str:
return timezone.localtime(datetime).strftime(format)

View File

@ -7,10 +7,10 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from common.time import dateformat, local_strftime
from common.utils import A, Button from common.utils import A, Button
from games.forms import DeviceForm from games.forms import DeviceForm
from games.models import Device from games.models import Device
from games.views.general import dateformat
@login_required @login_required
@ -47,7 +47,7 @@ def list_devices(request: HttpRequest) -> HttpResponse:
[ [
device.name, device.name,
device.get_type_display(), device.get_type_display(),
device.created_at.strftime(dateformat), local_strftime(device.created_at, dateformat),
render_to_string( render_to_string(
"cotton/button_group_sm.html", "cotton/button_group_sm.html",
{ {

View File

@ -7,10 +7,10 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from common.time import dateformat, local_strftime
from common.utils import A, Button, truncate_with_popover from common.utils import A, Button, truncate_with_popover
from games.forms import EditionForm from games.forms import EditionForm
from games.models import Edition, Game from games.models import Edition, Game
from games.views.general import dateformat
@login_required @login_required
@ -75,7 +75,7 @@ def list_editions(request: HttpRequest) -> HttpResponse:
truncate_with_popover(str(edition.platform)), truncate_with_popover(str(edition.platform)),
edition.year_released, edition.year_released,
edition.wikidata, edition.wikidata,
edition.created_at.strftime(dateformat), local_strftime(edition.created_at, dateformat),
render_to_string( render_to_string(
"cotton/button_group_sm.html", "cotton/button_group_sm.html",
{ {

View File

@ -8,18 +8,18 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse 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 common.utils import A, Button, safe_division, truncate_with_popover
from games.forms import GameForm from games.forms import GameForm
from games.models import Edition, Game, Purchase, Session from games.models import Edition, Game, Purchase, Session
from games.views.general import ( from games.views.general import use_custom_redirect
dateformat,
datetimeformat,
durationformat,
durationformat_manual,
timeformat,
use_custom_redirect,
)
@login_required @login_required
@ -75,7 +75,7 @@ def list_games(request: HttpRequest) -> HttpResponse:
), ),
game.year_released, game.year_released,
game.wikidata, game.wikidata,
game.created_at.strftime(dateformat), local_strftime(game.created_at, dateformat),
render_to_string( render_to_string(
"cotton/button_group_sm.html", "cotton/button_group_sm.html",
{ {
@ -175,9 +175,9 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse:
) )
if sessions: 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() 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 = (
playrange_start playrange_start
@ -269,7 +269,7 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse:
"columns": ["Date", "Duration", "Duration (manual)", "Actions"], "columns": ["Date", "Duration", "Duration (manual)", "Actions"],
"rows": [ "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) format_duration(session.duration_calculated, durationformat)
if session.duration_calculated if session.duration_calculated

View File

@ -12,12 +12,6 @@ from common.time import format_duration
from common.utils import safe_division from common.utils import safe_division
from games.models import Edition, Game, Platform, Purchase, Session 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]: def model_counts(request: HttpRequest) -> dict[str, bool]:
return { return {

View File

@ -7,10 +7,11 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from common.time import dateformat, local_strftime
from common.utils import A, Button from common.utils import A, Button
from games.forms import PlatformForm from games.forms import PlatformForm
from games.models import Platform from games.models import Platform
from games.views.general import dateformat, use_custom_redirect from games.views.general import use_custom_redirect
@login_required @login_required
@ -47,7 +48,7 @@ def list_platforms(request: HttpRequest) -> HttpResponse:
[ [
platform.name, platform.name,
platform.group, platform.group,
platform.created_at.strftime(dateformat), local_strftime(platform.created_at, dateformat),
render_to_string( render_to_string(
"cotton/button_group_sm.html", "cotton/button_group_sm.html",
{ {

View File

@ -13,10 +13,11 @@ from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from common.time import dateformat, local_strftime
from common.utils import A, Button, truncate_with_popover from common.utils import A, Button, truncate_with_popover
from games.forms import PurchaseForm from games.forms import PurchaseForm
from games.models import Edition, Purchase 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 @login_required
@ -80,23 +81,23 @@ def list_purchases(request: HttpRequest) -> HttpResponse:
purchase.price, purchase.price,
purchase.price_currency, purchase.price_currency,
purchase.infinite, 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 if purchase.date_refunded
else "-" else "-"
), ),
( (
purchase.date_finished.strftime(dateformat) local_strftime(purchase.date_finished, dateformat)
if purchase.date_finished if purchase.date_finished
else "-" else "-"
), ),
( (
purchase.date_dropped.strftime(dateformat) local_strftime(purchase.date_dropped, dateformat)
if purchase.date_dropped if purchase.date_dropped
else "-" else "-"
), ),
purchase.created_at.strftime(dateformat), local_strftime(purchase.created_at, dateformat),
render_to_string( render_to_string(
"cotton/button_group_sm.html", "cotton/button_group_sm.html",
{ {

View File

@ -8,18 +8,18 @@ from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from django.utils import timezone 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 common.utils import A, Button, truncate_with_popover
from games.forms import SessionForm from games.forms import SessionForm
from games.models import Purchase, Session from games.models import Purchase, Session
from games.views.general import ( from games.views.general import use_custom_redirect
dateformat,
datetimeformat,
durationformat,
durationformat_manual,
timeformat,
use_custom_redirect,
)
@login_required @login_required
@ -64,7 +64,7 @@ def list_sessions(request: HttpRequest) -> HttpResponse:
args=[session.purchase.edition.game.pk], 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) format_duration(session.duration_calculated, durationformat)
if session.duration_calculated if session.duration_calculated