42 lines
1.2 KiB
Python
42 lines
1.2 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 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)
|
||
|
|
||
|
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}]"
|