Make organisations natively sortable

This commit is contained in:
Chris Hill-Scott
2021-11-11 14:35:55 +00:00
parent 18dfc44899
commit 0c2b586e40
6 changed files with 45 additions and 21 deletions

View File

@@ -116,7 +116,7 @@ def add_organisation_from_nhs_local_service(service_id):
form = AddNHSLocalOrganisationForm(organisation_choices=[
(organisation.id, organisation.name)
for organisation in AllOrganisations()
for organisation in sorted(AllOrganisations())
if organisation.organisation_type == Organisation.TYPE_NHS_LOCAL
])

View File

@@ -60,3 +60,9 @@ class PaginatedModelList(ModelList):
self.items = response[self.response_key]
self.prev_page = response.get('links', {}).get('prev', None)
self.next_page = response.get('links', {}).get('next', None)
class SortByNameMixin():
def __lt__(self, other):
return self.name.lower() < other.name.lower()

View File

@@ -1,13 +1,18 @@
from flask import abort
from werkzeug.utils import cached_property
from app.models import JSONModel, ModelList, SerialisedModelCollection
from app.models import (
JSONModel,
ModelList,
SerialisedModelCollection,
SortByNameMixin,
)
from app.notify_client.email_branding_client import email_branding_client
from app.notify_client.letter_branding_client import letter_branding_client
from app.notify_client.organisations_api_client import organisations_client
class Organisation(JSONModel):
class Organisation(JSONModel, SortByNameMixin):
TYPE_CENTRAL = 'central'
TYPE_LOCAL = 'local'

View File

@@ -2,7 +2,7 @@ from flask import abort, current_app
from notifications_utils.serialised_model import SerialisedModelCollection
from werkzeug.utils import cached_property
from app.models import JSONModel
from app.models import JSONModel, SortByNameMixin
from app.models.contact_list import ContactLists
from app.models.job import (
ImmediateJobs,
@@ -27,7 +27,7 @@ from app.notify_client.template_folder_api_client import (
from app.utils import get_default_sms_sender
class Service(JSONModel):
class Service(JSONModel, SortByNameMixin):
ALLOWED_PROPERTIES = {
'active',
@@ -78,9 +78,6 @@ class Service(JSONModel):
def from_id(cls, service_id):
return cls(service_api_client.get_service(service_id)['data'])
def __lt__(self, other):
return self.name.lower() < other.name.lower()
@property
def permissions(self):
return self._dict.get('permissions', self.TEMPLATE_TYPES)

View File

@@ -14,7 +14,7 @@
<nav class="browse-list">
<ul>
{% for org in organisations %}
{% for org in organisations|sort %}
<li class="browse-list-item">
<a href="{{ url_for('main.organisation_dashboard', org_id=org.id) }}" class="govuk-link govuk-link--no-visited-state">{{ org.name }}</a>
{% if not org.active %}

View File

@@ -20,9 +20,9 @@ def test_organisation_page_shows_all_organisations(
mocker
):
orgs = [
{'id': '1', 'name': 'Test 1', 'active': True, 'count_of_live_services': 0},
{'id': '2', 'name': 'Test 2', 'active': True, 'count_of_live_services': 1},
{'id': '3', 'name': 'Test 3', 'active': False, 'count_of_live_services': 2},
{'id': 'A3', 'name': 'Test 3', 'active': True, 'count_of_live_services': 0},
{'id': 'B1', 'name': 'Test 1', 'active': True, 'count_of_live_services': 1},
{'id': 'C2', 'name': 'Test 2', 'active': False, 'count_of_live_services': 2},
]
get_organisations = mocker.patch(
@@ -39,15 +39,31 @@ def test_organisation_page_shows_all_organisations(
page.select_one('h1').text
) == "Organisations"
expected_hints = ('0 live services', '1 live service', '2 live services')
for index, org in enumerate(orgs):
assert page.select('.browse-list-item a')[index].text == org['name']
if not org['active']:
assert page.select_one('.table-field-status-default,heading-medium').text == '- archived'
assert normalize_spaces(page.select('.browse-list-hint')[index].text) == (
expected_hints[index]
assert [
(
normalize_spaces(link.text),
normalize_spaces(hint.text),
link['href'],
) for link, hint in zip(
page.select('.browse-list-item a'),
page.select('.browse-list-item .browse-list-hint'),
)
] == [
('Test 1', '1 live service', url_for(
'main.organisation_dashboard', org_id='B1'
)),
('Test 2', '2 live services', url_for(
'main.organisation_dashboard', org_id='C2'
)),
('Test 3', '0 live services', url_for(
'main.organisation_dashboard', org_id='A3'
)),
]
archived = page.select_one('.table-field-status-default.heading-medium')
assert normalize_spaces(archived.text) == '- archived'
assert normalize_spaces(archived.parent.text) == 'Test 2 - archived 2 live services'
assert normalize_spaces(
page.select_one('a.govuk-button--secondary').text
) == 'New organisation'
@@ -276,8 +292,8 @@ def test_nhs_local_can_create_own_organisations(
mocker.patch(
'app.models.organisation.AllOrganisations.client_method',
return_value=[
organisation_json('t1', 'Trust 1', organisation_type='nhs_local'),
organisation_json('t2', 'Trust 2', organisation_type='nhs_local'),
organisation_json('t1', 'Trust 1', organisation_type='nhs_local'),
organisation_json('gp1', 'GP 1', organisation_type='nhs_gp'),
organisation_json('c1', 'Central 1'),
],