Skip to content

Move template partial logic to mixin #103

Description

@joshuadavidthomas

In the most recent release (v0.15.0) I had to introduce a breaking change to crud.CRUDView related to template partials. I originally copied the code dealing with that from a project that used it and didn't think through all of the downstream effects it would have.

I think template partials should be an opt-in thing per-project, and right now that's accomplished via a class attribute enable_template_partials flag on CRUDView. I wonder if moving all the template partial logic to a specific mixin for that would be a good workaround for allowing project's to opt-in, by just adding it to the view class inheritence? This way, CRUDView could be left alone and continue to function without breaking projects using it and you could opt in by choice.

It could also allow a space for expanding the functionality of the template partial rendering, as right now it's just focused on the list view.

class TemplatePartialMixin:
	list_partial: ClassVar[str] = "object-list"

    def get_template_names(self):
        template_names = super().get_template_names()

        if (
            self.role == Role.LIST
            and getattr(self.request, "htmx", False)
            and self.request.htmx
            and not self.kwargs.get(self.page_kwarg, None)  # pyright: ignore[reportAny]
            and not self.request.GET.get(self.page_kwarg, None)
        ):
            template_names = [
                f"{template_name}#{self.list_partial}"
                for template_name in template_names
            ]

        return template_names

Metadata

Metadata

Assignees

No one assigned

    Labels

    🏋️ improvementEnhancements or optimizations to existing functionality🚨 breakingBreaking changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions