Add Service.get_templates method with filters by type and folder

With the addition of template folders we need to filter templates
based on a combination of type and parent folder ID.

This replaces the existing `templates_by_type` method with
`get_templates`, which supports both type and parent folder filters,
avoiding a need to create specific methods for each use case.

We still need the templates property to exist in some way in order
to cache it, but it needs to be clear that it's different from
`.get_templates`. One option was to make it "private" (i.e. `_templates`),
and always use `.get_templates` in the rest of the code, but this requires
adding "include all folders" to `.get_templates`, which doesn't have an
obvious interface since `parent_folder_id=None` already means "top-level
only".

This will probably come up again when we need to look into adding
templates from nested folders into the page for live search, but
for now renaming `Service.templates` to `.all_templates` makes it
clear what the property contains.
This commit is contained in:
Alexey Bezhan
2018-11-05 15:26:59 +00:00
parent 078595da9d
commit 29bed8ba55
6 changed files with 25 additions and 23 deletions

View File

@@ -44,7 +44,7 @@ def conversation_reply(
service_id,
notification_id,
):
templates = current_service.templates_by_type('sms')
templates = current_service.get_templates('sms')
return render_template(
'views/templates/choose-reply.html',
templates=templates,

View File

@@ -118,11 +118,11 @@ def choose_template(service_id, template_type='all'):
return render_template(
'views/templates/choose.html',
template_folders=current_service.template_folders,
templates=current_service.templates_by_type(template_type),
show_search_box=(len(current_service.templates_by_type(template_type)) > 7),
templates=current_service.get_templates(template_type),
show_search_box=(len(current_service.get_templates(template_type)) > 7),
show_template_nav=(
current_service.has_multiple_template_types
and (len(current_service.templates) > 2)
and (len(current_service.all_templates) > 2)
),
template_nav_items=template_nav_items,
template_type=template_type,