mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-20 14:29:51 -04:00
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:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user