From ff723e019d0d5e205bf03a126dba9add9f329504 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 16 Feb 2022 22:44:54 -0500 Subject: [PATCH] [rentals] Simplify locker index view/template rendering Also create locker info instances when a new locker unit is created --- pyproject.toml | 3 +- rentals/models.py | 39 +++++++++++--------- rentals/templates/rentals/lockers.dj.html | 45 ++++++++--------------- rentals/views.py | 5 +-- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b6349de..21ace98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,10 +29,11 @@ server = [ [tool.black] [tool.djlint] +profile="django" extension = ".dj.html" indent = 2 blank_line_after_tag = "load,extends" -ignore = "T003,H017,H030,H031" +ignore = "T003,H017,H021,H030,H031" [[tool.pdm.source]] url = "https://pypi.org/simple" diff --git a/rentals/models.py b/rentals/models.py index 84b9119..2d7fec8 100644 --- a/rentals/models.py +++ b/rentals/models.py @@ -1,5 +1,5 @@ from django.core import validators -from django.db import models +from django.db import models, transaction from membershipworks.models import Member @@ -29,6 +29,17 @@ class LockerUnit(models.Model): rows = models.PositiveIntegerField(default=5) columns = models.PositiveIntegerField(default=2) + def save(self, *args, **kwargs): + if self._state.adding: + # Create LockerInfo for each locker + with transaction.atomic(): + super().save(self, *args, **kwargs) + for column in range(self.columns): + for row in range(self.rows): + self.lockers.create(column=column + 1, row=row + 1) + else: + super().save(self, *args, **kwargs) + class Meta: # TODO: add constraint to check for letter overlaps constraints = [ @@ -47,20 +58,6 @@ class LockerUnit(models.Model): def number_for_locker(self, column: int, row: int) -> int: return row + self.first_number + (self.columns - column - 1) * self.rows - def iter_doors(self): - for row in range(self.rows): - for column in range(self.columns): - # TODO: filter could be optimized - (locker, _) = self.lockers.get_or_create( - locker_unit=self, row=row, column=column - ) - - yield ( - self.letter_for_column(column), - self.number_for_locker(column, row), - locker, - ) - # TODO: add check constraint to ensure that column and number are within locker_unit bounds class LockerInfo(models.Model): @@ -92,11 +89,17 @@ class LockerInfo(models.Model): ) ] + @property + def letter(self) -> str: + return self.locker_unit.letter_for_column(self.column) + + @property + def number(self) -> int: + return self.locker_unit.number_for_locker(self.column, self.row) + @property def address(self) -> str: - letter = self.locker_unit.letter_for_column(self.column) - number = self.locker_unit.number_for_locker(self.column, self.row) - return f"{letter}{number}" + return f"{self.letter}{self.number}" def __str__(self): return f"{self.locker_unit}-{self.address} [{self.renter}]" diff --git a/rentals/templates/rentals/lockers.dj.html b/rentals/templates/rentals/lockers.dj.html index 9ef3b30..4625504 100644 --- a/rentals/templates/rentals/lockers.dj.html +++ b/rentals/templates/rentals/lockers.dj.html @@ -25,22 +25,6 @@ gap: 0.5rem; } - .locker[data-columns="2"] { - grid-template-columns: repeat(2, min-content); - } - .locker[data-columns="4"] { - grid-template-columns: repeat(4, min-content); - } - .locker[data-columns="6"] { - grid-template-columns: repeat(6, min-content); - } - .locker[data-columns="8"] { - grid-template-columns: repeat(8, min-content); - } - .locker[data-columns="10"] { - grid-template-columns: repeat(10, min-content); - } - .locker .door { background-color: hsl(24, 100%, 25%); border: 0.2rem solid black; @@ -63,21 +47,22 @@
{{ bank.location }}
{% for unit, lockers in units.items %} -
- {% for row, column, locker, form in lockers %} -