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
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)

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.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",
{

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.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",
{

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.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

View File

@ -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 {

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.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",
{

View File

@ -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",
{

View File

@ -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