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`.
This commit is contained in:
Chris Hill-Scott
2022-02-01 11:34:39 +00:00
parent 41e74d249c
commit 82b3a96a83
2 changed files with 2 additions and 9 deletions

View File

@@ -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

View File

@@ -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))