From bd941387c2cec12f3a84f2f05f7c85eb7af39af3 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 24 Oct 2024 12:09:31 +0200 Subject: [PATCH] adds timezone for clubs --- tournaments/models/club.py | 7 ++++++- tournaments/models/match.py | 9 ++++++--- tournaments/models/tournament.py | 9 +++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tournaments/models/club.py b/tournaments/models/club.py index d2b2f29..1a13cfe 100644 --- a/tournaments/models/club.py +++ b/tournaments/models/club.py @@ -1,4 +1,5 @@ from django.db import models +from zoneinfo import available_timezones import uuid class Club(models.Model): @@ -15,7 +16,11 @@ class Club(models.Model): zip_code = models.CharField(max_length=10, null=True, blank=True) latitude = models.FloatField(null=True, blank=True) longitude = models.FloatField(null=True, blank=True) - + timezone = models.CharField( + max_length=50, + choices=[(tz, tz) for tz in sorted(available_timezones())], + default='CET' + ) court_count = models.IntegerField(default=2) broadcast_code = models.CharField(max_length=10, null=True, blank=True, unique=True) diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 4e4695d..6a0d954 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -2,7 +2,8 @@ from django.db import models from tournaments.models import group_stage from . import Round, GroupStage, FederalMatchCategory from django.utils import timezone, formats -from datetime import timedelta +from datetime import datetime, timedelta + import uuid from ..utils.extensions import format_seconds @@ -80,8 +81,10 @@ class Match(models.Model): def formatted_start_date(self): if self.start_date: - timezoned_datetime = timezone.localtime(self.start_date) - return formats.date_format(timezoned_datetime, format='H:i') + timezone = self.tournament().timezone() + local_start = self.start_date.astimezone(timezone) + # timezoned_datetime = timezone.localtime(self.start_date) + return formats.date_format(local_start, format='H:i') # return formats.date_format(self.start_date, format='H:i') else: return '' diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index bb1c6e3..9e47813 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -1,3 +1,4 @@ +from zoneinfo import ZoneInfo from django.db import models from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -7,6 +8,8 @@ from . import Event, TournamentPayment, FederalMatchCategory, FederalCategory, F import uuid from django.utils import timezone, formats from datetime import datetime, timedelta +from zoneinfo import ZoneInfo + from shared.cryptography import encryption_util from ..utils.extensions import plural_format @@ -129,6 +132,12 @@ class Tournament(models.Model): components.append(self.name) return (' ').join(components) + def timezone(self): + tz = 'CET' + if self.event and self.event.club: + tz = self.event.club.timezone + return ZoneInfo(tz) + def level(self): if self.federal_level_category == 0: return "Anim."