kers/events/models.py

64 lines
1.6 KiB
Python
Raw Normal View History

2020-07-21 20:24:45 +00:00
from django.db import models
2020-07-21 22:51:40 +00:00
from users.models import CustomUser
2020-07-21 20:24:45 +00:00
class Event(models.Model):
2020-07-22 02:08:30 +00: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 02:24:01 +00:00
2020-07-22 02:08:30 +00:00
def __str__(self):
return f"{self.date} {self.TIME_SLOTS[self.time]}"
2020-07-21 20:24:45 +00:00
2020-07-22 02:24:01 +00:00
def time_str(self):
2020-07-22 02:45:01 +00:00
return Event.TIME_SLOTS[self.time]
2020-07-22 02:24:01 +00: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 20:24:45 +00:00
class EventRegistration(models.Model):
2020-07-22 02:08:30 +00:00
INTERESTED = "I"
ADMITTED = "A"
2020-07-22 02:45:01 +00:00
REGISTRATION_STATE = {
INTERESTED: "Interested",
ADMITTED: "Admitted",
}
2020-07-22 02:02:13 +00:00
event = models.ForeignKey(Event, on_delete=models.CASCADE)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
2020-07-22 02:45:01 +00:00
state = models.CharField(max_length=1, choices=REGISTRATION_STATE.items())
2020-07-22 02:02:13 +00:00
2020-07-22 14:16:33 +00:00
class Meta:
constraints = [
models.UniqueConstraint(
fields=["event", "user"], name="user can only register once per event"
)
]
2020-07-22 02:02:13 +00:00
def __str__(self):
2020-07-22 02:13:20 +00:00
return f"Reservation[{self.user.username}:{self.event.date}:{self.state}]"
2020-07-22 02:24:01 +00:00
def state_str(self):
2020-07-22 02:45:01 +00:00
return EventRegistration.REGISTRATION_STATE[self.state]