From f85a9306a62cd5ef51861e79ee078ec587767542 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Thu, 16 Aug 2018 13:40:24 +0100 Subject: [PATCH] Move brandings sort into email_branding_client --- app/main/views/email_branding.py | 4 ++-- app/notify_client/email_branding_client.py | 7 ++++-- tests/conftest.py | 25 +++++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/main/views/email_branding.py b/app/main/views/email_branding.py index 1f4942beb..4f55b1e7f 100644 --- a/app/main/views/email_branding.py +++ b/app/main/views/email_branding.py @@ -26,10 +26,10 @@ from app.utils import get_cdn_domain, user_is_platform_admin @login_required @user_is_platform_admin def email_branding(): - brandings = email_branding_client.get_all_email_branding() + brandings = email_branding_client.get_all_email_branding(sort_key='name') form = ServiceSelectEmailBranding() - email_brandings = sorted(get_branding_as_value_and_label(brandings), key=lambda tup: tup[1].lower()) + email_brandings = get_branding_as_value_and_label(brandings) form.email_branding.choices = email_brandings + [('None', 'Create a new email branding')] if form.validate_on_submit(): diff --git a/app/notify_client/email_branding_client.py b/app/notify_client/email_branding_client.py index 59ebbde3f..ae2375144 100644 --- a/app/notify_client/email_branding_client.py +++ b/app/notify_client/email_branding_client.py @@ -9,8 +9,11 @@ class EmailBrandingClient(NotifyAdminAPIClient): def get_email_branding(self, branding_id): return self.get(url='/email-branding/{}'.format(branding_id)) - def get_all_email_branding(self): - return self.get(url='/email-branding')['email_branding'] + def get_all_email_branding(self, sort_key=None): + brandings = self.get(url='/email-branding')['email_branding'] + if sort_key and sort_key in brandings[0]: + brandings.sort(key=lambda branding: branding[sort_key].lower()) + return brandings def get_letter_email_branding(self): return self.get(url='/dvla_organisations') diff --git a/tests/conftest.py b/tests/conftest.py index 1f57d80e5..0e81119c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2430,14 +2430,23 @@ def mock_send_already_registered_email(mocker): @pytest.fixture(scope='function') def mock_get_all_email_branding(mocker): - def _get_all_email_branding(): - return [ - {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png'}, - {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'}, - {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png'}, - {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png'}, - {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png'}, - ] + def _get_all_email_branding(sort_key=None): + if sort_key: + return [ + {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png'}, + {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'}, + {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png'}, + {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png'}, + {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png'}, + ] + else: + return [ + {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png'}, + {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'}, + {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png'}, + {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png'}, + {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png'}, + ] return mocker.patch( 'app.email_branding_client.get_all_email_branding', side_effect=_get_all_email_branding