47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from django.db import models
|
|
from model_utils.models import TimeStampedModel
|
|
|
|
from users.models import CustomUser
|
|
|
|
|
|
class Event(models.Model):
|
|
start_year = models.IntegerField(default=2020)
|
|
|
|
def __str__(self):
|
|
return f"{self.start_year}-{self.start_year + 1}"
|
|
|
|
def count_registrations(self):
|
|
return self.eventregistration_set.count()
|
|
|
|
def count_registrations_accepted(self):
|
|
return self.eventregistration_set.filter(accepted=True).count()
|
|
|
|
def registration_of(self, user):
|
|
if not user.is_authenticated:
|
|
return None
|
|
|
|
registrations = self.eventregistration_set.filter(user=user).all()
|
|
if not registrations:
|
|
return None
|
|
assert (
|
|
len(registrations) == 1
|
|
), "Registrations should be unique per user and event"
|
|
return registrations[0]
|
|
|
|
|
|
class EventRegistration(TimeStampedModel):
|
|
event = models.ForeignKey(Event, on_delete=models.CASCADE)
|
|
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
|
|
# A user is by default valid.
|
|
# The board can then check if a user has indeed been active in any way and they can invalidate the membership
|
|
accepted = models.BooleanField(default=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["event", "user"], name="register_only_once_per_event"
|
|
),
|
|
]
|
|
|
|
def __str__(self):
|
|
return f"Reservation[{self.user.username}:{self.event.start_year}]"
|