kers/events/models.py

64 lines
1.6 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)
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: "Interested",
ADMITTED: "Admitted",
}
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]