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

@@ -68,7 +68,7 @@ class Service():
) > 1
@cached_property
def templates(self):
def all_templates(self):
templates = service_api_client.get_service_templates(self.id)['data']
@@ -77,12 +77,14 @@ class Service():
if template['template_type'] in self.available_template_types
]
def templates_by_type(self, template_type):
def get_templates(self, template_type='all', template_folder_id=None):
if isinstance(template_type, str):
template_type = [template_type]
return [
template for template in self.templates
if set(template_type) & {'all', template['template_type']}
template for template in self.all_templates
if (set(template_type) & {'all', template['template_type']})
and template.get('folder_id') == template_folder_id
]
@property
@@ -94,21 +96,21 @@ class Service():
@property
def has_templates(self):
return len(self.templates) > 0
return len(self.all_templates) > 0
@property
def has_multiple_template_types(self):
return len({
template['template_type'] for template in self.templates
template['template_type'] for template in self.all_templates
}) > 1
@property
def has_email_templates(self):
return len(self.templates_by_type('email')) > 0
return len(self.get_templates('email')) > 0
@property
def has_sms_templates(self):
return len(self.templates_by_type('sms')) > 0
return len(self.get_templates('sms')) > 0
@cached_property
def email_reply_to_addresses(self):