From 82b3a96a833618133e6a441d0527d0a605a5b104 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 1 Feb 2022 11:34:39 +0000 Subject: [PATCH 1/2] Use SortByName mixin where possible Implementing `__lt__` makes objects sortable. Rather than reimplementing `__lt__` each time we want to make an object sortable by name, we can inherit from the extant `SortByNameMixin`. --- app/broadcast_areas/models.py | 8 ++------ app/models/organisation.py | 3 --- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index d9286988a..48f30d8f7 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -8,21 +8,17 @@ from rtreelib import Rect from werkzeug.utils import cached_property from app.formatters import square_metres_to_square_miles +from app.models import SortByNameMixin from .populations import CITY_OF_LONDON from .repo import BroadcastAreasRepository, rtree_index -class SortableMixin: +class SortableMixin(SortByNameMixin): def __repr__(self): return f'{self.__class__.__name__}(<{self.id}>)' - def __lt__(self, other): - # Implementing __lt__ means any classes inheriting from this - # method are sortable - return self.name < other.name - def __eq__(self, other): return self.id == other.id diff --git a/app/models/organisation.py b/app/models/organisation.py index cb9aa53ac..79e185d41 100644 --- a/app/models/organisation.py +++ b/app/models/organisation.py @@ -64,9 +64,6 @@ class Organisation(JSONModel, SortByNameMixin): return cls({}) return cls(organisations_client.get_organisation(org_id)) - def __lt__(self, other): - return self.name.lower() < other.name.lower() - @classmethod def from_domain(cls, domain): return cls(organisations_client.get_organisation_by_domain(domain)) From e918e2497bd508a5d18ba4715f2d62316a4e4ddb Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 1 Feb 2022 11:36:30 +0000 Subject: [PATCH 2/2] Rename mixin to clarify purpose `SortableMixin` no longer has anything to do with sorting, except that it inherits from the similarly-named `SortByNameMixin`. This commit: - renames it to describe its main purpose: doing equality (and identity) checks based on the `id` attribute - makes it no longer inherit from `SortByNameMixin`, so that the two can be used independently or combined --- app/broadcast_areas/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index 48f30d8f7..d3663df48 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -14,7 +14,7 @@ from .populations import CITY_OF_LONDON from .repo import BroadcastAreasRepository, rtree_index -class SortableMixin(SortByNameMixin): +class IdEqualityMixin: def __repr__(self): return f'{self.__class__.__name__}(<{self.id}>)' @@ -75,7 +75,7 @@ class BaseBroadcastArea(ABC): return max(500, min(estimated_bleed, 5000)) -class BroadcastArea(BaseBroadcastArea, SortableMixin): +class BroadcastArea(BaseBroadcastArea, IdEqualityMixin, SortByNameMixin): def __init__(self, row): self.id, self.name, self._count_of_phones, self.library_id = row @@ -220,7 +220,7 @@ class CustomBroadcastAreas(SerialisedModelCollection): ) -class BroadcastAreaLibrary(SerialisedModelCollection, SortableMixin, GetItemByIdMixin): +class BroadcastAreaLibrary(SerialisedModelCollection, SortByNameMixin, IdEqualityMixin, GetItemByIdMixin): model = BroadcastArea