Put templates on service model

We do a lot of logic around choosing which templates to show. This logic
is all inside one view method.

It makes it cleaner to break this logic up into functions. But this
would mean passing around variables from one function to another.
Putting these methods onto a class (the service model) means that
there’s a place to store this data (rather than having to pass it around
a lot).

Making this code more manageable is important so that when we have
templates and folders it’s easy to encapsulate the logic around
combining the two.
This commit is contained in:
Chris Hill-Scott
2018-10-25 07:59:50 +01:00
parent d69e8b50cd
commit 1e6b79a546
6 changed files with 86 additions and 75 deletions

View File

@@ -319,25 +319,47 @@ class Service(dict):
) > 1
@property
def has_templates(self):
def templates(self):
from app import service_api_client
return service_api_client.count_service_templates(
self.id
) > 0
templates = service_api_client.get_service_templates(self.id)['data']
return [
template for template in templates
if template['template_type'] in self.available_template_types
]
def templates_by_type(self, template_type):
return [
template for template in self.templates
if template_type in {'all', template['template_type']}
]
@property
def available_template_types(self):
return [
channel for channel in ('email', 'sms', 'letter')
if self.has_permission(channel)
]
@property
def has_templates(self):
return len(self.templates) > 0
@property
def has_multiple_template_types(self):
return len({
template['template_type'] for template in self.templates
}) > 1
@property
def has_email_templates(self):
from app import service_api_client
return service_api_client.count_service_templates(
self.id, template_type='email'
) > 0
return len(self.templates_by_type('email')) > 0
@property
def has_sms_templates(self):
from app import service_api_client
return service_api_client.count_service_templates(
self.id, template_type='sms'
) > 0
return len(self.templates_by_type('sms')) > 0
@property
def has_email_reply_to_address(self):