2022-01-27 14:55:27 -05:00
|
|
|
from django.core import validators
|
2022-01-24 22:03:10 -05:00
|
|
|
from django.db import models
|
|
|
|
|
2022-02-16 16:06:51 -05:00
|
|
|
from membershipworks.models import Member
|
|
|
|
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
class LockerBank(models.Model):
|
2022-01-27 14:55:27 -05:00
|
|
|
"""A set of locker units, placed together"""
|
|
|
|
|
2022-01-24 22:03:10 -05:00
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
location = models.CharField(max_length=200)
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-01-27 14:55:27 -05:00
|
|
|
return f"{self.name} ({self.units.count()} units)"
|
2022-01-24 22:03:10 -05:00
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
|
|
|
|
class LockerUnit(models.Model):
|
|
|
|
"""A standalone set of lockers"""
|
|
|
|
|
|
|
|
bank = models.ForeignKey(
|
|
|
|
LockerBank, on_delete=models.SET_NULL, related_name="units", null=True
|
|
|
|
)
|
|
|
|
index = models.PositiveIntegerField()
|
|
|
|
first_letter = models.CharField(
|
|
|
|
max_length=1, validators=[validators.RegexValidator("[A-Z]")], unique=True
|
|
|
|
)
|
|
|
|
first_number = models.PositiveIntegerField()
|
|
|
|
rows = models.PositiveIntegerField(default=5)
|
|
|
|
columns = models.PositiveIntegerField(default=2)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
# TODO: add constraint to check for letter overlaps
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(fields=["bank", "index"], name="unique_bank_index")
|
|
|
|
]
|
|
|
|
ordering = ["index"]
|
|
|
|
|
2022-01-27 16:28:26 -05:00
|
|
|
def __str__(self):
|
|
|
|
last_letter = chr(ord(self.first_letter) + self.columns - 1)
|
|
|
|
last_number = self.first_number + self.columns * self.rows
|
2022-01-28 15:59:54 -05:00
|
|
|
return f"{self.bank.name} (Unit {last_letter}{self.first_number}-{self.first_letter}{last_number})"
|
2022-01-27 16:28:26 -05:00
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
def letter_for_column(self, column: int) -> str:
|
|
|
|
return chr(column + ord(self.first_letter))
|
|
|
|
|
|
|
|
def number_for_locker(self, column: int, row: int) -> int:
|
2022-01-28 15:59:54 -05:00
|
|
|
return row + self.first_number + (self.columns - column - 1) * self.rows
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
def iter_doors(self):
|
|
|
|
for row in range(self.rows):
|
|
|
|
for column in range(self.columns):
|
|
|
|
# TODO: filter could be optimized
|
2022-02-16 16:06:51 -05:00
|
|
|
(locker, _) = self.lockers.get_or_create(
|
|
|
|
locker_unit=self, row=row, column=column
|
|
|
|
)
|
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
yield (
|
|
|
|
self.letter_for_column(column),
|
|
|
|
self.number_for_locker(column, row),
|
2022-02-16 16:06:51 -05:00
|
|
|
locker,
|
2022-01-24 23:37:04 -05:00
|
|
|
)
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
|
2022-01-27 14:55:27 -05:00
|
|
|
# TODO: add check constraint to ensure that column and number are within locker_unit bounds
|
2022-02-16 16:06:51 -05:00
|
|
|
class LockerInfo(models.Model):
|
|
|
|
"""Information about a single locker"""
|
2022-01-27 14:55:27 -05:00
|
|
|
|
|
|
|
locker_unit = models.ForeignKey(
|
2022-02-16 16:06:51 -05:00
|
|
|
LockerUnit, on_delete=models.CASCADE, related_name="lockers"
|
2022-01-24 23:37:04 -05:00
|
|
|
)
|
2022-01-24 22:03:10 -05:00
|
|
|
column = models.PositiveIntegerField()
|
2022-01-27 14:55:27 -05:00
|
|
|
row = models.PositiveIntegerField()
|
2022-02-16 16:06:51 -05:00
|
|
|
blind_code = models.CharField(
|
|
|
|
max_length=5,
|
|
|
|
help_text="Stamped on some keys. Usually D###A.",
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
bitting_code = models.CharField(
|
|
|
|
max_length=5,
|
|
|
|
help_text="National Disc Tumbler, depths 1-4. Read bow-to-tip.",
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
renter = models.ForeignKey(
|
|
|
|
Member, on_delete=models.CASCADE, null=True, blank=True, db_constraint=False
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=["locker_unit", "column", "row"], name="unique_locker_info"
|
|
|
|
)
|
|
|
|
]
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self) -> str:
|
2022-01-27 14:55:27 -05:00
|
|
|
letter = self.locker_unit.letter_for_column(self.column)
|
|
|
|
number = self.locker_unit.number_for_locker(self.column, self.row)
|
2022-01-28 17:03:55 -05:00
|
|
|
return f"{letter}{number}"
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-02-16 16:06:51 -05:00
|
|
|
return f"{self.locker_unit}-{self.address} [{self.renter}]"
|