Commit Graph

20 Commits

Author SHA1 Message Date
Kenneth Kehl
8c9721d8e2 notify-api-412 use black to enforce python coding style 2023-08-25 09:12:23 -07:00
stvnrlly
f16b5dd1c4 remove broadcast-related code 2022-10-04 03:04:13 +00:00
Ben Thorner
5db2581669 Simplify tests for TemplateList class
In response to: [^1][^2][^3].

Originally there were three tests:

- One was testing the "get_user_template_folders" method directly.

- The other two were testing "get_template_folders", which calls
the "_user_" method if a user is passed - this is what the tests
were primarily checking, by passing or not passing a User object.

The two tests of "get_template_folders" also implicitly checked
that it filtered folders based on a "parent_folder_id". I wanted
to emulate the tests but it makes more sense to simplify them, as
the methods are now only used by TemplateList internally.

To simplify, we just keep just two tests to check that the overall
set of folders is filtered based on the presence of a User. We do
lose the implicit check of filtering by "parent_folder_id", but:

- We are still checking this implicitly in the sense that the loop
to generate the overall set of folders terminates. If the method
didn't do any filtering, the loop would be infinite.

- We need to acknowledge that these tests were incomplete to begin
with. There is also lots of coverage in higher level tests [^4],
although it's harder to reason exactly what is covered.

[^1]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890144076
[^2]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890151220
[^3]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890147506
[^4]: 1787f9f42f/tests/app/main/views/test_template_folders.py
2022-06-07 11:31:09 +01:00
Ben Thorner
1c4a2b4790 Cache templates and folders in TemplateList
This gives us the performance gains identified in [^1] for the test
service described in the spike:

- user_template_folders - from 10s to a little above 3s on its own

- get_templates_and_folders - from 10s to below 6s on its own

In combination, these two uses of caching reduce the test page load
time from 10s to a little above 3s. This is slightly higher than in
the spike PR due to all the extra work we're doing to generate the
"move to" list of folders, as described in a previous commit.

The render time is unchanged for services with few folders. We start
to see the benefit of this change at around 200 templates + folders,
with no evidence that any service will experience worse render times,
despite the extra work we're doing from previous commits.

[^1]: https://github.com/alphagov/notifications-admin/pull/4251
2022-06-07 11:05:36 +01:00
Ben Thorner
2771ae1274 Remove redundant user param from template methods
This will finally make it possible to cache some of the methods.
2022-06-07 11:05:35 +01:00
Ben Thorner
9126c8ca5e Move _template_ methods to TemplateList class
This is a straight move with a few minor tweaks:

- Some references to self.X in the code from Service now need to
become self.service.X.

- Some reference to self.service.Y in TemplateList can now become
self.Y for the methods that were migrated.

The remaining _template_ methods in Service are still called from
multiple places and there's no performance gain to motivate moving
them out of the Service class, which is now a more manageable size.
2022-06-07 11:05:34 +01:00
Ben Thorner
487dc1b488 Test _template_folders functions via TemplateList
This is part of the overall migration of the "_template_folders"
methods to the TemplateList class. Moving the existing tests now
will make the actual migration easier to follow.

To emulate the second and third tests, we need to grab a specific
folder from the TemplateList and then look at its folders - these
are set based on "get_template_folders" as before.
2022-06-07 11:05:30 +01:00
Ben Thorner
b97bf19b45 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]: 2e637f801f/app/main/views/templates.py (L356)
[^2]: bef0382cca/app/models/template_list.py (L31-L36)
2022-06-07 10:43:30 +01:00
Ben Thorner
a87138b9f9 Refactor loop over TemplateList items
This is a technique we'll use in later commits.
2022-06-06 10:36:49 +01:00
Ben Thorner
ad4ef12251 Reuse TemplateList to display move-to options
This is slightly less efficient than getting the folder dicts from
"get_user_template_folders" directly, since:

- TemplateList returns both templates and folders.
- TemplateList encapsulates the dicts in model classes.

We'll compensate for this later on:

- We'll introduce a new caching approach to make the call fast.
- We'll expose a property to avoid the "if" in the comprehension.
2022-06-06 10:36:48 +01:00
Chris Hill-Scott
5c9a886edc Preview content as hint for broadcast templates
Broadcast services only have broadcast templates. But we show the
template type under the name of the template. This is redundant. It
would be better to preview the content of the template instead.

This then makes the templates page consistent with the dashboard.

Depends on:
- [ ] https://github.com/alphagov/notifications-api/pull/2996
2020-10-14 13:21:19 +01:00
Chris Hill-Scott
01ec2ad9c7 Add template type hint on choose page 2020-07-03 15:47:29 +01:00
Chris Hill-Scott
71dc650db6 Make user model return a service model, not JSON
This makes it:
- nicer, by having access to sensibly named things like
  `Service.trial_mode` instead of `service['restricted']`.
- less likely to write Jinja code like `service.trail_mode`, which would
  fail silently if `service` was a dictionary
2019-06-13 13:47:28 +01:00
Alexey Bezhan
e6d7f7ebeb Add a user method to check folder permission
User model is the most natural place for a permission check method,
however this means that we need to pass the full user object to
service model methods and TemplateList instead of user_id.
2019-04-01 10:50:38 +01:00
Alexey Bezhan
3d401ce856 Hide current folder templates if user doesn't have a folder permission
TemplateList gets a list of templates in a current folder separately,
so we need to make sure `service.get_templates` checks for the
appropriate user permission
2019-03-11 14:30:50 +00:00
Alexey Bezhan
6638a0aecb Add user_id argument to TemplateList to allow filtering folders by user
Switches TemplateList to use get_user_template_folders by setting the
user_id.
2019-03-11 14:30:50 +00:00
Chris Hill-Scott
34f509af11 Fix empty folder message
Sometimes, when filtering the view by template type, the message was
saying the folder was empty, when really it should have been saying that
there were no templates of the ‘type’ you’re looking for, eg:
> There are no email templates in this folder

This was because it was looking at the filtered list of templates, not
all templates to determine whether a folder was really empty.
2019-02-05 14:28:11 +00:00
Chris Hill-Scott
206a7806d4 Allow users to navigate folders when copying
We already have a pattern for navigation folders and searching for
templates – let’s use it for the copy page too. And I reckon we can
represent services as folders if the user has multiple services they
could copy a template from.
2019-01-08 12:21:32 +00:00
Chris Hill-Scott
ee991d0142 Allow any sub-items to be moved from a folder
Since you can now see them when searching you should also be able to
select and move them. Which means that they needed to be included in
the `Form`’s list of possible choices of things to move.
2018-11-23 16:29:21 +00:00
Chris Hill-Scott
82d8207612 Refactor template list logic into one place
The Jinja template for the ‘choose templates’ page is now pulling in
data from a lot of diparate places in order to work out what to show. As
we add more logic about what to show (in order to make the live search
work) it’s going to get harder to have all this logic in the Jinja
template.

This commit refactors it back into Python where we have more language
features for managing complex logic.

It’s a bit weird to call this file a model, in that it’s dealing with
some presentational logic, rather than just data. Conceptually it’s more
like a view model[1].

1. https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
2018-11-23 14:00:40 +00:00