From 2743216519660415f1b4b5a764e119d7d284906c Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 8 Nov 2018 11:56:29 +0000 Subject: [PATCH] Show form elements for templates and folders This commit adds: - checkboxes to let you select a template or folder - radio buttons to let you select where to move those template(s) and/or folder(s) to It only does the `get` part of this work; handling the `post` and calling API will be done in a subsequent commit. --- .../stylesheets/components/message.scss | 15 +++++ app/main/forms.py | 45 +++++++++++++++ app/main/views/templates.py | 10 ++++ app/models/service.py | 6 ++ app/templates/components/checkbox.html | 23 ++++++-- app/templates/components/page-footer.html | 9 ++- app/templates/views/templates/_move_to.html | 7 +++ .../views/templates/_template_list.html | 38 +++++++++++++ app/templates/views/templates/choose.html | 38 +++++-------- tests/app/main/views/test_templates.py | 55 ++++++++++++++++++- 10 files changed, 215 insertions(+), 31 deletions(-) create mode 100644 app/templates/views/templates/_move_to.html create mode 100644 app/templates/views/templates/_template_list.html diff --git a/app/assets/stylesheets/components/message.scss b/app/assets/stylesheets/components/message.scss index db9f6a0a5..87c1198a8 100644 --- a/app/assets/stylesheets/components/message.scss +++ b/app/assets/stylesheets/components/message.scss @@ -23,3 +23,18 @@ #template-list { margin-top: $gutter; } + +.template-list-item, +.template-list-item-with-checkbox { + position: relative; +} + +.template-list-item-with-checkbox { + + padding-left: $gutter * 2; + + .multiple-choice { + position: absolute; + left: 0; + } +} diff --git a/app/main/forms.py b/app/main/forms.py index eec7a5daf..2e3e6a05e 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -22,6 +22,7 @@ from wtforms import ( IntegerField, PasswordField, RadioField, + SelectMultipleField, StringField, TextAreaField, ValidationError, @@ -30,6 +31,7 @@ from wtforms import ( ) from wtforms.fields.html5 import EmailField, SearchField, TelField from wtforms.validators import URL, DataRequired, Length, Optional, Regexp +from wtforms.widgets import CheckboxInput, ListWidget from app.main.validators import ( Blacklist, @@ -102,6 +104,11 @@ def get_next_days_until(until): ] +class MultiCheckboxField(SelectMultipleField): + widget = ListWidget(prefix_label=False) + option_widget = CheckboxInput() + + def email_address(label='Email address', gov_user=True): validators = [ Length(min=5, max=255), @@ -1111,3 +1118,41 @@ class ReturnedLettersForm(StripWhitespaceForm): class TemplateFolderForm(StripWhitespaceForm): name = StringField('Folder name', validators=[DataRequired(message='Can’t be empty')]) + + +class TemplateAndFoldersSelectionForm(Form): + + ALL_TEMPLATES_FOLDER = { + 'name': 'All templates', + 'id': None, + } + + def __init__( + self, + service, + template_type, + current_folder_id, + *args, + **kwargs + ): + + super().__init__(*args, **kwargs) + + self.templates_and_folders.choices = self.ids_and_names( + service.get_template_folders_and_templates(template_type, current_folder_id) + ) + + self.move_to.choices = self.ids_and_names( + [self.ALL_TEMPLATES_FOLDER] + service.all_template_folders, + exclude=current_folder_id, + ) + + @staticmethod + def ids_and_names(items, exclude=None): + return [ + (item['id'], item['name']) for item in items + if item['id'] != exclude + ] + + templates_and_folders = MultiCheckboxField('Choose templates or folders') + move_to = RadioField('Choose a folder') diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 8d7018bb7..7f938dc74 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -24,6 +24,7 @@ from app.main.forms import ( SearchTemplatesForm, SetTemplateSenderForm, SMSTemplateForm, + TemplateAndFoldersSelectionForm, TemplateFolderForm, ) from app.main.views.send import get_example_csv_rows, get_sender_details @@ -123,6 +124,10 @@ def choose_template(service_id, template_type='all', template_folder_id=None): return render_template( 'views/templates/choose.html', current_template_folder_id=template_folder_id, + can_manage_folders=( + current_service.has_permission('edit_folders') and + current_user.has_permissions('manage_templates') + ), template_folder_path=current_service.get_template_folder_path(template_folder_id), template_folders=current_service.get_template_folders(template_folder_id), templates=current_service.get_templates(template_type, template_folder_id), @@ -134,6 +139,11 @@ def choose_template(service_id, template_type='all', template_folder_id=None): template_nav_items=template_nav_items, template_type=template_type, search_form=SearchTemplatesForm(), + templates_and_folders_form=TemplateAndFoldersSelectionForm( + service=current_service, + template_type=template_type, + current_folder_id=template_folder_id, + ) ) diff --git a/app/models/service.py b/app/models/service.py index c0b6649b6..928ffbf1f 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -325,3 +325,9 @@ class Service(): path.append(folder) return list(reversed(path)) + + def get_template_folders_and_templates(self, template_type, template_folder_id): + return ( + self.get_templates(template_type, template_folder_id) + + self.get_template_folders(template_folder_id) + ) diff --git a/app/templates/components/checkbox.html b/app/templates/components/checkbox.html index ee2f8fb8d..0c6fcc843 100644 --- a/app/templates/components/checkbox.html +++ b/app/templates/components/checkbox.html @@ -4,12 +4,7 @@ width='2-3' ) %}
- + {{ checkbox_input(field.id, field.name, field.data) }}