kers/events/models.py

82 lines
2.4 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-08-04 03:32:02 +02:00
date = models.DateField()
capacity = models.IntegerField(default=6)
note = models.TextField(max_length=1024, default="")
responsible_person = models.ForeignKey(CustomUser, null=True, on_delete=models.SET_NULL)
def __str__(self):
return f"{self.date}"
class TimeSlot(models.Model):
2020-07-22 04:08:30 +02:00
MORNING, AFTERNOON, EVENING = range(3)
TIME_SLOTS = {
2020-07-22 17:43:50 +02:00
MORNING: "voormiddag",
AFTERNOON: "namiddag",
EVENING: "avond",
2020-07-22 04:08:30 +02:00
}
time = models.IntegerField(choices=TIME_SLOTS.items(), default=MORNING)
2020-08-04 03:32:02 +02:00
event = models.ForeignKey(Event, on_delete=models.CASCADE)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["time", "event"], name="no_duplicate_timeslots"
),
]
2020-07-22 04:24:01 +02:00
2020-07-22 04:08:30 +02:00
def __str__(self):
2020-08-04 03:32:02 +02:00
return f"{self.event.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-08-04 03:32:02 +02:00
return TimeSlot.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
2020-08-04 03:32:02 +02:00
assert len(registrations) == 1, "Registrations should be unique per user and timeslot"
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"
2020-07-25 02:33:42 +02:00
ADMITTED = "A"
2020-07-22 04:45:01 +02:00
REGISTRATION_STATE = {
2020-07-22 17:43:50 +02:00
INTERESTED: "op wachtlijst",
ADMITTED: "bevestigd",
2020-07-22 04:45:01 +02:00
}
2020-08-04 03:32:02 +02:00
time_slot = models.ForeignKey(TimeSlot, on_delete=models.CASCADE)
2020-07-22 04:02:13 +02:00
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(
2020-08-04 03:32:02 +02:00
fields=["time_slot", "user"], name="register_only_once_per_timeslot"
),
2020-07-22 16:16:33 +02:00
]
2020-07-22 04:02:13 +02:00
def __str__(self):
2020-08-04 03:32:02 +02:00
return f"Reservation[{self.user.username}:{self.time_slot.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]