mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-01 12:19:47 -04:00
Merge pull request #2719 from alphagov/create-letter-branding
Add platform admin page to create letter branding
This commit is contained in:
@@ -29,6 +29,7 @@ from app.main.views import ( # noqa
|
||||
find_users,
|
||||
platform_admin,
|
||||
email_branding,
|
||||
letter_branding,
|
||||
conversation,
|
||||
organisations,
|
||||
notifications,
|
||||
|
||||
@@ -851,6 +851,21 @@ class ServiceUpdateEmailBranding(StripWhitespaceForm):
|
||||
raise ValidationError('This field is required')
|
||||
|
||||
|
||||
class SVGFileUpload(StripWhitespaceForm):
|
||||
file = FileField_wtf(
|
||||
'Upload an SVG logo',
|
||||
validators=[
|
||||
FileAllowed(['svg'], 'SVG Images only!'),
|
||||
DataRequired(message="You need to upload a file to submit")
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class ServiceLetterBrandingDetails(StripWhitespaceForm):
|
||||
name = StringField('Name of brand', validators=[DataRequired()])
|
||||
domain = GovernmentDomainField('Domain')
|
||||
|
||||
|
||||
class PDFUploadForm(StripWhitespaceForm):
|
||||
file = FileField_wtf(
|
||||
'Upload a letter in PDF format to check if it fits in the printable area',
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
import uuid
|
||||
|
||||
import botocore
|
||||
from boto3 import resource
|
||||
from flask import current_app
|
||||
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||
|
||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||
TEMP_TAG = 'temp-{user_id}_'
|
||||
LOGO_LOCATION_STRUCTURE = '{temp}{unique_id}-{filename}'
|
||||
|
||||
|
||||
def get_s3_object(bucket_name, filename):
|
||||
s3 = resource('s3')
|
||||
return s3.Object(bucket_name, filename)
|
||||
|
||||
|
||||
def get_csv_location(service_id, upload_id):
|
||||
return (
|
||||
current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||
FILE_LOCATION_STRUCTURE.format(service_id, upload_id),
|
||||
)
|
||||
|
||||
|
||||
def get_csv_upload(service_id, upload_id):
|
||||
return get_s3_object(*get_csv_location(service_id, upload_id))
|
||||
|
||||
|
||||
def delete_s3_object(filename):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, filename).delete()
|
||||
|
||||
|
||||
def persist_logo(old_name, new_name):
|
||||
if old_name == new_name:
|
||||
return
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, new_name).copy_from(
|
||||
CopySource='{}/{}'.format(bucket_name, old_name))
|
||||
delete_s3_object(old_name)
|
||||
|
||||
|
||||
def get_s3_objects_filter_by_prefix(prefix):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
s3 = resource('s3')
|
||||
return s3.Bucket(bucket_name).objects.filter(Prefix=prefix)
|
||||
|
||||
|
||||
def get_temp_truncated_filename(filename, user_id):
|
||||
return filename[len(TEMP_TAG.format(user_id=user_id)):]
|
||||
|
||||
|
||||
def s3upload(service_id, filedata, region):
|
||||
upload_id = str(uuid.uuid4())
|
||||
bucket_name, file_location = get_csv_location(service_id, upload_id)
|
||||
utils_s3upload(
|
||||
filedata=filedata['data'],
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=file_location,
|
||||
)
|
||||
return upload_id
|
||||
|
||||
|
||||
def s3download(service_id, upload_id):
|
||||
contents = ''
|
||||
try:
|
||||
key = get_csv_upload(service_id, upload_id)
|
||||
contents = key.get()['Body'].read().decode('utf-8')
|
||||
except botocore.exceptions.ClientError as e:
|
||||
current_app.logger.error("Unable to download s3 file {}".format(
|
||||
FILE_LOCATION_STRUCTURE.format(service_id, upload_id)))
|
||||
raise e
|
||||
return contents
|
||||
|
||||
|
||||
def get_mou(organisation_is_crown):
|
||||
bucket = current_app.config['MOU_BUCKET_NAME']
|
||||
filename = 'crown.pdf' if organisation_is_crown else 'non-crown.pdf'
|
||||
attachment_filename = 'GOV.UK Notify data sharing and financial agreement{}.pdf'.format(
|
||||
'' if organisation_is_crown else ' (non-crown)'
|
||||
)
|
||||
try:
|
||||
key = get_s3_object(bucket, filename)
|
||||
return {
|
||||
'filename_or_fp': key.get()['Body'],
|
||||
'attachment_filename': attachment_filename,
|
||||
'as_attachment': True,
|
||||
}
|
||||
except botocore.exceptions.ClientError as exception:
|
||||
current_app.logger.error("Unable to download s3 file {}/{}".format(
|
||||
bucket, filename
|
||||
))
|
||||
raise exception
|
||||
|
||||
|
||||
def upload_logo(filename, filedata, region, user_id):
|
||||
upload_file_name = LOGO_LOCATION_STRUCTURE.format(
|
||||
temp=TEMP_TAG.format(user_id=user_id),
|
||||
unique_id=str(uuid.uuid4()),
|
||||
filename=filename
|
||||
)
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
utils_s3upload(
|
||||
filedata=filedata,
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=upload_file_name,
|
||||
content_type='image/png'
|
||||
)
|
||||
|
||||
return upload_file_name
|
||||
|
||||
|
||||
def permanent_logo_name(filename, user_id):
|
||||
if filename.startswith(TEMP_TAG.format(user_id=user_id)):
|
||||
return get_temp_truncated_filename(filename=filename, user_id=user_id)
|
||||
else:
|
||||
return filename
|
||||
|
||||
|
||||
def delete_temp_files_created_by(user_id):
|
||||
for obj in get_s3_objects_filter_by_prefix(TEMP_TAG.format(user_id=user_id)):
|
||||
delete_s3_object(obj.key)
|
||||
|
||||
|
||||
def delete_temp_file(filename):
|
||||
if not filename.startswith(TEMP_TAG[:5]):
|
||||
raise ValueError('Not a temp file: {}'.format(filename))
|
||||
|
||||
delete_s3_object(filename)
|
||||
|
||||
|
||||
def set_metadata_on_csv_upload(service_id, upload_id, **kwargs):
|
||||
get_csv_upload(
|
||||
service_id, upload_id
|
||||
).copy_from(
|
||||
CopySource='{}/{}'.format(*get_csv_location(service_id, upload_id)),
|
||||
ServerSideEncryption='AES256',
|
||||
Metadata={
|
||||
key: str(value) for key, value in kwargs.items()
|
||||
},
|
||||
MetadataDirective='REPLACE',
|
||||
)
|
||||
@@ -2,8 +2,8 @@ from flask import abort, render_template, request, send_file, url_for
|
||||
from flask_login import login_required
|
||||
|
||||
from app.main import main
|
||||
from app.main.s3_client import get_mou
|
||||
from app.main.views.sub_navigation_dictionaries import features_nav
|
||||
from app.s3_client.s3_mou_client import get_mou
|
||||
from app.utils import AgreementInfo
|
||||
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@ 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.s3_client import (
|
||||
from app.s3_client.s3_logo_client import (
|
||||
TEMP_TAG,
|
||||
delete_temp_file,
|
||||
delete_temp_files_created_by,
|
||||
permanent_logo_name,
|
||||
delete_email_temp_file,
|
||||
delete_email_temp_files_created_by,
|
||||
permanent_email_logo_name,
|
||||
persist_logo,
|
||||
upload_logo,
|
||||
upload_email_logo,
|
||||
)
|
||||
from app.utils import AgreementInfo, get_logo_cdn_domain, user_is_platform_admin
|
||||
|
||||
@@ -49,7 +49,7 @@ def update_email_branding(branding_id, logo=None):
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.file.data:
|
||||
upload_filename = upload_logo(
|
||||
upload_filename = upload_email_logo(
|
||||
form.file.data.filename,
|
||||
form.file.data,
|
||||
current_app.config['AWS_REGION'],
|
||||
@@ -57,11 +57,11 @@ def update_email_branding(branding_id, logo=None):
|
||||
)
|
||||
|
||||
if logo and logo.startswith(TEMP_TAG.format(user_id=session['user_id'])):
|
||||
delete_temp_file(logo)
|
||||
delete_email_temp_file(logo)
|
||||
|
||||
return redirect(url_for('.update_email_branding', branding_id=branding_id, logo=upload_filename))
|
||||
|
||||
updated_logo_name = permanent_logo_name(logo, session["user_id"]) if logo else None
|
||||
updated_logo_name = permanent_email_logo_name(logo, session["user_id"]) if logo else None
|
||||
|
||||
email_branding_client.update_email_branding(
|
||||
branding_id=branding_id,
|
||||
@@ -76,7 +76,7 @@ def update_email_branding(branding_id, logo=None):
|
||||
if logo:
|
||||
persist_logo(logo, updated_logo_name)
|
||||
|
||||
delete_temp_files_created_by(session["user_id"])
|
||||
delete_email_temp_files_created_by(session["user_id"])
|
||||
|
||||
return redirect(url_for('.email_branding', branding_id=branding_id))
|
||||
|
||||
@@ -98,7 +98,7 @@ def create_email_branding(logo=None):
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.file.data:
|
||||
upload_filename = upload_logo(
|
||||
upload_filename = upload_email_logo(
|
||||
form.file.data.filename,
|
||||
form.file.data,
|
||||
current_app.config['AWS_REGION'],
|
||||
@@ -106,11 +106,11 @@ def create_email_branding(logo=None):
|
||||
)
|
||||
|
||||
if logo and logo.startswith(TEMP_TAG.format(user_id=session['user_id'])):
|
||||
delete_temp_file(logo)
|
||||
delete_email_temp_file(logo)
|
||||
|
||||
return redirect(url_for('.create_email_branding', logo=upload_filename))
|
||||
|
||||
updated_logo_name = permanent_logo_name(logo, session["user_id"]) if logo else None
|
||||
updated_logo_name = permanent_email_logo_name(logo, session["user_id"]) if logo else None
|
||||
|
||||
email_branding_client.create_email_branding(
|
||||
logo=updated_logo_name,
|
||||
@@ -124,7 +124,7 @@ def create_email_branding(logo=None):
|
||||
if logo:
|
||||
persist_logo(logo, updated_logo_name)
|
||||
|
||||
delete_temp_files_created_by(session["user_id"])
|
||||
delete_email_temp_files_created_by(session["user_id"])
|
||||
|
||||
return redirect(url_for('.email_branding'))
|
||||
|
||||
|
||||
116
app/main/views/letter_branding.py
Normal file
116
app/main/views/letter_branding.py
Normal file
@@ -0,0 +1,116 @@
|
||||
from flask import (
|
||||
current_app,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
session,
|
||||
url_for,
|
||||
)
|
||||
from flask_login import login_required
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from requests import get as requests_get
|
||||
|
||||
from app import letter_branding_client
|
||||
from app.main import main
|
||||
from app.main.forms import ServiceLetterBrandingDetails, SVGFileUpload
|
||||
from app.s3_client.s3_logo_client import (
|
||||
LETTER_TEMP_TAG,
|
||||
delete_letter_temp_file,
|
||||
delete_letter_temp_files_created_by,
|
||||
get_letter_filename_with_no_path_or_extension,
|
||||
letter_filename_for_db,
|
||||
permanent_letter_logo_name,
|
||||
persist_logo,
|
||||
upload_letter_png_logo,
|
||||
upload_letter_temp_logo,
|
||||
)
|
||||
from app.utils import get_logo_cdn_domain, user_is_platform_admin
|
||||
|
||||
|
||||
@main.route("/letter-branding/create", methods=['GET', 'POST'])
|
||||
@main.route("/letter-branding/create/<path:logo>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@user_is_platform_admin
|
||||
def create_letter_branding(logo=None):
|
||||
file_upload_form = SVGFileUpload()
|
||||
letter_branding_details_form = ServiceLetterBrandingDetails()
|
||||
|
||||
file_upload_form_submitted = file_upload_form.file.data
|
||||
details_form_submitted = request.form.get('operation') == 'branding-details'
|
||||
|
||||
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 and logo.startswith(LETTER_TEMP_TAG.format(user_id=session['user_id'])):
|
||||
delete_letter_temp_file(logo)
|
||||
|
||||
return redirect(url_for('.create_letter_branding', logo=upload_filename))
|
||||
|
||||
if details_form_submitted and letter_branding_details_form.validate_on_submit():
|
||||
if logo:
|
||||
db_filename = letter_filename_for_db(logo, session['user_id'])
|
||||
png_file = get_png_file_from_svg(logo)
|
||||
|
||||
try:
|
||||
letter_branding_client.create_letter_branding(
|
||||
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'])
|
||||
|
||||
# TODO: redirect to all letter branding page once it exists
|
||||
return redirect(url_for('main.platform_admin'))
|
||||
|
||||
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
|
||||
else:
|
||||
# Show error on upload form if trying to submit with no logo
|
||||
file_upload_form.validate()
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
def get_png_file_from_svg(filename):
|
||||
filename_for_template_preview = get_letter_filename_with_no_path_or_extension(filename)
|
||||
|
||||
template_preview_svg_endpoint = '{}/{}.svg.png'.format(
|
||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||
filename_for_template_preview
|
||||
)
|
||||
|
||||
response = requests_get(
|
||||
template_preview_svg_endpoint,
|
||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||
)
|
||||
|
||||
return response.content
|
||||
|
||||
|
||||
def upload_letter_logos(old_filename, new_filename, png_file, user_id):
|
||||
persist_logo(old_filename, permanent_letter_logo_name(new_filename, 'svg'))
|
||||
|
||||
upload_letter_png_logo(
|
||||
permanent_letter_logo_name(new_filename, 'png'),
|
||||
png_file,
|
||||
current_app.config['AWS_REGION'],
|
||||
)
|
||||
|
||||
delete_letter_temp_files_created_by(user_id)
|
||||
@@ -42,7 +42,11 @@ from app.main.forms import (
|
||||
SetSenderForm,
|
||||
get_placeholder_form_instance,
|
||||
)
|
||||
from app.main.s3_client import s3download, s3upload, set_metadata_on_csv_upload
|
||||
from app.s3_client.s3_csv_client import (
|
||||
s3download,
|
||||
s3upload,
|
||||
set_metadata_on_csv_upload,
|
||||
)
|
||||
from app.template_previews import TemplatePreview, get_page_count_for_letter
|
||||
from app.utils import (
|
||||
Spreadsheet,
|
||||
|
||||
@@ -75,6 +75,7 @@ class HeaderNavigation(Navigation):
|
||||
'platform-admin': {
|
||||
'add_organisation',
|
||||
'create_email_branding',
|
||||
'create_letter_branding',
|
||||
'email_branding',
|
||||
'find_users_by_email',
|
||||
'live_services',
|
||||
@@ -405,6 +406,7 @@ class MainNavigation(Navigation):
|
||||
'conversation_updates',
|
||||
'cookies',
|
||||
'create_email_branding',
|
||||
'create_letter_branding',
|
||||
'data_retention',
|
||||
'delete_template_folder',
|
||||
'delivery_and_failure',
|
||||
@@ -583,6 +585,7 @@ class CaseworkNavigation(Navigation):
|
||||
'copy_template',
|
||||
'create_api_key',
|
||||
'create_email_branding',
|
||||
'create_letter_branding',
|
||||
'data_retention',
|
||||
'delete_service_template',
|
||||
'delete_template_folder',
|
||||
@@ -817,6 +820,7 @@ class OrgNavigation(Navigation):
|
||||
'copy_template',
|
||||
'create_api_key',
|
||||
'create_email_branding',
|
||||
'create_letter_branding',
|
||||
'data_retention',
|
||||
'delete_service_template',
|
||||
'delete_template_folder',
|
||||
|
||||
@@ -6,5 +6,13 @@ class LetterBrandingClient(NotifyAdminAPIClient):
|
||||
def get_letter_branding(self):
|
||||
return self.get(url='/dvla_organisations')
|
||||
|
||||
def create_letter_branding(self, filename, name, domain):
|
||||
data = {
|
||||
"filename": filename,
|
||||
"name": name,
|
||||
"domain": domain,
|
||||
}
|
||||
return self.post(url="/letter-branding", data=data)
|
||||
|
||||
|
||||
letter_branding_client = LetterBrandingClient()
|
||||
|
||||
0
app/s3_client/__init__.py
Normal file
0
app/s3_client/__init__.py
Normal file
57
app/s3_client/s3_csv_client.py
Normal file
57
app/s3_client/s3_csv_client.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import uuid
|
||||
|
||||
import botocore
|
||||
from flask import current_app
|
||||
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||
|
||||
from app.s3_client.s3_logo_client import get_s3_object
|
||||
|
||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||
|
||||
|
||||
def get_csv_location(service_id, upload_id):
|
||||
return (
|
||||
current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||
FILE_LOCATION_STRUCTURE.format(service_id, upload_id),
|
||||
)
|
||||
|
||||
|
||||
def get_csv_upload(service_id, upload_id):
|
||||
return get_s3_object(*get_csv_location(service_id, upload_id))
|
||||
|
||||
|
||||
def s3upload(service_id, filedata, region):
|
||||
upload_id = str(uuid.uuid4())
|
||||
bucket_name, file_location = get_csv_location(service_id, upload_id)
|
||||
utils_s3upload(
|
||||
filedata=filedata['data'],
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=file_location,
|
||||
)
|
||||
return upload_id
|
||||
|
||||
|
||||
def s3download(service_id, upload_id):
|
||||
contents = ''
|
||||
try:
|
||||
key = get_csv_upload(service_id, upload_id)
|
||||
contents = key.get()['Body'].read().decode('utf-8')
|
||||
except botocore.exceptions.ClientError as e:
|
||||
current_app.logger.error("Unable to download s3 file {}".format(
|
||||
FILE_LOCATION_STRUCTURE.format(service_id, upload_id)))
|
||||
raise e
|
||||
return contents
|
||||
|
||||
|
||||
def set_metadata_on_csv_upload(service_id, upload_id, **kwargs):
|
||||
get_csv_upload(
|
||||
service_id, upload_id
|
||||
).copy_from(
|
||||
CopySource='{}/{}'.format(*get_csv_location(service_id, upload_id)),
|
||||
ServerSideEncryption='AES256',
|
||||
Metadata={
|
||||
key: str(value) for key, value in kwargs.items()
|
||||
},
|
||||
MetadataDirective='REPLACE',
|
||||
)
|
||||
135
app/s3_client/s3_logo_client.py
Normal file
135
app/s3_client/s3_logo_client.py
Normal file
@@ -0,0 +1,135 @@
|
||||
import uuid
|
||||
|
||||
from boto3 import resource
|
||||
from flask import current_app
|
||||
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||
|
||||
TEMP_TAG = 'temp-{user_id}_'
|
||||
EMAIL_LOGO_LOCATION_STRUCTURE = '{temp}{unique_id}-{filename}'
|
||||
LETTER_PREFIX = 'letters/static/images/letter-template/'
|
||||
LETTER_TEMP_TAG = LETTER_PREFIX + TEMP_TAG
|
||||
LETTER_TEMP_LOGO_LOCATION = 'letters/static/images/letter-template/temp-{user_id}_{unique_id}-{filename}'
|
||||
|
||||
|
||||
def get_s3_object(bucket_name, filename):
|
||||
s3 = resource('s3')
|
||||
return s3.Object(bucket_name, filename)
|
||||
|
||||
|
||||
def delete_s3_object(filename):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, filename).delete()
|
||||
|
||||
|
||||
def persist_logo(old_name, new_name):
|
||||
if old_name == new_name:
|
||||
return
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, new_name).copy_from(
|
||||
CopySource='{}/{}'.format(bucket_name, old_name))
|
||||
delete_s3_object(old_name)
|
||||
|
||||
|
||||
def get_s3_objects_filter_by_prefix(prefix):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
s3 = resource('s3')
|
||||
return s3.Bucket(bucket_name).objects.filter(Prefix=prefix)
|
||||
|
||||
|
||||
def get_temp_truncated_filename(filename, user_id):
|
||||
return filename[len(TEMP_TAG.format(user_id=user_id)):]
|
||||
|
||||
|
||||
def get_letter_filename_with_no_path_or_extension(filename):
|
||||
return filename[len(LETTER_PREFIX):-4]
|
||||
|
||||
|
||||
def upload_email_logo(filename, filedata, region, user_id):
|
||||
upload_file_name = EMAIL_LOGO_LOCATION_STRUCTURE.format(
|
||||
temp=TEMP_TAG.format(user_id=user_id),
|
||||
unique_id=str(uuid.uuid4()),
|
||||
filename=filename
|
||||
)
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
utils_s3upload(
|
||||
filedata=filedata,
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=upload_file_name,
|
||||
content_type='image/png'
|
||||
)
|
||||
|
||||
return upload_file_name
|
||||
|
||||
|
||||
def upload_letter_temp_logo(filename, filedata, region, user_id):
|
||||
upload_filename = LETTER_TEMP_LOGO_LOCATION.format(
|
||||
user_id=user_id,
|
||||
unique_id=str(uuid.uuid4()),
|
||||
filename=filename
|
||||
)
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
utils_s3upload(
|
||||
filedata=filedata,
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=upload_filename,
|
||||
content_type='image/svg+xml'
|
||||
)
|
||||
|
||||
return upload_filename
|
||||
|
||||
|
||||
def upload_letter_png_logo(filename, filedata, region):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
utils_s3upload(
|
||||
filedata=filedata,
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=filename,
|
||||
content_type='image/png'
|
||||
)
|
||||
|
||||
|
||||
def permanent_email_logo_name(filename, user_id):
|
||||
if filename.startswith(TEMP_TAG.format(user_id=user_id)):
|
||||
return get_temp_truncated_filename(filename=filename, user_id=user_id)
|
||||
else:
|
||||
return filename
|
||||
|
||||
|
||||
def permanent_letter_logo_name(filename, extension):
|
||||
return LETTER_PREFIX + filename + '.' + extension
|
||||
|
||||
|
||||
def letter_filename_for_db(filename, user_id):
|
||||
filename = get_letter_filename_with_no_path_or_extension(filename)
|
||||
|
||||
if filename.startswith(TEMP_TAG.format(user_id=user_id)):
|
||||
filename = get_temp_truncated_filename(filename=filename, user_id=user_id)
|
||||
|
||||
return filename
|
||||
|
||||
|
||||
def delete_email_temp_files_created_by(user_id):
|
||||
for obj in get_s3_objects_filter_by_prefix(TEMP_TAG.format(user_id=user_id)):
|
||||
delete_s3_object(obj.key)
|
||||
|
||||
|
||||
def delete_letter_temp_files_created_by(user_id):
|
||||
for obj in get_s3_objects_filter_by_prefix(LETTER_TEMP_TAG.format(user_id=user_id)):
|
||||
delete_s3_object(obj.key)
|
||||
|
||||
|
||||
def delete_email_temp_file(filename):
|
||||
if not filename.startswith(TEMP_TAG[:5]):
|
||||
raise ValueError('Not a temp file: {}'.format(filename))
|
||||
|
||||
delete_s3_object(filename)
|
||||
|
||||
|
||||
def delete_letter_temp_file(filename):
|
||||
if not filename.startswith(LETTER_TEMP_TAG[:43]):
|
||||
raise ValueError('Not a temp file: {}'.format(filename))
|
||||
|
||||
delete_s3_object(filename)
|
||||
24
app/s3_client/s3_mou_client.py
Normal file
24
app/s3_client/s3_mou_client.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import botocore
|
||||
from flask import current_app
|
||||
|
||||
from app.s3_client.s3_logo_client import get_s3_object
|
||||
|
||||
|
||||
def get_mou(organisation_is_crown):
|
||||
bucket = current_app.config['MOU_BUCKET_NAME']
|
||||
filename = 'crown.pdf' if organisation_is_crown else 'non-crown.pdf'
|
||||
attachment_filename = 'GOV.UK Notify data sharing and financial agreement{}.pdf'.format(
|
||||
'' if organisation_is_crown else ' (non-crown)'
|
||||
)
|
||||
try:
|
||||
key = get_s3_object(bucket, filename)
|
||||
return {
|
||||
'filename_or_fp': key.get()['Body'],
|
||||
'attachment_filename': attachment_filename,
|
||||
'as_attachment': True,
|
||||
}
|
||||
except botocore.exceptions.ClientError as exception:
|
||||
current_app.logger.error("Unable to download s3 file {}/{}".format(
|
||||
bucket, filename
|
||||
))
|
||||
raise exception
|
||||
@@ -0,0 +1,37 @@
|
||||
{% extends "views/platform-admin/_base_template.html" %}
|
||||
{% from "components/file-upload.html" import file_upload %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
{% from "components/form.html" import form_wrapper %}
|
||||
|
||||
{% block per_page_title %}
|
||||
Create letter branding
|
||||
{% endblock %}
|
||||
|
||||
{% block platform_admin_content %}
|
||||
|
||||
<h1 class="heading-large">Add letter branding</h1>
|
||||
<div class="grid-row">
|
||||
<div class="column-three-quarters">
|
||||
{% if logo %}
|
||||
<div id="logo-img">
|
||||
<img src="https://{{ cdn_url }}/{{ logo }}"/>
|
||||
</div>
|
||||
{% endif %}
|
||||
{{ file_upload(file_upload_form.file) }}
|
||||
{% call form_wrapper() %}
|
||||
<div class="form-group">
|
||||
<div style='margin-top:15px;'>{{textbox(letter_branding_details_form.name)}}</div>
|
||||
<div style='margin-top:15px;'>{{textbox(letter_branding_details_form.domain)}}</div>
|
||||
{{ page_footer(
|
||||
'Save',
|
||||
button_name='operation',
|
||||
button_value='branding-details',
|
||||
back_link=url_for('main.platform_admin'),
|
||||
back_link_text='Back to letter branding selection',
|
||||
) }}
|
||||
</div>
|
||||
{% endcall %}
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -126,7 +126,7 @@ def get_errors_for_csv(recipients, template_type):
|
||||
|
||||
def generate_notifications_csv(**kwargs):
|
||||
from app import notification_api_client
|
||||
from app.main.s3_client import s3download
|
||||
from app.s3_client.s3_csv_client import s3download
|
||||
if 'page' not in kwargs:
|
||||
kwargs['page'] = 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user