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

Automatically generate filterset_class for snippets and generic IndexView through list_filter #10256

Closed
wants to merge 6 commits into from

Conversation

laymonage
Copy link
Member

@laymonage laymonage commented Mar 22, 2023

As part of wagtail/rfcs#85.

This PR adds a get_filterset_class on the generic IndexView that automatically generates a WagtailFilterSet subclass with the fields defined in IndexView.list_filter. The API is also surfaced in the SnippetViewSet class to ease customisation for Snippets' IndexView.

For Snippets' IndexView, the get_filterset_class() method works by calling SnippetViewSet.get_filterset_class() first. If the result is None, it then falls back to the generic IndexView.get_filterset_class() implementation where the class is dynamically generated if necessary.

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 new features: Has the documentation been updated accordingly?

Please describe additional details for testing this change.

Continuing from the test example in #10241.

from wagtail.snippets.models import register_snippet
from wagtail.snippets.views.snippets import SnippetViewSet


class PersonViewSet(SnippetViewSet):
    icon = "user"
    list_per_page = 2
    chooser_per_page = 1
    admin_url_namespace = "some_namespace"
    base_url_path = "deep/within/the/admin"
    chooser_admin_url_namespace = "my_chooser_namespace"
    chooser_base_url_path = "choose/wisely"
    # this will generate exact filters
    list_filter = ["job_title", "first_name"]
    # alternatively, this will generate filters with lookups
    list_filter = {
        "job_title": ["contains"],
        "first_name": ["contains"],
        "first_published_at": ["date"],
    }



register_snippet(Person, viewset=PersonViewSet)

Footnotes

  1. Development Testing

@squash-labs
Copy link

squash-labs bot commented Mar 22, 2023

Manage this branch in Squash

Test this branch here: https://laymonagecustom-snippets-list-ss8iq.squash.io

@laymonage laymonage self-assigned this Mar 23, 2023
@laymonage laymonage force-pushed the custom-snippets-list-filter branch 3 times, most recently from 833f027 to ee477dc Compare March 23, 2023 14:22
@laymonage laymonage requested review from gasman and removed request for gasman March 23, 2023 14:22
@laymonage
Copy link
Member Author

Hold on, I haven't added docs in the topics/snippets.md file.

@laymonage
Copy link
Member Author

Ok, docs added.

get_fields() only returns the model fields, but filters can also be
model fields + lookups, e.g. `text__contains`
@laymonage laymonage changed the title Automatically generate filterset_class for snippets and generic IndexView through list_filter Automatically generate filterset_class for snippets and generic IndexView through list_filter# Mar 31, 2023
@laymonage laymonage changed the title Automatically generate filterset_class for snippets and generic IndexView through list_filter# Automatically generate filterset_class for snippets and generic IndexView through list_filter Mar 31, 2023
Copy link
Collaborator

@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.

All looks good to me!

@@ -0,0 +1,32 @@
# Generated by Django 4.2b1 on 2023-03-21 17:12
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this file needs renumbering - will do that while I merge.

gasman added a commit that referenced this pull request Apr 3, 2023
@gasman
Copy link
Collaborator

gasman commented Apr 3, 2023

Merged in a3498b2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants