mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-25 00:33:58 -04:00
Add org select and manage pages
This commit is contained in:
@@ -96,7 +96,7 @@ class Development(Config):
|
|||||||
SESSION_PROTECTION = None
|
SESSION_PROTECTION = None
|
||||||
STATSD_ENABLED = False
|
STATSD_ENABLED = False
|
||||||
CSV_UPLOAD_BUCKET_NAME = 'development-notifications-csv-upload'
|
CSV_UPLOAD_BUCKET_NAME = 'development-notifications-csv-upload'
|
||||||
LOGO_UPLOAD_BUCKET_NAME = 'development-notifications-logo-upload'
|
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-tools'
|
||||||
|
|
||||||
|
|
||||||
class Test(Development):
|
class Test(Development):
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ from app.main.views import (
|
|||||||
providers,
|
providers,
|
||||||
platform_admin,
|
platform_admin,
|
||||||
letter_jobs,
|
letter_jobs,
|
||||||
|
organisations,
|
||||||
conversation,
|
conversation,
|
||||||
notifications
|
notifications
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ from wtforms import (
|
|||||||
SelectField)
|
SelectField)
|
||||||
from wtforms.fields.html5 import EmailField, TelField, SearchField
|
from wtforms.fields.html5 import EmailField, TelField, SearchField
|
||||||
from wtforms.validators import (DataRequired, Email, Length, Regexp, Optional)
|
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)
|
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):
|
class LetterBranding(Form):
|
||||||
|
|
||||||
def __init__(self, choices=[], *args, **kwargs):
|
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,
|
ChooseTimeForm,
|
||||||
get_placeholder_form_instance
|
get_placeholder_form_instance
|
||||||
)
|
)
|
||||||
from app.main.uploader import (
|
from app.main.s3_client import (
|
||||||
s3upload,
|
s3upload,
|
||||||
s3download
|
s3download
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -19,3 +19,19 @@ class OrganisationsClient(NotifyAdminAPIClient):
|
|||||||
|
|
||||||
def get_letter_organisations(self):
|
def get_letter_organisations(self):
|
||||||
return self.get(url='/dvla_organisations')
|
return self.get(url='/dvla_organisations')
|
||||||
|
|
||||||
|
def create_organisation(self, logo, name, colour):
|
||||||
|
data = {
|
||||||
|
"logo": logo,
|
||||||
|
"name": name,
|
||||||
|
"colour": colour
|
||||||
|
}
|
||||||
|
return self.post("/organisation", data)
|
||||||
|
|
||||||
|
def update_organisation(self, org_id, logo, name, colour):
|
||||||
|
data = {
|
||||||
|
"logo": logo,
|
||||||
|
"name": name,
|
||||||
|
"colour": colour
|
||||||
|
}
|
||||||
|
return self.post("/organisation/{}".format(org_id), data)
|
||||||
|
|||||||
@@ -49,6 +49,18 @@
|
|||||||
<li>
|
<li>
|
||||||
<a href="{{ url_for('main.platform_admin') }}">Platform admin</a>
|
<a href="{{ url_for('main.platform_admin') }}">Platform admin</a>
|
||||||
</li>
|
</li>
|
||||||
|
<<<<<<< HEAD
|
||||||
|
=======
|
||||||
|
<li>
|
||||||
|
<a href="{{ url_for('main.view_providers') }}">Providers</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="{{ url_for('main.organisations') }}">Organisations</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="{{ url_for('main.letter_jobs') }}">Letter jobs</a>
|
||||||
|
</li>
|
||||||
|
>>>>>>> Add org select and manage pages
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ url_for('main.sign_out')}}">Sign out</a>
|
<a href="{{ url_for('main.sign_out')}}">Sign out</a>
|
||||||
|
|||||||
@@ -6,12 +6,13 @@
|
|||||||
secondary_link=False,
|
secondary_link=False,
|
||||||
secondary_link_text=None,
|
secondary_link_text=None,
|
||||||
delete_link=False,
|
delete_link=False,
|
||||||
delete_link_text="delete"
|
delete_link_text="delete",
|
||||||
|
button_disabled=False
|
||||||
) %}
|
) %}
|
||||||
<div class="page-footer">
|
<div class="page-footer">
|
||||||
{% if button_text %}
|
{% if button_text %}
|
||||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
|
||||||
<input type="submit" class="button{% if destructive %}-destructive{% endif %}" value="{{ button_text }}" />
|
<input type="submit" class="button{% if destructive %}-destructive{% endif %}" value="{{ button_text }}"{% if button_disabled %} disabled{% endif %}/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if back_link %}
|
{% if back_link %}
|
||||||
<a class="page-footer-back-link" href="{{ back_link }}">{{ back_link_text }}</a>
|
<a class="page-footer-back-link" href="{{ back_link }}">{{ back_link_text }}</a>
|
||||||
|
|||||||
@@ -79,16 +79,19 @@
|
|||||||
{% macro branding_radios(
|
{% macro branding_radios(
|
||||||
field,
|
field,
|
||||||
hint=None,
|
hint=None,
|
||||||
branding_dict={}
|
branding_dict={},
|
||||||
|
show_header=True
|
||||||
) %}
|
) %}
|
||||||
<div class="form-group {% if field.errors %} form-group-error{% endif %}">
|
<div class="form-group {% if field.errors %} form-group-error{% endif %}">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend class="form-label">
|
<legend class="form-label">
|
||||||
{{ field.label.text }}
|
{% if show_header %}
|
||||||
|
{{ field.label.text }}
|
||||||
|
{% endif %}
|
||||||
{% if field.errors %}
|
{% if field.errors %}
|
||||||
<span class="error-message">
|
<span class="error-message">
|
||||||
{{ field.errors[0] }}
|
{{ field.errors[0] }}
|
||||||
</span>
|
</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</legend>
|
</legend>
|
||||||
{% for value, option, checked in field.iter_choices() %}
|
{% for value, option, checked in field.iter_choices() %}
|
||||||
@@ -102,7 +105,7 @@
|
|||||||
/>
|
/>
|
||||||
<label class="block-label" for="{{ field.name }}-{{ loop.index }}">
|
<label class="block-label" for="{{ field.name }}-{{ loop.index }}">
|
||||||
{% if branding_dict.get(value, {}).get('colour') %}
|
{% if branding_dict.get(value, {}).get('colour') %}
|
||||||
<span style="background: {{ branding_dict[value].colour }}; display: inline-block; width: 3px; height: 27px"></span>
|
<span style="background: {{ branding_dict[value].colour }}; display: inline-block; width: 3px; height: 27px;"></span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if branding_dict.get(value, {}).get('logo') %}
|
{% if branding_dict.get(value, {}).get('logo') %}
|
||||||
<img
|
<img
|
||||||
|
|||||||
55
app/templates/views/organisations/manage-org.html
Normal file
55
app/templates/views/organisations/manage-org.html
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
{% extends "withoutnav_template.html" %}
|
||||||
|
{% from "components/file-upload.html" import file_upload %}
|
||||||
|
{% from "components/page-footer.html" import page_footer %}
|
||||||
|
{% from "components/textbox.html" import textbox %}
|
||||||
|
|
||||||
|
{% block service_page_title %}
|
||||||
|
{{ '{} an organisations logo'.format('Update' if organisation else 'Create')}}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block maincolumn_content %}
|
||||||
|
|
||||||
|
<h1 class="heading-large">{{ '{} an organisations logo'.format('Update' if organisation else 'Create')}}</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(
|
||||||
|
form.file,
|
||||||
|
button_text='{} logo'.format('Update' if organisation else 'Upload')
|
||||||
|
) }}
|
||||||
|
<form method="post">
|
||||||
|
<div class="form-group">
|
||||||
|
<div style='margin-top:15px;'>{{textbox(form.name)}}</div>
|
||||||
|
<div>{{textbox(form.colour, width='1-4')}}
|
||||||
|
<span id='colour_span' style="background: {{ organisation.colour }}; {% if not organisation.colour %}visibility:hidden; {% endif %}border:1px black solid; width: 3px; height: 25px;position:absolute;margin-top:138px;margin-left:185px;display:block;"></span>
|
||||||
|
</div>
|
||||||
|
{{ page_footer(
|
||||||
|
'Save',
|
||||||
|
back_link=url_for('.organisations', organisation_id=organisation.id if organisation else 'None'),
|
||||||
|
back_link_text='Back to organisation selection',
|
||||||
|
button_disabled=True if not logo else False
|
||||||
|
) }}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
function update_colour_span() {
|
||||||
|
if (document.getElementById('colour').value) {
|
||||||
|
document.getElementById('colour_span').style.visibility = 'visible';
|
||||||
|
document.getElementById('colour_span').style.background = document.getElementById('colour').value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
document.getElementById('colour_span').style.visibility = 'hidden';
|
||||||
|
document.getElementById('colour_span').style.background = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
26
app/templates/views/organisations/select-org.html
Normal file
26
app/templates/views/organisations/select-org.html
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{% extends "withoutnav_template.html" %}
|
||||||
|
{% from "components/radios.html" import radios, branding_radios %}
|
||||||
|
{% from "components/page-footer.html" import page_footer %}
|
||||||
|
|
||||||
|
{% block service_page_title %}
|
||||||
|
Select organisation
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block maincolumn_content %}
|
||||||
|
|
||||||
|
<h1 class="heading-large">
|
||||||
|
<div>Select an organisation to update</div>
|
||||||
|
<div>or create a new organisation</div>
|
||||||
|
</h1>
|
||||||
|
<div class="grid-row">
|
||||||
|
<div class="column-three-quarters">
|
||||||
|
<form method="post">
|
||||||
|
{{ branding_radios(form.organisation, branding_dict=branding_dict, show_header=False) }}
|
||||||
|
{{ page_footer(
|
||||||
|
'Next'
|
||||||
|
) }}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
@@ -26,4 +26,8 @@ notifications-python-client==4.3.1
|
|||||||
awscli>=1.11,<1.12
|
awscli>=1.11,<1.12
|
||||||
awscli-cwlogs>=1.4,<1.5
|
awscli-cwlogs>=1.4,<1.5
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
git+https://github.com/alphagov/notifications-utils.git@17.7.0#egg=notifications-utils==17.7.0
|
git+https://github.com/alphagov/notifications-utils.git@17.7.0#egg=notifications-utils==17.7.0
|
||||||
|
=======
|
||||||
|
git+https://github.com/alphagov/notifications-utils.git@17.5.5#egg=notifications-utils==17.5.5
|
||||||
|
>>>>>>> Add org select and manage pages
|
||||||
|
|||||||
241
tests/app/main/views/test_organisations.py
Normal file
241
tests/app/main/views/test_organisations.py
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
from io import BytesIO
|
||||||
|
from unittest.mock import call
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from flask import url_for
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.main.s3_client import TEMP_TAG, LOGO_LOCATION_STRUCTURE
|
||||||
|
|
||||||
|
sample_orgs = [
|
||||||
|
{'id': '1', 'name': 'org 1', 'colour': 'red', 'logo': 'logo1.png'},
|
||||||
|
{'id': '2', 'name': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'},
|
||||||
|
{'id': '3', 'name': None, 'colour': None, 'logo': 'logo3.png'},
|
||||||
|
{'id': '4', 'name': 'org 4', 'colour': None, 'logo': 'logo4.png'},
|
||||||
|
{'id': '5', 'name': None, 'colour': 'blue', 'logo': 'logo5.png'},
|
||||||
|
]
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def visit_manage_org_with_org(logged_in_platform_admin_client):
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
session['organisation'] = sample_orgs[0]
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.get(
|
||||||
|
url_for('.manage_org')
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
return BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def visit_manage_org_without_org(logged_in_platform_admin_client):
|
||||||
|
response = logged_in_platform_admin_client.get(
|
||||||
|
url_for('.manage_org')
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
return BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
|
||||||
|
|
||||||
|
def test_organisations_page_shows_full_orgs_list(logged_in_platform_admin_client, mocker):
|
||||||
|
mocker.patch('app.organisations_client.get_organisations', return_value=sample_orgs)
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.get(
|
||||||
|
url_for('.organisations')
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
assert ' '.join(page.find('h1').text.split()) == "Select an organisation to update or create a new organisation"
|
||||||
|
for index, label in enumerate(page.select('div.multiple-choice > label')):
|
||||||
|
if index < len(sample_orgs):
|
||||||
|
if sample_orgs[index]['colour']:
|
||||||
|
assert 'background: {};'.format(sample_orgs[index]['colour']) in label.find('span')['style']
|
||||||
|
|
||||||
|
assert ' '.join(label.text.split()) == str(sample_orgs[index]['name'])
|
||||||
|
assert label.find('img')['src'].endswith('/' + sample_orgs[index]['logo'])
|
||||||
|
else:
|
||||||
|
assert ' '.join(label.text.split()) == 'Create a new organisation'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("org_id", [
|
||||||
|
'None', '1', '2'
|
||||||
|
])
|
||||||
|
def test_organisations_radio_default_to_just_updated_or_new_org(
|
||||||
|
logged_in_platform_admin_client, mocker, org_id):
|
||||||
|
mocker.patch('app.organisations_client.get_organisations', return_value=sample_orgs)
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.get(
|
||||||
|
url_for('.organisations', organisation_id=org_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
|
||||||
|
selected = [r for r in page.select('div.multiple-choice > input') if r.attrs.get('checked')][0]
|
||||||
|
assert selected["value"] == org_id
|
||||||
|
|
||||||
|
|
||||||
|
def test_organisations_post_sets_organisation_in_session_after_selecting_org(
|
||||||
|
logged_in_platform_admin_client, mocker):
|
||||||
|
mocker.patch('app.organisations_client.get_organisations', return_value=sample_orgs)
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for('.organisations'),
|
||||||
|
data={'organisation': sample_orgs[0]['id']}
|
||||||
|
)
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
assert session['organisation'] == sample_orgs[0]
|
||||||
|
assert response.status_code == 302
|
||||||
|
assert response.location == url_for('.manage_org', _external=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_organisations_post_deletes_organisation_session_on_new_org(
|
||||||
|
logged_in_platform_admin_client, mocker):
|
||||||
|
mocker.patch('app.organisations_client.get_organisations', return_value=sample_orgs)
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
session['organisation'] = sample_orgs[0]
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for('.organisations'),
|
||||||
|
data={'organisation': 'None'}
|
||||||
|
)
|
||||||
|
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
assert session.get('organisation') is None
|
||||||
|
assert response.status_code == 302
|
||||||
|
assert response.location == url_for('.manage_org', _external=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_manage_orgs_shows_correct_org_info(visit_manage_org_with_org):
|
||||||
|
assert visit_manage_org_with_org.select_one('#logo-img > img')['src'].endswith('/' + sample_orgs[0]['logo'])
|
||||||
|
assert visit_manage_org_with_org.select_one('#name').attrs.get('value') == sample_orgs[0]['name']
|
||||||
|
assert visit_manage_org_with_org.select_one('#colour').attrs.get('value') == sample_orgs[0]['colour']
|
||||||
|
|
||||||
|
|
||||||
|
def test_manage_orgs_does_not_show_data_for_new_org(visit_manage_org_without_org):
|
||||||
|
assert visit_manage_org_without_org.select_one('div.page-footer > input.button').has_attr('disabled')
|
||||||
|
assert visit_manage_org_without_org.select_one('#logo-img > img') is None
|
||||||
|
assert visit_manage_org_without_org.select_one('#name').attrs.get('value') == ''
|
||||||
|
assert visit_manage_org_without_org.select_one('#colour').attrs.get('value') == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_is_enabled_when_logo_is_set(visit_manage_org_with_org):
|
||||||
|
assert visit_manage_org_with_org.select_one('div.page-footer > input.button').has_attr('disabled') is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_is_disabled_when_logo_is_set(visit_manage_org_without_org):
|
||||||
|
assert visit_manage_org_without_org.select_one('div.page-footer > input.button').has_attr('disabled')
|
||||||
|
|
||||||
|
|
||||||
|
def test_shows_temp_logo_after_uploading_logo(logged_in_platform_admin_client, mocker, fake_uuid):
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
user_id = session["user_id"]
|
||||||
|
|
||||||
|
temp_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'test.png')
|
||||||
|
|
||||||
|
mocker.patch('app.main.views.organisations.upload_logo', return_value=temp_filename)
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_file')
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_files_created_by')
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for('.manage_org'),
|
||||||
|
data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
||||||
|
content_type='multipart/form-data',
|
||||||
|
follow_redirects=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
|
||||||
|
assert page.select_one('#logo-img > img').attrs['src'].endswith(temp_filename)
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_enabled_after_loading_logo(logged_in_platform_admin_client, mocker, fake_uuid):
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
user_id = session["user_id"]
|
||||||
|
|
||||||
|
temp_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'test.png')
|
||||||
|
|
||||||
|
mocker.patch('app.main.views.organisations.upload_logo', return_value=temp_filename)
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_file')
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_files_created_by')
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for('.manage_org'),
|
||||||
|
data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
||||||
|
content_type='multipart/form-data',
|
||||||
|
follow_redirects=True
|
||||||
|
)
|
||||||
|
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
assert not page.select_one('div.page-footer > input.button').has_attr('disabled')
|
||||||
|
|
||||||
|
|
||||||
|
def test_allows_saving_after_uploading_logo():
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
user_id = session["user_id"]
|
||||||
|
|
||||||
|
temp_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'test.png')
|
||||||
|
|
||||||
|
mocker.patch('app.main.views.organisations.upload_logo', return_value=temp_filename)
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_file')
|
||||||
|
mocker.patch('app.main.views.organisations.delete_temp_files_created_by')
|
||||||
|
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for('.manage_org'),
|
||||||
|
data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
||||||
|
content_type='multipart/form-data',
|
||||||
|
follow_redirects=True
|
||||||
|
)
|
||||||
|
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
|
||||||
|
assert not page.select_one('div.page-footer > input.button').has_attr('disabled')
|
||||||
|
|
||||||
|
|
||||||
|
def test_deletes_previous_temp_logo_after_uploading_logo(logged_in_platform_admin_client, mocker, fake_uuid):
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
user_id = session["user_id"]
|
||||||
|
|
||||||
|
temp_old_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'old_test.png')
|
||||||
|
temp_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'test.png')
|
||||||
|
|
||||||
|
mocked_upload_logo = mocker.patch(
|
||||||
|
'app.main.views.organisations.upload_logo',
|
||||||
|
return_value=temp_filename
|
||||||
|
)
|
||||||
|
mocked_delete_temp_file = mocker.patch('app.main.views.organisations.delete_temp_file')
|
||||||
|
|
||||||
|
logged_in_platform_admin_client.post(
|
||||||
|
url_for('.manage_org', logo=temp_old_filename),
|
||||||
|
data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
||||||
|
content_type='multipart/form-data'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert mocked_upload_logo.called
|
||||||
|
assert mocked_delete_temp_file.called
|
||||||
|
assert mocked_delete_temp_file.call_args == call(temp_old_filename)
|
||||||
|
|
||||||
|
|
||||||
|
def test_logo_persisted_when_organisation_saved(logged_in_platform_admin_client, mocker, fake_uuid):
|
||||||
|
with logged_in_platform_admin_client.session_transaction() as session:
|
||||||
|
user_id = session["user_id"]
|
||||||
|
|
||||||
|
temp_filename = LOGO_LOCATION_STRUCTURE.format(TEMP_TAG.format(user_id), fake_uuid, 'test.png')
|
||||||
|
|
||||||
|
mocked_upload_logo = mocker.patch('app.main.views.organisations.upload_logo')
|
||||||
|
mocked_persist_logo = mocker.patch('app.main.views.organisations.persist_logo', return_value='test.png')
|
||||||
|
mocked_delete_temp_files_by = mocker.patch('app.main.views.organisations.delete_temp_files_created_by')
|
||||||
|
|
||||||
|
logged_in_platform_admin_client.post(
|
||||||
|
url_for('.manage_org', logo=temp_filename),
|
||||||
|
content_type='multipart/form-data'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert not mocked_upload_logo.called
|
||||||
|
assert mocked_persist_logo.called
|
||||||
|
assert mocked_delete_temp_files_by.called
|
||||||
|
assert mocked_delete_temp_files_by.call_args == call(user_id)
|
||||||
|
|
||||||
|
|
||||||
|
def test_shows_colour_when_valid_colour_entered():
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user