kers/events/models.py

75 lines
2 KiB
Python
Raw Normal View History

2020-07-21 22:24:45 +02:00
from django.db import models
2020-07-22 00:51:40 +02:00
from users.models import CustomUser
2020-07-21 22:24:45 +02:00
class Event(models.Model):
2020-07-22 04:08:30 +02:00
MORNING, AFTERNOON, EVENING = range(3)
TIME_SLOTS = {
MORNING: "Voormiddag",
AFTERNOON: "Namiddag",
EVENING: "Avond",
}
date = models.DateField()
time = models.IntegerField(choices=TIME_SLOTS.items(), default=MORNING)
capacity = models.IntegerField(default=6)
2020-07-22 04:24:01 +02:00
2020-07-22 04:08:30 +02:00
def __str__(self):
return f"{self.date} {self.TIME_SLOTS[self.time]}"
2020-07-21 22:24:45 +02:00
2020-07-22 04:24:01 +02:00
def time_str(self):
2020-07-22 04:45:01 +02:00
return Event.TIME_SLOTS[self.time]
2020-07-22 04:24:01 +02:00
def count_with_status(self, status):
return self.eventregistration_set.filter(state=status).count()
def count_interested(self):
return self.count_with_status(EventRegistration.INTERESTED)
def count_admitted(self):
return self.count_with_status(EventRegistration.ADMITTED)
def registration_of(self, user):
2020-07-22 17:28:39 +02:00
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]
2020-07-21 22:24:45 +02:00
class EventRegistration(models.Model):
2020-07-22 04:08:30 +02:00
INTERESTED = "I"
ADMITTED = "A"
2020-07-22 04:45:01 +02:00
REGISTRATION_STATE = {
INTERESTED: "Op wachtlijst",
ADMITTED: "Bevestigd",
2020-07-22 04:45:01 +02:00
}
2020-07-22 04:02:13 +02:00
event = models.ForeignKey(Event, on_delete=models.CASCADE)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
2020-07-22 04:45:01 +02:00
state = models.CharField(max_length=1, choices=REGISTRATION_STATE.items())
2020-07-22 04:02:13 +02:00
2020-07-22 16:16:33 +02:00
class Meta:
constraints = [
models.UniqueConstraint(
fields=["event", "user"], name="user can only register once per event"
)
]
2020-07-22 04:02:13 +02:00
def __str__(self):
2020-07-22 04:13:20 +02:00
return f"Reservation[{self.user.username}:{self.event.date}:{self.state}]"
2020-07-22 04:24:01 +02:00
def state_str(self):
2020-07-22 04:45:01 +02:00
return EventRegistration.REGISTRATION_STATE[self.state]