from django.conf import settings from django.core.exceptions import ValidationError from django.db import models from django.utils import timezone from bookings.models import Reservation class CheckIn(models.Model): class Source(models.TextChoices): QR_SCAN = "qr_scan", "QR scan" MANUAL = "manual", "Manual" reservation = models.OneToOneField( Reservation, on_delete=models.PROTECT, related_name="check_in", ) checked_in_at = models.DateTimeField(default=timezone.now) checked_in_by = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="checkins", ) source = models.CharField(max_length=20, choices=Source.choices, default=Source.QR_SCAN) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-checked_in_at"] indexes = [ models.Index(fields=["checked_in_at"]), models.Index(fields=["checked_in_by"]), ] def __str__(self): return f"Check-in for reservation {self.reservation_id}" def clean(self): super().clean() if self.reservation_id and not self.reservation.is_confirmed: raise ValidationError({"reservation": "Only confirmed reservations can be checked in."}) def save(self, *args, **kwargs): self.full_clean() return super().save(*args, **kwargs)