fsbraun / djangocms-rest

BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

As a developer I would like to have a HeadlessCmsPluginBase so that I can provide a simple plugin visualization for editors without having to write a plugin template for each of my custom plugins. #7

Open macolo opened 5 days ago

macolo commented 5 days ago

Solution Example

plugins/your_custom_plugin/cms_plugins.py (just an example on how to use HeadlessCMSPluginBase)

class CustomHeadingPlugin(HeadlessCMSPluginBase):
    model = models.CustomHeadingPluginModel
    module = 'Layout Helpers'
    name = "My Custom Heading"
    allow_children = False

headless_plugin_base.py

from cms.plugin_base import CMSPluginBase
from django.forms import ModelForm

from . import models

class HeadlessCMSPluginBase(CMSPluginBase):
    render_template = 'headless-plugin-default.html'

    # ModelForm
    def get_form_instance(self, instance):
        class HeadlessGenericForm(ModelForm):
            class Meta:
                model = self.model
                fields='__all__'

        form = HeadlessGenericForm(instance=instance)
        return form

    def render(self, context, instance, placeholder):
        form = self.get_form_instance(instance)
        context = super().render(context, instance, placeholder)
        context.update({
            'form': form,
            'name': self.name
        })
        return context

templates/headless-plugin-default.html

<div class="{{ instance.plugin_type|lower }}">
  <h3>{{ name}} (#{{ instance }})</h3>

  <ul>
  {% for field in form %}
    <li>
        {{ field.label }}: {{ field.value }}
    </li>
  {% endfor %}
  </ul>
</div>
fsbraun commented 5 days ago

Nice! Thank you! I'd love to even just show the admin form (which might be formatted using fieldsets on the plugin class).

fsbraun commented 3 days ago

@macolo Thinking a bit about this proposal, I would potentially go one step further:

This way, we can have the following: