~netlandish/django-wiki

f5e08d55f71af8849dc331b3048b6d7e3bdfdba5 — Benjamin Bach 4 years ago 82cdc57
Restore SelectWidgetBootstrap but simplify it to almost nothing
3 files changed, 22 insertions(+), 44 deletions(-)

M src/wiki/forms.py
D src/wiki/templates/wiki/forms/select.html
D src/wiki/templates/wiki/forms/select_option.html
M src/wiki/forms.py => src/wiki/forms.py +22 -28
@@ 149,7 149,7 @@ class SpamProtectionMixin:
                    gettext(
                        "Spam protection: You are only allowed to create or edit %(revisions)d article(s) per %(interval_name)s."
                    )
                    % {"revisions": max_count, "interval_name": interval_name,}
                    % {"revisions": max_count, "interval_name": interval_name}
                )

        if not settings.LOG_IPS_ANONYMOUS:


@@ 323,35 323,22 @@ class EditForm(forms.Form, SpamProtectionMixin):

class SelectWidgetBootstrap(forms.Select):
    """
    http://twitter.github.com/bootstrap/components.html#buttonDropdowns
    Needs bootstrap and jquery
    Formerly, we used Bootstrap 3's dropdowns. They look nice. But to
    reduce bugs and reliance on JavaScript, it's now been replaced by
    a conventional system platform drop-down.

    https://getbootstrap.com/docs/4.4/components/dropdowns/
    """

    template_name = "wiki/forms/select.html"
    option_template_name = "wiki/forms/select_option.html"
    def __init__(self, attrs=None, choices=()):
        if attrs is None:
            attrs = {"class": ""}
        elif "class" not in attrs:
            attrs["class"] = ""
        attrs["class"] += " form-control"

    def __init__(self, attrs={}, choices=(), disabled=False):
        attrs["class"] = "btn-group float-left btn-group-form"
        self.disabled = disabled
        self.noscript_widget = forms.Select(attrs={}, choices=choices)
        super().__init__(attrs, choices)

    def __setattr__(self, k, value):
        super().__setattr__(k, value)
        if k not in ("attrs", "disabled"):
            self.noscript_widget.__setattr__(k, value)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        context["label"] = _("Select an option")
        context["noscript"] = self.noscript_widget.render(name, value, {})
        context["disabled"] = " disabled" if self.disabled else ""
        return context

    class Media(forms.Media):

        js = ("wiki/js/forms.js",)


class TextInputPrepend(forms.TextInput):
    template_name = "wiki/forms/text.html"


@@ 447,9 434,14 @@ class PermissionsForm(PluginSettingsFormMixin, forms.ModelForm):
        help_text=_("Enter the username of the owner."),
    )
    group = forms.ModelChoiceField(
        Group.objects.all(), empty_label=_("(none)"), label=_("Group"), required=False,
        widget=forms.Select(attrs={"class": "form-control"})
        Group.objects.all(),
        empty_label=_("(none)"),
        label=_("Group"),
        required=False,
        widget=forms.Select(attrs={"class": "form-control"}),
    )
    if settings.USE_BOOTSTRAP_SELECT_WIDGET:
        group.widget = SelectWidgetBootstrap()

    recursive = forms.BooleanField(
        label=_("Inherit permissions"),


@@ 510,7 502,9 @@ class PermissionsForm(PluginSettingsFormMixin, forms.ModelForm):
                else Group.objects.none(),
                empty_label=_("(none)"),
                required=False,
                widget=forms.Select(attrs={"disabled": True}),
                widget=SelectWidgetBootstrap(disabled=True)
                if settings.USE_BOOTSTRAP_SELECT_WIDGET
                else forms.Select(attrs={"disabled": True}),
            )
            self.fields["group_read"].widget = forms.HiddenInput()
            self.fields["group_write"].widget = forms.HiddenInput()

D src/wiki/templates/wiki/forms/select.html => src/wiki/templates/wiki/forms/select.html +0 -15
@@ 1,15 0,0 @@
<div{% include "django/forms/widgets/attrs.html" %}>
  <button class="btn btn-group-label{{ disabled }}" type="button">{{ label }}</button>
  <button class="btn btn-secondary dropdown-toggle{{ disabled }}" type="button" data-toggle="dropdown">
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">{% for group_name, group_choices, group_index in widget.optgroups %}
      {% if group_name %}<li class="divider" label="{{ group_name }}"></li>{% endif %}
      {% for option in group_choices %}
      {% include option.template_name with widget=option %}
      {% endfor %}
      {% endfor %}
    </ul>
    <input type="hidden" name="{{ widget.name }}" value="" class="btn-group-value" />
</div>
<noscript>{{ noscript }}</noscript>

D src/wiki/templates/wiki/forms/select_option.html => src/wiki/templates/wiki/forms/select_option.html +0 -1
@@ 1,1 0,0 @@
<li><a href="javascript:void(0)" data-value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ widget.label }}</a></li>