cmsmanage/rentals/models.py

46 lines
1.5 KiB
Python

from django.contrib.auth import get_user_model
from django.db import models
class LockerBank(models.Model):
name = models.CharField(max_length=200)
location = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
rows = models.PositiveIntegerField()
columns = models.PositiveIntegerField()
# TODO: add constraint for unique first letter?
def __str__(self):
return f"{self.name} ({self.columns}x{self.rows})"
@property
def initial(self):
return self.name[0].upper()
@property
def iter_doors(self):
for row in range(self.rows):
for column in range(self.columns):
# TODO: filter could be optimized
yield chr(row + ord("A")), column + 1, self.rentals.filter(
row=row, column=column
)
class LockerRental(models.Model):
locker_bank = models.ForeignKey(
LockerBank, on_delete=models.CASCADE, related_name="rentals"
)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
row = models.PositiveIntegerField()
column = models.PositiveIntegerField()
# TODO: add check constraint to ensure that row and column are within locker_bank bounds
# TODO: add unique constraint on (bank, row, column)?
@property
def address(self) -> str:
return f"{self.locker_bank.name}-{self.locker_bank.initial}{chr(self.row + ord('A'))}{self.column}"
def __str__(self):
return f"{self.user}: {self.address}"