diff --git a/app/main/forms.py b/app/main/forms.py index 4cc423401..3be4b71d4 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -782,7 +782,7 @@ class RadioFieldWithRequiredMessage(RadioField): raise ValueError(self.required_message) -class ServiceSetBranding(StripWhitespaceForm): +class ServiceSetEmailBranding(StripWhitespaceForm): branding_style = RadioFieldWithNoneOption( 'Branding style', @@ -793,20 +793,25 @@ class ServiceSetBranding(StripWhitespaceForm): DEFAULT = (FieldWithNoneOption.NONE_OPTION_VALUE, 'GOV.UK') - def __init__(self, all_email_brandings, current_email_branding): + def __init__(self, all_branding_options, current_branding): - super().__init__(branding_style=current_email_branding) + super().__init__(branding_style=current_branding) self.branding_style.choices = sorted( - all_email_brandings + [self.DEFAULT], + all_branding_options + [self.DEFAULT], key=lambda branding: ( - branding[0] != current_email_branding, + branding[0] != current_branding, branding[0] is not self.DEFAULT[0], branding[1].lower(), ), ) +class ServiceSetLetterBranding(ServiceSetEmailBranding): + # form is the same, but instead of GOV.UK we have None as a valid option + DEFAULT = (FieldWithNoneOption.NONE_OPTION_VALUE, 'None') + + class ServicePreviewBranding(StripWhitespaceForm): branding_style = HiddenFieldWithNoneOption('branding_style') @@ -881,27 +886,6 @@ class CreateOrUpdateOrganisation(StripWhitespaceForm): name = StringField('Name', validators=[DataRequired()]) -class LetterBranding(StripWhitespaceForm): - - def __init__(self, choices=[], *args, **kwargs): - super().__init__(*args, **kwargs) - self.dvla_org_id.choices = list(sorted( - choices, - key=lambda choice: ( - choice[0] != kwargs.get('dvla_org_id'), - choice[0] != '001', - choice[1], - ), - )) - - dvla_org_id = RadioField( - 'Which logo should this service’s letter have?', - validators=[ - DataRequired() - ] - ) - - class EmailFieldInWhitelist(EmailField, StripWhitespaceStringField): pass @@ -976,7 +960,7 @@ class ChooseTemplateType(StripWhitespaceForm): ]) -class SearchTemplatesForm(StripWhitespaceForm): +class SearchByNameForm(StripWhitespaceForm): search = SearchField('Search by name') diff --git a/app/main/views/conversation.py b/app/main/views/conversation.py index abe71fc3a..65016bca3 100644 --- a/app/main/views/conversation.py +++ b/app/main/views/conversation.py @@ -6,7 +6,7 @@ from notifications_utils.template import SMSPreviewTemplate from app import current_service, notification_api_client, service_api_client from app.main import main -from app.main.forms import SearchTemplatesForm +from app.main.forms import SearchByNameForm from app.utils import user_has_permissions @@ -50,7 +50,7 @@ def conversation_reply( templates=templates, show_search_box=(len(templates) > 7), template_type='sms', - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm(), notification_id=notification_id, ) diff --git a/app/main/views/email_branding.py b/app/main/views/email_branding.py index 338de3c77..22b29325d 100644 --- a/app/main/views/email_branding.py +++ b/app/main/views/email_branding.py @@ -3,7 +3,7 @@ from flask_login import login_required from app import email_branding_client from app.main import main -from app.main.forms import SearchTemplatesForm, ServiceUpdateEmailBranding +from app.main.forms import SearchByNameForm, ServiceUpdateEmailBranding from app.s3_client.s3_logo_client import ( TEMP_TAG, delete_email_temp_file, @@ -24,8 +24,7 @@ def email_branding(): return render_template( 'views/email-branding/select-branding.html', email_brandings=brandings, - search_form=SearchTemplatesForm(), - show_search_box=len(brandings) > 9, + search_form=SearchByNameForm(), agreement_info=AgreementInfo, ) diff --git a/app/main/views/index.py b/app/main/views/index.py index 75e8baab9..c68296f9a 100644 --- a/app/main/views/index.py +++ b/app/main/views/index.py @@ -14,7 +14,7 @@ from notifications_utils.template import HTMLEmailTemplate from app import email_branding_client from app.main import main -from app.main.forms import FieldWithNoneOption, SearchTemplatesForm +from app.main.forms import FieldWithNoneOption, SearchByNameForm from app.main.views.sub_navigation_dictionaries import features_nav from app.utils import AgreementInfo, get_logo_cdn_domain @@ -74,7 +74,7 @@ def pricing(): (cc, country['names'], country['billable_units']) for cc, country in INTERNATIONAL_BILLING_RATES.items() ], key=lambda x: x[0]), - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm(), agreement_info=AgreementInfo.from_current_user(), ) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index ed8c5a5a4..6d3b51de3 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -31,12 +31,11 @@ from app.main.forms import ( ConfirmPasswordForm, FreeSMSAllowance, InternationalSMSForm, - LetterBranding, LinkOrganisationsForm, OrganisationTypeForm, RenameServiceForm, RequestToGoLiveForm, - SearchTemplatesForm, + SearchByNameForm, ServiceContactDetailsForm, ServiceDataRetentionEditForm, ServiceDataRetentionForm, @@ -45,7 +44,8 @@ from app.main.forms import ( ServiceLetterContactBlockForm, ServicePreviewBranding, ServiceReplyToEmailForm, - ServiceSetBranding, + ServiceSetEmailBranding, + ServiceSetLetterBranding, ServiceSmsSenderForm, ServiceSwitchChannelForm, SMSPrefixForm, @@ -54,7 +54,6 @@ from app.main.forms import ( from app.utils import ( AgreementInfo, email_safe, - get_logo_cdn_domain, user_has_permissions, user_is_gov_user, user_is_platform_admin, @@ -759,9 +758,9 @@ def set_free_sms_allowance(service_id): def service_set_email_branding(service_id): email_branding = email_branding_client.get_all_email_branding() - form = ServiceSetBranding( - all_email_brandings=get_branding_as_value_and_label(email_branding), - current_email_branding=current_service.email_branding_id, + form = ServiceSetEmailBranding( + all_branding_options=get_branding_as_value_and_label(email_branding), + current_branding=current_service.email_branding_id, ) if form.validate_on_submit(): @@ -774,9 +773,7 @@ def service_set_email_branding(service_id): return render_template( 'views/service-settings/set-email-branding.html', form=form, - branding_dict=get_branding_as_dict(email_branding), - search_form=SearchTemplatesForm(), - show_search_box=(len(email_branding) > 6) + search_form=SearchByNameForm() ) @@ -805,23 +802,24 @@ def service_preview_email_branding(service_id): @main.route("/services//service-settings/set-letter-branding", methods=['GET', 'POST']) @login_required @user_is_platform_admin -def set_letter_branding(service_id): +def service_set_letter_branding(service_id): + letter_branding = letter_branding_client.get_all_letter_branding() - form = LetterBranding( - choices=letter_branding_client.get_letter_branding().items(), - dvla_org_id=current_service.dvla_organisation, + form = ServiceSetLetterBranding( + all_branding_options=get_branding_as_value_and_label(letter_branding), + current_branding=current_service.letter_branding_id, ) if form.validate_on_submit(): current_service.update( - dvla_organisation=form.dvla_org_id.data + letter_branding=form.branding_style.data ) return redirect(url_for('.service_settings', service_id=service_id)) return render_template( 'views/service-settings/set-letter-branding.html', form=form, - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm() ) @@ -958,15 +956,6 @@ def get_branding_as_value_and_label(email_branding): ] -def get_branding_as_dict(email_branding): - return { - branding['id']: { - 'logo': 'https://{}/{}'.format(get_logo_cdn_domain(), branding['logo']), - 'colour': branding['colour'] - } for branding in email_branding - } - - def convert_dictionary_to_wtforms_choices_format(dictionary, value, label): return [ (item[value], item[label]) for item in dictionary diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 2e0831cf1..c277e2892 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -22,7 +22,7 @@ from app.main.forms import ( EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, - SearchTemplatesForm, + SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, @@ -149,7 +149,7 @@ def choose_template(service_id, template_type='all', template_folder_id=None): ), template_nav_items=get_template_nav_items(template_folder_id), template_type=template_type, - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm(), templates_and_folders_form=templates_and_folders_form, move_to_children=templates_and_folders_form.move_to.children(), option_hints=option_hints @@ -348,7 +348,7 @@ def choose_template_to_copy( ), template_folder_path=service.get_template_folder_path(from_folder), from_service=service, - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm(), ) else: @@ -358,7 +358,7 @@ def choose_template_to_copy( Service(service) for service in user_api_client.get_services_for_user(current_user) ]), - search_form=SearchTemplatesForm(), + search_form=SearchByNameForm(), ) diff --git a/app/models/service.py b/app/models/service.py index 8bfe6d52a..8ecd722a2 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -23,11 +23,11 @@ class Service(): ALLOWED_PROPERTIES = { 'active', 'contact_link', - 'dvla_organisation', 'email_branding', 'email_from', 'id', 'inbound_api', + 'letter_branding', 'letter_contact_block', 'letter_logo_filename', 'message_limit', @@ -303,11 +303,15 @@ class Service(): return email_branding_client.get_email_branding(self.email_branding_id)['email_branding'] return None + @property + def letter_branding_id(self): + return self._dict['letter_branding'] + @cached_property def letter_branding(self): - return letter_branding_client.get_letter_branding().get( - self.dvla_organisation, '001' - ) + if self.letter_branding_id: + return letter_branding_client.get_letter_branding(self.letter_branding_id) + return None @cached_property def organisation_name(self): diff --git a/app/navigation.py b/app/navigation.py index f784360fe..13355a3a3 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -248,7 +248,7 @@ class HeaderNavigation(Navigation): 'service_switch_research_mode', 'services_or_dashboard', 'set_free_sms_allowance', - 'set_letter_branding', + 'service_set_letter_branding', 'set_organisation_type', 'set_sender', 'set_template_sender', @@ -364,7 +364,7 @@ class MainNavigation(Navigation): 'service_settings', 'service_sms_senders', 'set_free_sms_allowance', - 'set_letter_branding', + 'service_set_letter_branding', 'set_organisation_type', 'submit_request_to_go_live', }, @@ -703,7 +703,7 @@ class CaseworkNavigation(Navigation): 'service_switch_research_mode', 'services_or_dashboard', 'set_free_sms_allowance', - 'set_letter_branding', + 'service_set_letter_branding', 'set_organisation_type', 'set_sender', 'set_template_sender', @@ -935,7 +935,7 @@ class OrgNavigation(Navigation): 'service_switch_research_mode', 'services_or_dashboard', 'set_free_sms_allowance', - 'set_letter_branding', + 'service_set_letter_branding', 'set_organisation_type', 'set_sender', 'set_template_sender', diff --git a/app/notify_client/letter_branding_client.py b/app/notify_client/letter_branding_client.py index 84ab0b80f..88afe99c6 100644 --- a/app/notify_client/letter_branding_client.py +++ b/app/notify_client/letter_branding_client.py @@ -1,11 +1,17 @@ -from app.notify_client import NotifyAdminAPIClient +from app.notify_client import NotifyAdminAPIClient, cache class LetterBrandingClient(NotifyAdminAPIClient): - def get_letter_branding(self): - return self.get(url='/dvla_organisations') + @cache.set('letter_branding-{branding_id}') + def get_letter_branding(self, branding_id): + return self.get(url='/letter-branding/{}'.format(branding_id)) + @cache.set('letter_branding') + def get_all_letter_branding(self): + return self.get(url='/letter-branding') + + @cache.delete('letter_branding') def create_letter_branding(self, filename, name, domain): data = { "filename": filename, diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 75e3bdb04..7bce1b2d3 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -74,10 +74,9 @@ class ServiceAPIClient(NotifyAdminAPIClient): 'research_mode', 'sms_sender', 'created_by', - 'branding', + 'letter_branding', 'email_branding', 'letter_contact_block', - 'dvla_organisation', 'permissions', 'organisation_type', 'free_sms_fragment_limit', diff --git a/app/templates/views/email-branding/select-branding.html b/app/templates/views/email-branding/select-branding.html index 19d172dda..6007d7f98 100644 --- a/app/templates/views/email-branding/select-branding.html +++ b/app/templates/views/email-branding/select-branding.html @@ -17,7 +17,7 @@ Add new brand - {{ live_search(target_selector='.email-brand', show=show_search_box, form=search_form) }} + {{ live_search(target_selector='.email-brand', show=True, form=search_form) }}