Make creating TemplateListServices consistent

TemplateListServices are used when we want to show the service
as an additional layer of hierarchy when a user copies a template,
potentially across services [^1].

Normally a TemplateFolder is given "folders" and "templates" by
TemplateList [^2]; TemplateListService was doing it the other
way round and getting its own instead.

Using a subclass of TemplateList means we can make the approach
consistent, which will support the caching approach later on, as
well as simplifying how we work with templates and folders.

[^1]: https://github.com/alphagov/notifications-admin/blob/2e637f801f1f1ebfe1ff19fba7e19bbf40b7cfb4/app/main/views/templates.py#L356
[^2]: https://github.com/alphagov/notifications-admin/blob/bef0382cca16ba87d04430f8cd391092b1dc91ef/app/models/template_list.py#L31-L36
This commit is contained in:
Ben Thorner
2022-06-07 10:43:30 +01:00
parent a87138b9f9
commit b97bf19b45
+29 -20
View File
@@ -66,6 +66,28 @@ class TemplateList():
))
class ServiceTemplateList(TemplateList):
def __iter__(self):
template_list_service = TemplateListService(
self.service,
templates=self.service.get_templates(
template_folder_id=None,
),
folders=self.service.get_template_folders(
parent_folder_id=None,
),
)
yield template_list_service
yield from self.get_templates_and_folders(
self.template_type,
self.template_folder_id,
self.user,
ancestors=[template_list_service]
)
class TemplateLists():
def __init__(self, user):
@@ -84,20 +106,10 @@ class TemplateLists():
return
for service in self.services:
template_list_service = TemplateListService(service)
yield template_list_service
for service_templates_and_folders in TemplateList(
service, user=self.user
).get_templates_and_folders(
template_type='all',
template_folder_id=None,
yield from ServiceTemplateList(
service=service,
user=self.user,
ancestors=[template_list_service],
):
yield service_templates_and_folders
)
@property
def templates_to_show(self):
@@ -183,21 +195,18 @@ class TemplateListFolder(TemplateListItem):
class TemplateListService(TemplateListFolder):
is_service = True
def __init__(
self,
service,
templates,
folders,
):
super().__init__(
folder=service._dict,
templates=service.get_templates(
template_folder_id=None,
),
folders=service.get_template_folders(
parent_folder_id=None,
),
templates=templates,
folders=folders,
ancestors=[],
service_id=service.id,
)