Reflect template folder structure on inbound conversation reply page

This commit is contained in:
Pea Tyczynska
2019-03-21 15:57:52 +00:00
parent 3fc4f6866c
commit 0743a68e09
5 changed files with 67 additions and 108 deletions

View File

@@ -7,6 +7,7 @@ from notifications_utils.template import SMSPreviewTemplate
from app import current_service, notification_api_client, service_api_client
from app.main import main
from app.main.forms import SearchByNameForm
from app.models.template_list import TemplateList
from app.utils import user_has_permissions
@@ -38,20 +39,26 @@ def conversation_updates(service_id, notification_id):
@main.route("/services/<service_id>/conversation/<notification_id>/reply-with")
@main.route("/services/<service_id>/conversation/<notification_id>/reply-with/from-folder/<uuid:from_folder>")
@login_required
@user_has_permissions('send_messages')
def conversation_reply(
service_id,
notification_id,
from_folder=None,
):
templates = current_service.get_user_templates_across_folders(current_user.id, template_type='sms')
return render_template(
'views/templates/choose-reply.html',
templates=templates,
show_search_box=(len(templates) > 7),
template_type='sms',
templates_and_folders=TemplateList(
current_service,
template_folder_id=from_folder,
user_id=current_user.id,
template_type='sms'
),
template_folder_path=current_service.get_template_folder_path(from_folder),
search_form=SearchByNameForm(),
notification_id=notification_id,
template_type='sms'
)

View File

@@ -142,6 +142,7 @@ class Service():
@cached_property
def all_templates(self):
templates = service_api_client.get_service_templates(self.id)['data']
return [
@@ -153,15 +154,6 @@ class Service():
def all_template_ids(self):
return {template['id'] for template in self.all_templates}
def get_templates_for_folder(self, template_type, all_templates, folder_id):
if isinstance(template_type, str):
template_type = [template_type]
return [
template for template in all_templates
if (set(template_type) & {'all', template['template_type']})
and (template.get('folder') == folder_id)
]
def get_templates(self, template_type='all', template_folder_id=None, user_id=None):
if user_id and template_folder_id and self.has_permission('edit_folder_permissions'):
folder = self.get_template_folder(template_folder_id)
@@ -172,17 +164,11 @@ class Service():
template_type = [template_type]
if template_folder_id:
template_folder_id = str(template_folder_id)
return self.get_templates_for_folder(template_type, self.all_templates, template_folder_id)
def get_user_templates_across_folders(self, user_id, template_type='all'):
folders = self.all_template_folders
all_templates = self.all_templates
user_templates = []
user_templates += self.get_templates_for_folder(template_type, all_templates, None)
for folder in folders:
if user_id in folder.get("users_with_permission", []):
user_templates += self.get_templates_for_folder(template_type, all_templates, folder["id"])
return user_templates
return [
template for template in self.all_templates
if (set(template_type) & {'all', template['template_type']})
and template.get('folder') == template_folder_id
]
@property
def available_template_types(self):

View File

@@ -2,6 +2,7 @@
{% from "components/message-count-label.html" import message_count_label %}
{% from "components/textbox.html" import textbox %}
{% from "components/live-search.html" import live_search %}
{% from "components/folder-path.html" import folder_path %}
{% extends "withnav_template.html" %}
@@ -11,9 +12,12 @@
{% block maincolumn_content %}
<div class="bottom-gutter-1-2">
<h1 class="heading-large">Choose a template</h1>
{{ folder_path(template_folder_path, current_service.id, template_type) }}
</div>
{% if not templates %}
{% if not templates_and_folders.templates_to_show %}
{% if current_user.has_permissions('manage_templates') %}
<p class="bottom-gutter">
@@ -29,20 +33,35 @@
{% else %}
{{ live_search(target_selector='#template-list .column-whole', show=show_search_box, form=search_form) }}
{{ live_search(target_selector='#template-list .column-whole', show=True, form=search_form) }}
<nav class="grid-row" id=template-list>
{% for template in templates %}
<div class="column-whole">
<nav id="template-list">
{% for item in templates_and_folders %}
<div class="template-list-item {% if item.ancestors %}template-list-item-hidden-by-default{% endif %} {% if not item.ancestors %}template-list-item-without-ancestors{% endif %}">
<h2 class="message-name">
<a href="{{ url_for('.conversation_reply_with_template', service_id=current_service.id, template_id=template.id, notification_id=notification_id) }}">{{ template.name }}</a>
{% for ancestor in item.ancestors %}
<a href="{{ url_for('.conversation_reply', service_id=current_service.id, notification_id=notification_id, from_folder=ancestor.id) }}" class="template-list-folder">
{{ ancestor.name }}
</a> <span class="message-name-separator">/</span>
{% endfor %}
{% if item.is_folder %}
<a href="{{ url_for('.conversation_reply', service_id=current_service.id, notification_id=notification_id, from_folder=item.id) }}" class="template-list-folder">
<span class="live-search-relevant">{{ item.name }}</span>
</a>
{% else %}
<a href="{{ url_for('.conversation_reply_with_template', service_id=current_service.id, template_id=item.id, notification_id=notification_id) }}">
<span class="live-search-relevant">{{ item.name }}</span>
</a>
{% endif %}
</h2>
<p class="message-type">
{{ message_count_label(1, template.template_type, suffix='')|capitalize }} template
{{ item.hint }}
</p>
</div>
{% endfor %}
</nav>
{% endif %}
{% endblock %}

View File

@@ -260,6 +260,7 @@ def test_conversation_reply_shows_link_to_add_templates_if_service_has_no_templa
fake_uuid,
mock_get_service_templates_when_no_templates_exist,
mock_get_template_folders,
active_user_with_permissions
):
page = client_request.get(
'main.conversation_reply',
@@ -282,9 +283,11 @@ def test_conversation_reply_shows_templates(
fake_uuid,
mocker,
mock_get_template_folders,
active_user_with_permissions
active_user_with_permissions,
service_one
):
service_one["permissions"] += ["edit_folder_permissions"]
all_templates = {'data': [
_template('sms', 'sms_template_one', parent=INV_PARENT_FOLDER_ID),
_template('sms', 'sms_template_two'),
@@ -312,38 +315,30 @@ def test_conversation_reply_shows_templates(
notification_id=fake_uuid,
)
for index, expected in enumerate([
'sms_template_two',
'sms_template_three'
]):
link = page.select('.message-name')[index]
assert normalize_spaces(link.text) == expected
assert link.select_one('a')['href'].startswith(
url_for(
'main.conversation_reply_with_template',
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
template_id='',
)
link = page.select('.template-list-item-without-ancestors')
assert normalize_spaces(link[0].text) == "Parent 2 - visible 1 template"
assert normalize_spaces(link[1].text) == 'sms_template_two Text message template'
assert link[0].select_one('a')['href'].startswith(
url_for(
'main.conversation_reply',
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
from_folder=VIS_PARENT_FOLDER_ID
)
def test_conversation_reply_shows_live_search_if_list_of_templates_taller_than_screen(
client_request,
fake_uuid,
mock_get_more_service_templates_than_can_fit_onscreen,
mock_get_template_folders,
):
page = client_request.get(
'main.conversation_reply',
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
)
assert page.select('.live-search')
assert link[1].select_one('a')['href'].startswith(
url_for(
'main.conversation_reply_with_template',
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
template_id='',
)
)
def test_conversation_reply_shows_live_search_if_list_of_templates_fits_onscreen(
def test_conversation_reply_shows_live_search(
client_request,
fake_uuid,
mock_get_service_templates,
@@ -355,7 +350,7 @@ def test_conversation_reply_shows_live_search_if_list_of_templates_fits_onscreen
notification_id=fake_uuid,
)
assert not page.select('.live-search')
assert page.select('.live-search')
def test_conversation_reply_redirects_with_phone_number_from_notification(

View File

@@ -1,7 +1,6 @@
import uuid
from app.models.service import Service
from tests.conftest import _template
INV_PARENT_FOLDER_ID = '7e979e79-d970-43a5-ac69-b625a8d147b0'
INV_CHILD_1_FOLDER_ID = '92ee1ee0-e4ee-4dcc-b1a7-a5da9ebcfa2b'
@@ -173,50 +172,3 @@ def test_get_template_folders_shows_all_folders_when_user_id_not_passed_in(
'users_with_permission': [active_user_with_permissions.id]
}
]
def test_get_user_templates_across_folders(
mock_get_template_folders,
service_one,
active_user_with_permissions,
mocker
):
all_templates = {'data': [
_template('sms', 'sms_template_one', parent=INV_CHILD_1_FOLDER_ID),
_template('sms', 'sms_template_two'),
_template('email', 'email_template_one', parent=VIS_PARENT_FOLDER_ID),
_template('letter', 'letter_template_one')
]}
mock_get_template_folders.return_value = _get_all_folders(active_user_with_permissions)
mocker.patch('app.service_api_client.get_service_templates', return_value=all_templates)
service_one['permissions'] = ['edit_folder_permissions', 'letter', 'email', 'sms']
service = Service(service_one)
result = service.get_user_templates_across_folders(active_user_with_permissions.id)
assert result == [
{'folder': None, 'id': mocker.ANY,
'name': 'sms_template_two', 'template_type': 'sms'},
{'folder': None, 'id': mocker.ANY,
'name': 'letter_template_one', 'template_type': 'letter'},
{'folder': 'bbbb222b-2b22-2b22-222b-b222b22b2222', 'id': mocker.ANY,
'name': 'email_template_one', 'template_type': 'email'}
]
def test_get_user_templates_across_folders_sms_only(
mock_get_template_folders,
service_one,
active_user_with_permissions,
mocker
):
all_templates = {'data': [
_template('sms', 'sms_template_one', parent=INV_CHILD_1_FOLDER_ID),
_template('sms', 'sms_template_two'),
_template('email', 'email_template_one', parent=VIS_PARENT_FOLDER_ID),
_template('letter', 'letter_template_one')
]}
mock_get_template_folders.return_value = _get_all_folders(active_user_with_permissions)
mocker.patch('app.service_api_client.get_service_templates', return_value=all_templates)
service_one['permissions'] = ['edit_folder_permissions', 'letter', 'email', 'sms']
service = Service(service_one)
result = service.get_user_templates_across_folders(active_user_with_permissions.id, template_type='sms')
assert result == [{'folder': None, 'id': mocker.ANY, 'name': 'sms_template_two', 'template_type': 'sms'}]