Refactor template navigation options into model

This has two advantages:
- less logic in the view
- lets us filer the available navigation by which channels (email, text
  letter) a service has available
This commit is contained in:
Chris Hill-Scott
2018-11-08 11:54:57 +00:00
parent 4df96f3682
commit 921de47164
2 changed files with 15 additions and 7 deletions

View File

@@ -117,12 +117,7 @@ def choose_template(service_id, template_type='all', template_folder_id=None):
), ),
'' ''
) )
for label, key in filter(None, [ for label, key in [('All', 'all')] + current_service.available_template_types_as_tuples
('All', 'all'),
('Text message', 'sms'),
('Email', 'email'),
('Letter', 'letter') if current_service.has_permission('letter') else None,
])
] ]
return render_template( return render_template(

View File

@@ -36,6 +36,12 @@ class Service():
'service_callback_api', 'service_callback_api',
} }
TEMPLATE_TYPES = (
('sms', 'Text message'),
('email', 'Email'),
('letter', 'Letter'),
)
def __init__(self, _dict): def __init__(self, _dict):
# in the case of a bad request current service may be `None` # in the case of a bad request current service may be `None`
self._dict = _dict or {} self._dict = _dict or {}
@@ -116,10 +122,17 @@ class Service():
@property @property
def available_template_types(self): def available_template_types(self):
return [ return [
channel for channel in ('email', 'sms', 'letter') channel for channel, _ in self.TEMPLATE_TYPES
if self.has_permission(channel) if self.has_permission(channel)
] ]
@property
def available_template_types_as_tuples(self):
return [
(label, value) for value, label in self.TEMPLATE_TYPES
if self.has_permission(value)
]
@property @property
def has_templates(self): def has_templates(self):
return len(self.all_templates) > 0 return len(self.all_templates) > 0