def get_form(self, request, obj=None, **kwargs): from cmsplugin_cascade.models import PluginExtraFields glossary_fields = list(kwargs.pop('glossary_fields', self.glossary_fields)) try: site = get_current_site(request) extra_fields = PluginExtraFields.objects.get(plugin_type=self.__class__.__name__, site=site) except ObjectDoesNotExist: pass else: # add a text input field to let the user name an ID tag for this HTML element if extra_fields.allow_id_tag: glossary_fields.append(PartialFormField('extra_element_id', widgets.TextInput(), label=_("Named Element ID"), )) # add a select box to let the user choose one or more CSS classes class_names = extra_fields.css_classes.get('class_names', '').replace(' ', '') if class_names: choices = [(clsname, clsname) for clsname in class_names.split(',')] if extra_fields.css_classes.get('multiple'): widget = widgets.SelectMultiple(choices=choices) else: widget = widgets.Select(choices=((None, _("Select CSS")),) + tuple(choices)) glossary_fields.append(PartialFormField('extra_css_classes', widget, label=_("Customized CSS Classes"), help_text=_("Customized CSS classes to be added to this element.") )) # add input fields to let the user enter styling information for style, choices_tuples in CASCADE_EXTRA_INLINE_STYLES.items(): inline_styles = extra_fields.inline_styles.get('extra_fields:{0}'.format(style)) if not inline_styles: continue Widget = choices_tuples[1] if issubclass(Widget, MultipleCascadingSizeWidget): key = 'extra_inline_styles:{0}'.format(style) allowed_units = extra_fields.inline_styles.get('extra_units:{0}'.format(style)).split(',') widget = Widget(inline_styles, allowed_units=allowed_units, required=False) glossary_fields.append(PartialFormField(key, widget, label=style)) else: for inline_style in inline_styles: key = 'extra_inline_styles:{0}'.format(inline_style) label = '{0}: {1}'.format(style, inline_style) glossary_fields.append(PartialFormField(key, Widget(), label=label)) kwargs.update(glossary_fields=glossary_fields) return super(ExtraFieldsMixin, self).get_form(request, obj, **kwargs)
def __init__(self, model, admin_site): super(PluginExtraFieldsAdmin, self).__init__(model, admin_site) self.style_fields = [] for style, choices_tuples in CASCADE_EXTRA_INLINE_STYLES.items(): extra_field = PartialFormField('extra_fields:{0}'.format(style), widgets.CheckboxSelectMultiple(choices=((c, c) for c in choices_tuples[0])), label=_("Customized {0} Fields:").format(style), ) Widget = choices_tuples[1] if issubclass(Widget, MultipleCascadingSizeWidget): self.style_fields.append(( extra_field, PartialFormField('extra_units:{0}'.format(style), widgets.Select(choices=self.DISTANCE_UNITS), label=_("Units for {0} Fields:").format(style), initial=self.DISTANCE_UNITS[0][0], ), )) else: self.style_fields.append(extra_field)