Skip to content

Commit

Permalink
Limit to one event at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Dec 2, 2023
1 parent 6c5548a commit ff403ab
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone
from django.utils.html import format_html
Expand Down Expand Up @@ -138,7 +139,7 @@ def update_current_qr_i(self, i: int):

@property
def members(self):
"""Returns all members of the team, it's a related manager so to convert to queryset use .all() or filter it."""
"""Returns all members of the team, it's a related manager so to convert to queryset use .all() or filter it."""
return User.objects.filter(team=str(self.id))

@property
Expand All @@ -158,7 +159,7 @@ def invites(self):

@property
def qr_len(self):
"""Amount of codes the team has completed (+1) (assuming no skips)"""
"""Amount of codes the team has completed (+1) (assuming no skips)"""
return int(self.current_qr_i) + 1

def __str__(self):
Expand Down Expand Up @@ -217,6 +218,21 @@ def current_hunt(cls):
return cls.objects.get(start__lt=timezone.now(), end__gt=timezone.now())
except cls.DoesNotExist:
return None

def clean(self):
"""
Due to how this was designed, it is not possible to have multiple hunts running at the same time.
This method prevents that from happening.
"""
overlapping_events = self.objects.filter(
start_date__lte=self.start,
end_date__gte=self.end
).exclude(pk=self.pk)

if overlapping_events.exists():
raise ValidationError('This event overlaps with existing events. Please choose a different time. Or Delete the other event.')


class Meta:
constraints = [
models.CheckConstraint(
Expand All @@ -235,10 +251,7 @@ class Meta:
name="form_in_ending_text",
),
# Ensure there isn't a different hunt running in that timespan
models.CheckConstraint(
check=models.Q(start__gt=models.F("end")),
name="no_overlapping_hunts", # todo check if this works
),

]


Expand Down

0 comments on commit ff403ab

Please sign in to comment.