start implementing time machine mode (thread local)

This commit is contained in:
Mira Weller
2024-04-30 21:49:57 +02:00
parent 0b05eb34f4
commit 100528ad0f
5 changed files with 101 additions and 13 deletions
+14 -13
View File
@@ -76,6 +76,7 @@ from pretix.helpers.thumb import get_thumbnail
from ..settings import settings_hierarkey
from .organizer import Organizer, Team
from ...presale.timemachine import time_machine_now
logger = logging.getLogger(__name__)
@@ -243,11 +244,11 @@ class EventMixin:
Is true, when ``presale_end`` is set and in the past.
"""
if self.effective_presale_end:
return now() > self.effective_presale_end
return time_machine_now() > self.effective_presale_end
elif self.date_to:
return now() > self.date_to
return time_machine_now() > self.date_to
else:
return now().astimezone(self.timezone).date() > self.date_from.astimezone(self.timezone).date()
return time_machine_now().astimezone(self.timezone).date() > self.date_from.astimezone(self.timezone).date()
@property
def effective_presale_start(self):
@@ -267,7 +268,7 @@ class EventMixin:
Is true, when ``presale_end`` is not set or in the future and ``presale_start`` is not
set or in the past.
"""
if self.effective_presale_start and now() < self.effective_presale_start:
if self.effective_presale_start and time_machine_now() < self.effective_presale_start:
return False
return not self.presale_has_ended
@@ -315,11 +316,11 @@ class EventMixin:
q_variation = (
Q(active=True)
& Q(sales_channels__contains=channel)
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& Q(Q(available_from__isnull=True) | Q(available_from__lte=time_machine_now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=time_machine_now()))
& Q(item__active=True)
& Q(Q(item__available_from__isnull=True) | Q(item__available_from__lte=now()))
& Q(Q(item__available_until__isnull=True) | Q(item__available_until__gte=now()))
& Q(Q(item__available_from__isnull=True) | Q(item__available_from__lte=time_machine_now()))
& Q(Q(item__available_until__isnull=True) | Q(item__available_until__gte=time_machine_now()))
& Q(Q(item__category__isnull=True) | Q(item__category__is_addon=False))
& Q(item__sales_channels__contains=channel)
& Q(item__require_bundling=False)
@@ -694,7 +695,7 @@ class Event(EventMixin, LoggedModel):
@property
def presale_has_ended(self):
if self.has_subevents:
return self.presale_end and now() > self.presale_end
return self.presale_end and time_machine_now() > self.presale_end
else:
return super().presale_has_ended
@@ -1187,8 +1188,8 @@ class Event(EventMixin, LoggedModel):
)
).filter(
Q(active=True) & Q(is_public=True) & (
Q(Q(date_to__isnull=True) & Q(date_from__gte=now() - timedelta(hours=24)))
| Q(date_to__gte=now() - timedelta(hours=24))
Q(Q(date_to__isnull=True) & Q(date_from__gte=time_machine_now() - timedelta(hours=24)))
| Q(date_to__gte=time_machine_now() - timedelta(hours=24))
)
) # order_by doesn't make sense with I18nField
if ordering in ("date_ascending", "date_descending"):
@@ -1508,7 +1509,7 @@ class SubEvent(EventMixin, LoggedModel):
disabled_items=Coalesce(
Subquery(
SubEventItem.objects.filter(
Q(disabled=True) | Q(available_from__gt=now()) | Q(available_until__lt=now()),
Q(disabled=True) | Q(available_from__gt=time_machine_now()) | Q(available_until__lt=time_machine_now()),
subevent=OuterRef('pk'),
).order_by().values('subevent').annotate(items=GroupConcat('item_id', delimiter=',')).values('items'),
output_field=models.TextField(),
@@ -1519,7 +1520,7 @@ class SubEvent(EventMixin, LoggedModel):
disabled_vars=Coalesce(
Subquery(
SubEventItemVariation.objects.filter(
Q(disabled=True) | Q(available_from__gt=now()) | Q(available_until__lt=now()),
Q(disabled=True) | Q(available_from__gt=time_machine_now()) | Q(available_until__lt=time_machine_now()),
subevent=OuterRef('pk'),
).order_by().values('subevent').annotate(items=GroupConcat('variation_id', delimiter=',')).values('items'),
output_field=models.TextField(),