mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-05 14:11:41 -04:00
Add option to copy existing template when adding
Sometimes when setting up a service you might have a few very similar templates, in which only a small amount of content. Or you might even have a few of services, which are used by different teams but have similar templates. Copy and pasting, especially from one service to another, is a pain. This commit makes it easier by allowing users to copy an existing template when choosing to add a new one, instead of starting from scratch.
This commit is contained in:
@@ -837,14 +837,15 @@ class ChooseTemplateType(StripWhitespaceForm):
|
||||
]
|
||||
)
|
||||
|
||||
def __init__(self, include_letters=False, *args, **kwargs):
|
||||
def __init__(self, include_letters=False, include_copy=False, *args, **kwargs):
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.template_type.choices = filter(None, [
|
||||
('email', 'Email'),
|
||||
('sms', 'Text message'),
|
||||
('letter', 'Letter') if include_letters else None
|
||||
('letter', 'Letter') if include_letters else None,
|
||||
('copy-existing', 'Copy of an existing template') if include_copy else None,
|
||||
])
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,12 @@ from notifications_python_client.errors import HTTPError
|
||||
from notifications_utils.formatters import nl2br
|
||||
from notifications_utils.recipients import first_column_headings
|
||||
|
||||
from app import current_service, service_api_client, template_statistics_client
|
||||
from app import (
|
||||
current_service,
|
||||
service_api_client,
|
||||
template_statistics_client,
|
||||
user_api_client,
|
||||
)
|
||||
from app.main import main
|
||||
from app.main.forms import (
|
||||
ChooseTemplateType,
|
||||
@@ -202,11 +207,21 @@ def view_template_version_preview(service_id, template_id, version, filetype):
|
||||
def add_template_by_type(service_id):
|
||||
|
||||
form = ChooseTemplateType(
|
||||
include_letters='letter' in current_service['permissions']
|
||||
include_letters='letter' in current_service['permissions'],
|
||||
include_copy=any((
|
||||
service_api_client.count_service_templates(service_id),
|
||||
len(user_api_client.get_service_ids_for_user(current_user)) > 1,
|
||||
)),
|
||||
)
|
||||
|
||||
if form.validate_on_submit():
|
||||
|
||||
if form.template_type.data == 'copy-existing':
|
||||
return redirect(url_for(
|
||||
'.choose_template_to_copy',
|
||||
service_id=service_id,
|
||||
))
|
||||
|
||||
if form.template_type.data == 'letter':
|
||||
blank_letter = service_api_client.create_service_template(
|
||||
'Untitled',
|
||||
@@ -240,6 +255,51 @@ def add_template_by_type(service_id):
|
||||
return render_template('views/templates/add.html', form=form)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/copy")
|
||||
@login_required
|
||||
@user_has_permissions('manage_templates')
|
||||
def choose_template_to_copy(service_id):
|
||||
return render_template(
|
||||
'views/templates/copy.html',
|
||||
services=[{
|
||||
'name': service['name'],
|
||||
'id': service['id'],
|
||||
'templates': [
|
||||
template for template in
|
||||
service_api_client.get_service_templates(service['id'])['data']
|
||||
if template['template_type'] in current_service['permissions']
|
||||
],
|
||||
} for service in user_api_client.get_services_for_user(current_user)],
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/copy/<uuid:template_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@user_has_permissions('manage_templates')
|
||||
def copy_template(service_id, template_id):
|
||||
|
||||
if not user_api_client.user_belongs_to_service(
|
||||
current_user, request.args.get('from_service')
|
||||
):
|
||||
abort(403)
|
||||
|
||||
template = service_api_client.get_service_template(
|
||||
request.args.get('from_service'),
|
||||
str(template_id),
|
||||
)['data']
|
||||
template['template_content'] = template['content']
|
||||
template['name'] = 'Copy of ‘{}’'.format(template['name'])
|
||||
form = form_objects[template['template_type']](**template)
|
||||
|
||||
return render_template(
|
||||
'views/edit-{}-template.html'.format(template['template_type']),
|
||||
form=form,
|
||||
template_type=template['template_type'],
|
||||
heading_action='Add',
|
||||
services=user_api_client.get_service_ids_for_user(current_user),
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/action-blocked/<notification_type>/<return_to>/<template_id>")
|
||||
@login_required
|
||||
@user_has_permissions('manage_templates')
|
||||
|
||||
Reference in New Issue
Block a user