Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference index - rebase of #9170 #9279

Closed
wants to merge 19 commits into from

Conversation

gasman
Copy link
Collaborator

@gasman gasman commented Oct 4, 2022

#9170 rebased with additional fixes:

  • Objects listed in the usage report now have their titles hidden if the user doesn't have edit access for them
  • The title text on those links now correctly reflects the content type, rather than always being "Edit this page"
  • Indexing base models such as Page (in rebuild_references_index and elsewhere) no longer wrongly removes references belonging to child models
  • Document links in rich text are now picked up (the document link handler was missing extract_references)
  • Usage link on the snippet edit page (previously unstyled in the form body) is now moved to the status side panel

kaedroho and others added 18 commits October 4, 2022 15:57
MySQL doesn't allow long fields to be used in unique indexes.
Co-authored-by: Matt Westcott <matthew@torchbox.com>
…belonging to a more-specific one

(Mostly) fixes the issue described in wagtail#9170 (comment). When deleting old references within `ReferenceIndex.create_or_update_for_object`, skip over any with a source content_type that does not match the currently-indexed object or any of its superclasses - these can be assumed to come from a more specific version of the object, with relations that we don't know about from inspecting the less-specific one.

This does have the side effect that once an object has been indexed in its more specific form, any invocations of create_or_update_for_object on the less-specific model will be over-cautious, and fail to delete records even if they legitimately refer to relations that exist on the base model - i.e. those references will stick around until the more specific model is indexed. This is a lesser bug than the original, though, and running `rebuild_references_index` will make the index consistent with the database state.
@gasman gasman added this to the 4.1 milestone Oct 4, 2022
@gasman gasman requested a review from kaedroho October 4, 2022 17:48
@squash-labs
Copy link

squash-labs bot commented Oct 4, 2022

Manage this branch in Squash

Test this branch here: https://gasmankaedroho-reference-index-vkxp2.squash.io

@gasman gasman mentioned this pull request Oct 5, 2022
Copy link
Contributor

@kaedroho kaedroho left a comment

Choose a reason for hiding this comment

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

The changes look good to me, thanks!

Just one comment but it's not blocking.

edit_link_title = None
else:
label = str(object)
edit_link_title = _("Edit this %s") % object._meta.verbose_name
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if it's possible to word this in a way to make it easier to translate for languages with genders? (we've probably got a lot of cases like this so maybe this should be solved separately)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, this is already a frequent issue in the Wagtail codebase, particularly within modeladmin and snippets. #7117 raises this and a couple of similar issues.

Copy link
Contributor

@zerolab zerolab left a comment

Choose a reason for hiding this comment

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

a couple of non-block notes from me (we discussed them earlier)


@classmethod
def extract_references(cls, attrs):
yield cls.get_model(), attrs["id"], "", ""
Copy link
Contributor

Choose a reason for hiding this comment

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

was mentioning this earlier, but this would be a great to add type annotations to this, and perhaps use a TypedDict as it takes some cognitive load get this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Have added some supporting comments in 0c2dee3 (but the definitive description of the data format is still on the docstring for ReferenceIndex._extract_references_from_object, as I think it's best to avoid repeating this too much)

Copy link
Contributor

Choose a reason for hiding this comment

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

🌟


# Find existing references in the database so we know what to add/delete
existing_references = {
(to_content_type_id, to_object_id, model_path, content_path): (
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick from my tired self: this comprehension and the one below (deleted_reference_ids) while smart, they also make the code more difficult to parse

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added extra comments in 0c2dee3 and turned the second comprehension into a plain loop - hopefully that makes it easier to follow.

Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

gasman added a commit that referenced this pull request Oct 5, 2022
@gasman
Copy link
Collaborator Author

gasman commented Oct 5, 2022

Merged in 4c7e1ea

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.

None yet

3 participants