Add organisations pages to show orgs and create/edit them

This commit is contained in:
Ken Tsang
2018-02-08 12:19:21 +00:00
parent b11bfd49e3
commit f0e8661e34
6 changed files with 228 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ from app.main.views import ( # noqa
letter_jobs,
email_branding,
conversation,
organisations,
notifications,
inbound_number
)

View File

@@ -665,6 +665,11 @@ class ServiceCreateEmailBranding(StripWhitespaceForm):
file = FileField_wtf('Upload a PNG logo', validators=[FileAllowed(['png'], 'PNG Images only!')])
class CreateOrUpdateOrganisation(StripWhitespaceForm):
name = StringField('Name', validators=[DataRequired()])
class LetterBranding(StripWhitespaceForm):
def __init__(self, choices=[], *args, **kwargs):

View File

@@ -0,0 +1,63 @@
from flask import redirect, render_template, url_for
from flask_login import login_required
from app import organisations_client
from app.main import main
from app.main.forms import CreateOrUpdateOrganisation
from app.utils import user_has_permissions
@main.route("/organisations", methods=['GET'])
@login_required
@user_has_permissions(admin_override=True)
def organisations():
orgs = organisations_client.get_organisations()
return render_template(
'views/organisations/organisations.html',
organisations=orgs
)
@main.route("/organisation/<org_id>/edit", methods=['GET', 'POST'])
@login_required
@user_has_permissions(admin_override=True)
def update_organisation(org_id):
org = organisations_client.get_organisation(org_id)
form = CreateOrUpdateOrganisation()
if form.validate_on_submit():
organisations_client.update_organisation(
org_id=org_id,
name=form.name.data
)
return redirect(url_for('.organisations'))
form.name.data = org['name']
return render_template(
'views/organisations/manage-organisation.html',
form=form,
organisation=org
)
@main.route("/organisations/create", methods=['GET', 'POST'])
@login_required
@user_has_permissions(admin_override=True)
def create_organisation():
form = CreateOrUpdateOrganisation()
if form.validate_on_submit():
organisations_client.create_organisation(
name=form.name.data,
)
return redirect(url_for('.organisations'))
return render_template(
'views/organisations/manage-organisation.html',
form=form
)

View File

@@ -0,0 +1,27 @@
{% extends "views/platform-admin/_base_template.html" %}
{% from "components/page-footer.html" import page_footer %}
{% from "components/textbox.html" import textbox, colour_textbox %}
{% block service_page_title %}
{{ '{} an organisations'.format('Update' if organisation else 'Create')}}
{% endblock %}
{% block platform_admin_content %}
<h1 class="heading-large">{{ '{} an organisation'.format('Update' if organisation else 'Create')}}</h1>
<div class="grid-row">
<div class="column-three-quarters">
<form method="post">
<div class="form-group">
<div style='margin-top:15px;'>{{textbox(form.name)}}</div>
{{ page_footer(
'Save',
back_link=url_for('.organisations'),
back_link_text='Back to organisations',
) }}
</div>
</form>
</div>
</div>
{% endblock %}

View File

@@ -0,0 +1,28 @@
{% extends "views/platform-admin/_base_template.html" %}
{% from "components/page-footer.html" import page_footer %}
{% block service_page_title %}
Show organisations
{% endblock %}
{% block platform_admin_content %}
<h1 class="heading-large">
<div>Organisations</div>
</h1>
<div class="grid-row">
<div class="column-three-quarters">
{% for org in organisations %}
<div>
<a href="{{ url_for('main.update_organisation', org_id=org['id']) }}" class="browse-list-link">{{ org['name'] }}</a>
{% if not org['active'] %}
<span class="table-field-status-default heading-medium">- archived</span>
{% endif %}
</div>
{% endfor %}
<br/>
<div><a href="{{ url_for('main.create_organisation') }}" class="browse-list-link">Create an organisation</a></div>
</div>
</div>
{% endblock %}

View File

@@ -0,0 +1,104 @@
from bs4 import BeautifulSoup
from flask import url_for
from tests.conftest import (
normalize_spaces
)
def test_organisation_page_shows_all_organisations(
logged_in_platform_admin_client,
mocker
):
orgs = [
{'id': '1', 'name': 'Test 1', 'active': True},
{'id': '2', 'name': 'Test 2', 'active': True},
{'id': '3', 'name': 'Test 3', 'active': False},
]
mocker.patch(
'app.organisations_client.get_organisations', return_value=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 normalize_spaces(
page.select_one('h1').text
) == "Organisations"
for index, org in enumerate(orgs):
assert (page.select('a.browse-list-link')[index]).text == org['name']
if not org['active']:
assert (page.select_one('.table-field-status-default,heading-medium')).text == '- archived'
assert normalize_spaces((page.select('a.browse-list-link')[-1]).text) == 'Create an organisation'
def test_edit_organisation_shows_the_correct_organisation(
logged_in_platform_admin_client,
fake_uuid,
mocker
):
org = {'id': fake_uuid, 'name': 'Test 1', 'active': True}
mocker.patch(
'app.organisations_client.get_organisation', return_value=org
)
response = logged_in_platform_admin_client.get(
url_for('.update_organisation', org_id=fake_uuid)
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.select_one('#name').attrs.get('value') == org['name']
def test_create_new_organisation(
logged_in_platform_admin_client,
mocker,
fake_uuid
):
mock_create_organisation = mocker.patch(
'app.organisations_client.create_organisation'
)
org = {'name': 'new name'}
logged_in_platform_admin_client.post(
url_for('.create_organisation'),
content_type='multipart/form-data',
data=org
)
mock_create_organisation.assert_called_once_with(name=org['name'])
def test_update_organisation(
logged_in_platform_admin_client,
mocker,
fake_uuid,
):
org = {'name': 'new name'}
mocker.patch(
'app.organisations_client.get_organisation', return_value=org
)
mock_update_organisation = mocker.patch(
'app.organisations_client.update_organisation'
)
logged_in_platform_admin_client.post(
url_for('.update_organisation', org_id=fake_uuid),
content_type='multipart/form-data',
data=org
)
assert mock_update_organisation.called
mock_update_organisation.assert_called_once_with(
org_id=fake_uuid,
name=org['name']
)