Skip to content

Conversation

laymonage
Copy link
Member

@laymonage laymonage commented Feb 14, 2023

Fixes #10008, may also fix #1602 albeit with a different approach. Needs #10099. Likely closes #4702.

This PR ensures there's a link to the usage view from the delete and unpublish confirmation views. For the delete view, the link to the usage view has an extra ?describe_on_delete=1 parameter, which will make the usage view also describe what happens to the references of the object that's to be deleted.

The anchor linked in the specific fields currently still doesn't work as mentioned in #10007 (review). I'll try to make a separate PR if I find a way to fix it.

image

image

Performance optimisations

The first commit adds some performance optimisations when accessing the usage view. This is mostly done by using the ContentType model's default manager so that we use its cache instead of accessing the ForeignKey directly, which will query the database. Additional optimisation is also done for the count by making it a cached property.

Before

image

After

image

Please check the following:

  • Do the tests still pass?1
  • Does the code comply with the style guide?
    • Run make lint from the Wagtail root.
  • For Python changes: Have you added tests to cover the new/fixed behaviour?
  • For front-end changes: Did you test on all of Wagtail’s supported environments?2
    • Please list the exact browser and operating system versions you tested:
    • Please list which assistive technologies 3 you tested:
  • For new features: Has the documentation been updated accordingly?

Please describe additional details for testing this change.

Footnotes

  1. Development Testing

  2. Browser and device support

  3. Accessibility Target

@squash-labs
Copy link

squash-labs bot commented Feb 14, 2023

Manage this branch in Squash

Test this branch here: https://laymonageusage-before-delete-u-lqo95.squash.io

@laymonage laymonage added this to the 5.0 milestone Feb 14, 2023
@laymonage laymonage force-pushed the usage-before-delete-unpublish branch from 375393e to 9030c8f Compare February 16, 2023 15:25
@laymonage laymonage force-pushed the usage-before-delete-unpublish branch 3 times, most recently from aaf38cd to 991f5cf Compare February 21, 2023 16:32
@laymonage laymonage changed the title Show usage summary on delete and unpublish confirmation views Show on delete information in usage views to be linked from delete/unpublish confirmation views Feb 22, 2023
@laymonage laymonage force-pushed the usage-before-delete-unpublish branch from 991f5cf to f05fa2f Compare February 22, 2023 12:15
@laymonage laymonage marked this pull request as ready for review February 22, 2023 12:15
@laymonage laymonage requested a review from gasman February 22, 2023 12:15
label = f"{capfirst(field.verbose_name)}"
block = field.stream_block
block_idx = 1
while isinstance(block, StreamBlock):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note to pre-empt feature creep more than anything...

If we're intending to support recursing into blocks to any depth, then we probably ought to recognise StructBlock / ListBlock, and at that point the concept of 'following path components' should be made into a method on the Block API rather than matching individual block types here.

However, given that:

  1. it's probably uncommon for people to nest StreamBlocks directly in StreamBlocks
  2. if we explicitly write this code to not go more than one level, we'd probably want an if isinstance(block, StreamBlock) here anyhow to cover the possibility that we might support other top-level blocks besides StreamBlock in the near future (see "normalize" mechanism for StreamField blocks #9738)

...this code in its current form will in practice only descend one level most of the time, and changing it to actually enforce only going one level deep would remove a maybe-occasionally-useful feature for no real benefit. So for that reason, I'm happy to stick with this :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yep, I just realised nesting StreamBlock in a StreamBlock isn't very useful!

if we explicitly write this code to not go more than one level, we'd probably want an if isinstance(block, StreamBlock) here anyhow to cover the possibility that we might support other top-level blocks besides StreamBlock in the near future (see #9738)

That's exactly what I had initially, then I thought people might have a nested StreamBlock and the code to handle it wasn't that different apart from changing the if to while, so I made the call. Would definitely be great if we can normalise this across all "container" blocks, though!

Copy link
Contributor

@gasman gasman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one tiny issue I've found - the deletion confirmation message for documents doesn't display correctly.
Screenshot 2023-03-15 at 18 42 19

Other than that (and the comment update mentioned earlier), all looks good to me!

@gasman
Copy link
Contributor

gasman commented Mar 15, 2023

Sorry, just found another niggle - if an on_delete=PROTECT ForeignKey prevents an image from being deleted, the delete button is not hidden as it is for the document / snippet / page confirmation views, and clicking it gives a "Sorry, you do not have permission to access this area" error.

Screenshot 2023-03-15 at 19 17 12

@laymonage laymonage force-pushed the usage-before-delete-unpublish branch from f05fa2f to d75b89c Compare March 16, 2023 10:32
@laymonage
Copy link
Member Author

Thanks @gasman!

I've rebased, and made these commits as fixups to the relevant commits (you won't see them in this PR as they've been used to amend the original commits):

Tests for deleting protected images and documents have been added in f51a873.

@laymonage laymonage requested a review from gasman March 16, 2023 15:42

return TemplateResponse(
request,
"wagtaildocs/documents/confirm_delete.html",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should override get_templates() and look for this template before self.template_name in case people override this template?

I think it's unlikely people override this template though...

We don't have to do this for images and pages because we are still using their templates instead of the generic one. As for snippets, the work in #10206 should cover template customisation in a more robust way.

@gasman
Copy link
Contributor

gasman commented Mar 21, 2023

In laymonage@20f3b28 the confirmation message needs to be inside the {% if not is_protected %} block too, I think:
Screenshot 2023-03-21 at 10 51 30

Make use of cached properties and ContentType's manager cache
Show the block path and label to better understand which block has the reference
@laymonage laymonage force-pushed the usage-before-delete-unpublish branch from f51a873 to 568aa64 Compare March 21, 2023 12:10
@laymonage
Copy link
Member Author

laymonage commented Mar 21, 2023

@gasman Sorry about that, should've noticed that mistake 😣

Fixed in 568aa64 with a test.

@lb-
Copy link
Member

lb- commented Mar 21, 2023

This is amazing stuff. Thanks @laymonage and @gasman

@kedmundson
Copy link

Great to see this making it into core!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show usage summary when unpublishing/deleting an object Support for PROTECT on foreign keys
4 participants