mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 13:39:41 -04:00
Add org select and manage pages
This commit is contained in:
@@ -28,6 +28,7 @@ from app.main.views import (
|
||||
providers,
|
||||
platform_admin,
|
||||
letter_jobs,
|
||||
organisations,
|
||||
conversation,
|
||||
notifications
|
||||
)
|
||||
|
||||
@@ -25,6 +25,7 @@ from wtforms import (
|
||||
SelectField)
|
||||
from wtforms.fields.html5 import EmailField, TelField, SearchField
|
||||
from wtforms.validators import (DataRequired, Email, Length, Regexp, Optional)
|
||||
from flask_wtf.file import FileField as FileField_wtf, FileAllowed
|
||||
|
||||
from app.main.validators import (Blacklist, CsvFileValidator, ValidGovEmail, NoCommasInPlaceHolders, OnlyGSMCharacters)
|
||||
|
||||
@@ -559,6 +560,28 @@ class ServiceBrandingOrg(Form):
|
||||
)
|
||||
|
||||
|
||||
class ServiceSelectOrg(Form):
|
||||
|
||||
def __init__(self, organisations=[], *args, **kwargs):
|
||||
self.organisation.choices = organisations
|
||||
super(ServiceSelectOrg, self).__init__(*args, **kwargs)
|
||||
|
||||
organisation = RadioField(
|
||||
'Organisation',
|
||||
validators=[
|
||||
DataRequired()
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class ServiceManageOrg(Form):
|
||||
|
||||
name = StringField('Name')
|
||||
|
||||
colour = StringField('Colour', render_kw={'onkeyup': 'update_colour_span()', 'onblur': 'update_colour_span()'})
|
||||
file = FileField_wtf('Upload a PNG logo', validators=[FileAllowed(['png'], 'PNG Images only!')])
|
||||
|
||||
|
||||
class LetterBranding(Form):
|
||||
|
||||
def __init__(self, choices=[], *args, **kwargs):
|
||||
|
||||
95
app/main/s3_client.py
Normal file
95
app/main/s3_client.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import uuid
|
||||
import botocore
|
||||
from boto3 import resource, client
|
||||
from flask import current_app
|
||||
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||
|
||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||
TEMP_TAG = 'temp-{}_'
|
||||
LOGO_LOCATION_STRUCTURE = '{}{}-{}'
|
||||
|
||||
|
||||
def s3upload(service_id, filedata, region):
|
||||
upload_id = str(uuid.uuid4())
|
||||
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
||||
utils_s3upload(filedata=filedata['data'],
|
||||
region=region,
|
||||
bucket_name=current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||
file_location=upload_file_name)
|
||||
return upload_id
|
||||
|
||||
|
||||
def s3download(service_id, upload_id):
|
||||
contents = ''
|
||||
try:
|
||||
s3 = resource('s3')
|
||||
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
|
||||
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
||||
key = s3.Object(bucket_name, upload_file_name)
|
||||
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 upload_logo(filename, filedata, region, user_id):
|
||||
upload_id = str(uuid.uuid4())
|
||||
upload_file_name = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), upload_id, filename)
|
||||
utils_s3upload(filedata=filedata,
|
||||
region=region,
|
||||
bucket_name=current_app.config['LOGO_UPLOAD_BUCKET_NAME'],
|
||||
file_location=upload_file_name,
|
||||
content_type='image/png')
|
||||
return upload_file_name
|
||||
|
||||
|
||||
def persist_logo(filename, user_id):
|
||||
try:
|
||||
if filename.startswith(TEMP_TAG.format(user_id)):
|
||||
persisted_filename = filename[len(TEMP_TAG.format(user_id)):]
|
||||
else:
|
||||
return filename
|
||||
|
||||
s3 = resource('s3')
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
|
||||
s3.Object(bucket_name, persisted_filename).copy_from(CopySource='{}/{}'.format(bucket_name, filename))
|
||||
s3.Object(bucket_name, filename).delete()
|
||||
|
||||
return persisted_filename
|
||||
except botocore.exceptions.ClientError as e:
|
||||
current_app.logger.error("Unable to get s3 bucket contents {}".format(
|
||||
bucket_name))
|
||||
raise e
|
||||
|
||||
|
||||
def delete_temp_files_created_by(user_id):
|
||||
try:
|
||||
s3 = resource('s3')
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
|
||||
for obj in s3.Bucket(bucket_name).objects.filter(Prefix=TEMP_TAG.format(user_id)):
|
||||
s3.Object(bucket_name, obj.key).delete()
|
||||
|
||||
except botocore.exceptions.ClientError as e:
|
||||
current_app.logger.error("Unable to delete s3 bucket temp files created by {} from {}".format(
|
||||
user_id, bucket_name))
|
||||
raise e
|
||||
|
||||
|
||||
def delete_temp_file(filename):
|
||||
try:
|
||||
if not filename.startswith(TEMP_TAG):
|
||||
raise ValueError('Not a temp file')
|
||||
|
||||
s3 = resource('s3')
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
|
||||
s3.Object(bucket_name, filename).delete()
|
||||
|
||||
except botocore.exceptions.ClientError as e:
|
||||
current_app.logger.error("Unable to delete s3 bucket file {} from {}".format(
|
||||
filename, bucket_name))
|
||||
raise e
|
||||
@@ -1,32 +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'
|
||||
|
||||
|
||||
def s3upload(service_id, filedata, region):
|
||||
upload_id = str(uuid.uuid4())
|
||||
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
||||
utils_s3upload(filedata=filedata['data'],
|
||||
region=region,
|
||||
bucket_name=current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||
file_location=upload_file_name)
|
||||
return upload_id
|
||||
|
||||
|
||||
def s3download(service_id, upload_id):
|
||||
contents = ''
|
||||
try:
|
||||
s3 = resource('s3')
|
||||
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
|
||||
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
||||
key = s3.Object(bucket_name, upload_file_name)
|
||||
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
|
||||
98
app/main/views/organisations.py
Normal file
98
app/main/views/organisations.py
Normal file
@@ -0,0 +1,98 @@
|
||||
from flask import current_app, redirect, render_template, session, url_for, request
|
||||
from flask_login import login_required
|
||||
|
||||
from app import organisations_client
|
||||
from app.main import main
|
||||
from app.main.forms import (
|
||||
ServiceSelectOrg,
|
||||
ServiceManageOrg)
|
||||
from app.utils import user_has_permissions, get_cdn_domain
|
||||
from app.main.s3_client import (
|
||||
TEMP_TAG,
|
||||
upload_logo,
|
||||
delete_temp_file,
|
||||
delete_temp_files_created_by,
|
||||
persist_logo
|
||||
)
|
||||
from app.main.views.service_settings import get_branding_as_value_and_label, get_branding_as_dict
|
||||
|
||||
|
||||
@main.route("/organisations", methods=['GET', 'POST'])
|
||||
@main.route("/organisations/<organisation_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@user_has_permissions(admin_override=True)
|
||||
def organisations(organisation_id=None):
|
||||
orgs = organisations_client.get_organisations()
|
||||
|
||||
form = ServiceSelectOrg()
|
||||
form.organisation.choices = get_branding_as_value_and_label(orgs) + [('None', 'Create a new organisation')]
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.organisation.data != 'None':
|
||||
session['organisation'] = [o for o in orgs if o['id'] == form.organisation.data][0]
|
||||
elif session.get('organisation'):
|
||||
del session['organisation']
|
||||
|
||||
return redirect(url_for('.manage_org'))
|
||||
|
||||
form.organisation.data = organisation_id if organisation_id in [o['id'] for o in orgs] else 'None'
|
||||
|
||||
return render_template(
|
||||
'views/organisations/select-org.html',
|
||||
form=form,
|
||||
branding_dict=get_branding_as_dict(orgs),
|
||||
organisation_id=organisation_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/organisations/manage", methods=['GET', 'POST'])
|
||||
@main.route("/organisations/manage/<logo>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@user_has_permissions(admin_override=True)
|
||||
def manage_org(logo=None):
|
||||
form = ServiceManageOrg()
|
||||
|
||||
org = session.get("organisation")
|
||||
|
||||
logo = logo if logo else org.get('logo') if org else None
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.file.data:
|
||||
upload_filename = upload_logo(
|
||||
form.file.data.filename,
|
||||
form.file.data,
|
||||
current_app.config['AWS_REGION'],
|
||||
user_id=session["user_id"]
|
||||
)
|
||||
|
||||
if logo and logo.startswith(TEMP_TAG.format(session['user_id'])):
|
||||
delete_temp_file(logo)
|
||||
|
||||
return redirect(
|
||||
url_for('.manage_org', logo=upload_filename))
|
||||
|
||||
logo = persist_logo(logo, session["user_id"])
|
||||
delete_temp_files_created_by(session["user_id"])
|
||||
|
||||
if org:
|
||||
organisations_client.update_organisation(
|
||||
org_id=org['id'], logo=logo, name=form.name.data, colour=form.colour.data)
|
||||
org_id = org['id']
|
||||
else:
|
||||
resp = organisations_client.create_organisation(
|
||||
logo=logo, name=form.name.data, colour=form.colour.data)
|
||||
org_id = resp['data']['id']
|
||||
|
||||
return redirect(url_for('.organisations', organisation_id=org_id))
|
||||
|
||||
if org:
|
||||
form.name.data = org['name']
|
||||
form.colour.data = org['colour']
|
||||
|
||||
return render_template(
|
||||
'views/organisations/manage-org.html',
|
||||
form=form,
|
||||
organisation=org,
|
||||
cdn_url=get_cdn_domain(),
|
||||
logo=logo
|
||||
)
|
||||
@@ -33,7 +33,7 @@ from app.main.forms import (
|
||||
ChooseTimeForm,
|
||||
get_placeholder_form_instance
|
||||
)
|
||||
from app.main.uploader import (
|
||||
from app.main.s3_client import (
|
||||
s3upload,
|
||||
s3download
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user