Merge pull request #2751 from alphagov/update-letter-branding

Allow letter branding to be updated
This commit is contained in:
Katie Smith
2019-02-13 09:19:47 +00:00
committed by GitHub
13 changed files with 480 additions and 34 deletions

View File

@@ -10,10 +10,9 @@ from app.utils import AgreementInfo, email_safe, user_is_gov_user
def _create_service(service_name, organisation_type, email_from, form):
free_sms_fragment_limit = current_app.config['DEFAULT_FREE_SMS_FRAGMENT_LIMITS'].get(organisation_type)
email_branding = email_branding_client.get_email_branding_id_for_domain(
'nhs.uk' if organisation_type == 'nhs' else
AgreementInfo.from_current_user().canonical_domain
)
domain = 'nhs.uk' if organisation_type == 'nhs' else AgreementInfo.from_current_user().canonical_domain
email_branding = email_branding_client.get_email_branding_id_for_domain(domain)
try:
service_id = service_api_client.create_service(
service_name=service_name,
@@ -22,6 +21,7 @@ def _create_service(service_name, organisation_type, email_from, form):
restricted=True,
user_id=session['user_id'],
email_from=email_from,
service_domain=domain
)
session['service_id'] = service_id

View File

@@ -1,3 +1,4 @@
from botocore.exceptions import ClientError as BotoClientError
from flask import (
current_app,
redirect,
@@ -45,6 +46,92 @@ def letter_branding():
)
@main.route("/letter-branding/<branding_id>/edit", methods=['GET', 'POST'])
@main.route("/letter-branding/<branding_id>/edit/<path:logo>", methods=['GET', 'POST'])
@login_required
@user_is_platform_admin
def update_letter_branding(branding_id, logo=None):
letter_branding = letter_branding_client.get_letter_branding(branding_id)
file_upload_form = SVGFileUpload()
letter_branding_details_form = ServiceLetterBrandingDetails(
name=letter_branding['name'],
domain=letter_branding['domain']
)
file_upload_form_submitted = file_upload_form.file.data
details_form_submitted = request.form.get('operation') == 'branding-details'
logo = logo if logo else permanent_letter_logo_name(letter_branding['filename'], 'svg')
if file_upload_form_submitted and file_upload_form.validate_on_submit():
upload_filename = upload_letter_temp_logo(
file_upload_form.file.data.filename,
file_upload_form.file.data,
current_app.config['AWS_REGION'],
user_id=session["user_id"]
)
if logo.startswith(LETTER_TEMP_TAG.format(user_id=session['user_id'])):
delete_letter_temp_file(logo)
return redirect(url_for('.update_letter_branding', branding_id=branding_id, logo=upload_filename))
if details_form_submitted and letter_branding_details_form.validate_on_submit():
db_filename = letter_filename_for_db(logo, session['user_id'])
try:
if db_filename == letter_branding['filename']:
letter_branding_client.update_letter_branding(
branding_id=branding_id,
filename=db_filename,
name=letter_branding_details_form.name.data,
domain=letter_branding_details_form.domain.data
)
return redirect(url_for('main.letter_branding'))
else:
png_file = get_png_file_from_svg(logo)
letter_branding_client.update_letter_branding(
branding_id=branding_id,
filename=db_filename,
name=letter_branding_details_form.name.data,
domain=letter_branding_details_form.domain.data
)
upload_letter_logos(logo, db_filename, png_file, session['user_id'])
return redirect(url_for('main.letter_branding'))
except HTTPError as e:
if 'domain' in e.message:
letter_branding_details_form.domain.errors.append(e.message['domain'][0])
elif 'name' in e.message:
letter_branding_details_form.name.errors.append(e.message['name'][0])
else:
raise e
except BotoClientError:
# we had a problem saving the file - rollback the db changes
letter_branding_client.update_letter_branding(
branding_id=branding_id,
filename=letter_branding['filename'],
name=letter_branding['name'],
domain=letter_branding['domain']
)
file_upload_form.file.errors = ['Error saving uploaded file - try uploading again']
return render_template(
'views/letter-branding/manage-letter-branding.html',
file_upload_form=file_upload_form,
letter_branding_details_form=letter_branding_details_form,
cdn_url=get_logo_cdn_domain(),
logo=logo,
is_update=True
)
@main.route("/letter-branding/create", methods=['GET', 'POST'])
@main.route("/letter-branding/create/<path:logo>", methods=['GET', 'POST'])
@login_required

View File

@@ -88,6 +88,7 @@ class HeaderNavigation(Navigation):
'suspend_service',
'trial_services',
'update_email_branding',
'update_letter_branding',
'user_information',
'view_provider',
'view_providers',
@@ -499,6 +500,7 @@ class MainNavigation(Navigation):
'two_factor_email',
'two_factor_email_sent',
'update_email_branding',
'update_letter_branding',
'user_information',
'user_profile',
'user_profile_email',
@@ -731,6 +733,7 @@ class CaseworkNavigation(Navigation):
'two_factor_email',
'two_factor_email_sent',
'update_email_branding',
'update_letter_branding',
'usage',
'user_information',
'user_profile',
@@ -964,6 +967,7 @@ class OrgNavigation(Navigation):
'two_factor_email',
'two_factor_email_sent',
'update_email_branding',
'update_letter_branding',
'usage',
'user_information',
'user_profile',

View File

@@ -20,5 +20,16 @@ class LetterBrandingClient(NotifyAdminAPIClient):
}
return self.post(url="/letter-branding", data=data)
@cache.delete('letter_branding')
@cache.delete('letter_branding-{branding_id}')
def update_letter_branding(self, branding_id, filename, name, domain):
data = {
"filename": filename,
"name": name,
"domain": domain,
}
return self.post(url="/letter-branding/{}".format(branding_id), data=data)
letter_branding_client = LetterBrandingClient()

View File

@@ -13,6 +13,7 @@ class ServiceAPIClient(NotifyAdminAPIClient):
restricted,
user_id,
email_from,
service_domain,
):
"""
Create a service and return the json.
@@ -24,7 +25,8 @@ class ServiceAPIClient(NotifyAdminAPIClient):
"message_limit": message_limit,
"user_id": user_id,
"restricted": restricted,
"email_from": email_from
"email_from": email_from,
"service_domain": service_domain
}
data = _attach_current_user(data)
return self.post("/service", data)['data']['id']

View File

@@ -5,12 +5,12 @@
{% from "components/form.html" import form_wrapper %}
{% block per_page_title %}
Create letter branding
{{ '{} letter branding'.format('Update' if is_update else 'Create')}}
{% endblock %}
{% block platform_admin_content %}
<h1 class="heading-large">Add letter branding</h1>
<h1 class="heading-large">{{ '{} letter branding'.format('Update' if is_update else 'Add')}}</h1>
<div class="grid-row">
<div class="column-three-quarters">
{% if logo %}
@@ -18,7 +18,7 @@
<img src="https://{{ cdn_url }}/{{ logo }}"/>
</div>
{% endif %}
{{ file_upload(file_upload_form.file) }}
{{ file_upload(file_upload_form.file, button_text='{} logo'.format('Update' if is_update else 'Upload')) }}
{% call form_wrapper() %}
<div class="form-group">
<div style='margin-top:15px;'>{{textbox(letter_branding_details_form.name)}}</div>

View File

@@ -20,7 +20,9 @@
{% for brand in letter_brandings %}
<div class="letter-brand">
<div class="message-name">
{{ brand.name }}
<a href="{{ url_for('.update_letter_branding', branding_id=brand.id) }}">
{{ brand.name }}
</a>
</div>
<p class="message-type">
{% if brand.domain %}