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

Make register_templates plugin-friendly #703

Open
mjl opened this issue Apr 20, 2022 · 2 comments
Open

Make register_templates plugin-friendly #703

mjl opened this issue Apr 20, 2022 · 2 comments

Comments

@mjl
Copy link
Contributor

mjl commented Apr 20, 2022

Context: When creating a FeinCMS site using some kind of plugin mechanism, I ran into this problem.

Each plugin wants to contribute some Template to the main project. To do that, it will do something like

from feincms.module.page.models import Page

regions_main = (('main', 'Content'), )
Page.register_templates(
    {'key': 'mmmmh', 'title': 'Tasty template', 'path': 'munch/nomnom.html', 'regions': regions_main}
)

This will not work.

Each region holds a list of content types that can be used in that region. Unfortunately, this list is built when a concrete content type is created, so calling register_templates again later will add regions that have no content types at all.

I can paper over this by patching in all content types here

for region in Page._feincms_all_regions:
    if not region._content_types:
        region._content_types = Page._feincms_content_types.copy()  # copy is important otherwise CTs will be duplicated

but that is not always what is wanted, since I can't restrict CTs to a region that way.

If we ever want to restrict a CT to a certain region, things will become more complicated (the region set for each CT is not stored anywhere, so that information is lost once the CT has been instanciated).

Food for thought...

@matthiask
Copy link
Member

matthiask commented Apr 22, 2022

Moving the list of regions per CT to the modeladmin class has worked well for us in django-content-editor https://django-content-editor.readthedocs.io/en/latest/#restricting-plugins-to-a-subset-of-all-available-regions

I have done some work in the past trying to rebuild ItemEditor on top of ContentEditor in the past since the latter fixes a few ugly bugs (e.g. it's easy to crash the formset handling code when using ItemEditor's frontend) but haven't really gotten that far. It shouldn't be hard but I got confused while looking at the contentblock_init_handlers and contentblock_move_handlers. ContentEditor doesn't need them as much because it doesn't actually move blocks around in the DOM, it only reassigns the FlexBox order property.

@mjl
Copy link
Contributor Author

mjl commented May 4, 2022

Not totally the same thing, but related:

Actually, it would also be nice to restrict CTs to regions of a certain template (or template list). Currently, the only way is to creatively name your regions (ie. 'template1-main', 'template2-main'...), which doesn't play well with {% extends somebasetemplate.html %} that only knows how to render 'main'.

Perhaps overloading the regions parameter for create_content_type with a container class like that:

Page.register_templates({'key': 'magazine', 'title': 'whatevs', 'path': 'munch/nomnom.html', 'regions': ('main', )})

allowed_regions = RegionSelector(templates=['magazine', 'overview'], regions=['main']) + \
                  RegionSelector(templates='someotherpage', regions='*')

Page.create_content_type(MagazineArticle, regions=allowed_regions)

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

No branches or pull requests

2 participants