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 %}