From 8b9c390a74aa7e9c0cf87b3aa4d900698fee1cb5 Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Thu, 22 Feb 2018 23:33:12 +0100 Subject: [PATCH] Update SelectWidgetBootstrap to template rendering. --- src/wiki/forms.py | 61 +++---------------- src/wiki/templates/wiki/forms/select.html | 15 +++++ .../templates/wiki/forms/select_option.html | 1 + 3 files changed, 26 insertions(+), 51 deletions(-) create mode 100644 src/wiki/templates/wiki/forms/select.html create mode 100644 src/wiki/templates/wiki/forms/select_option.html diff --git a/src/wiki/forms.py b/src/wiki/forms.py index ee320437..b818a907 100644 --- a/src/wiki/forms.py +++ b/src/wiki/forms.py @@ -1,7 +1,6 @@ import random import string from datetime import timedelta -from itertools import chain from django import forms from django.apps import apps @@ -9,17 +8,13 @@ from django.contrib.auth import get_user_model from django.contrib.auth.forms import UserCreationForm from django.core import validators from django.core.validators import RegexValidator -from django.forms.utils import flatatt from django.forms.widgets import HiddenInput from django.shortcuts import get_object_or_404 from django.urls import Resolver404, resolve from django.utils import timezone -from django.utils.encoding import force_text -from django.utils.html import conditional_escape, escape from django.utils.safestring import mark_safe from django.utils.translation import gettext, gettext_lazy as _, pgettext_lazy from wiki import models -from wiki.compat import BuildAttrsCompat from wiki.conf import settings from wiki.core import permissions from wiki.core.diff import simple_merge @@ -285,12 +280,15 @@ class EditForm(forms.Form, SpamProtectionMixin): return cd -class SelectWidgetBootstrap(BuildAttrsCompat, forms.Select): +class SelectWidgetBootstrap(forms.Select): """ http://twitter.github.com/bootstrap/components.html#buttonDropdowns Needs bootstrap and jquery """ + template_name = "wiki/forms/select.html" + option_template_name = "wiki/forms/select_option.html" + def __init__(self, attrs={}, choices=(), disabled=False): attrs['class'] = 'btn-group pull-left btn-group-form' self.disabled = disabled @@ -302,51 +300,12 @@ class SelectWidgetBootstrap(BuildAttrsCompat, forms.Select): if k not in ('attrs', 'disabled'): self.noscript_widget.__setattr__(k, value) - def render(self, name, value, attrs=None, choices=()): - if value is None: - value = '' - final_attrs = self.build_attrs_compat(attrs, name=name) - output = [ - """""" - """ """ - """ """ - """ """ - """ """ - """""" - """""" % - {'attrs': flatatt(final_attrs), - 'options': self.render_options(choices, [value]), - 'label': _('Select an option'), - 'name': name, 'disabled': ' disabled' if self.disabled else '', - 'noscript': self.noscript_widget.render(name, value, {})}] - return mark_safe('\n'.join(output)) - - def render_option(self, selected_choices, option_value, option_label): - option_value = force_text(option_value) - selected_html = ( - option_value in selected_choices) and ' selected="selected"' or '' - return '
  • %s
  • ' % ( - escape(option_value), selected_html, - conditional_escape(force_text(option_label))) - - def render_options(self, choices, selected_choices): - # Normalize to strings. - selected_choices = set([force_text(v) for v in selected_choices]) - output = [] - for option_value, option_label in chain(self.choices, choices): - if isinstance(option_label, (list, tuple)): - output.append( - '
  • ' % - escape(force_text(option_value))) - for option in option_label: - output.append(self.render_option(selected_choices, *option)) - else: - output.append(self.render_option(selected_choices, option_value, option_label)) - return '\n'.join(output) + 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): diff --git a/src/wiki/templates/wiki/forms/select.html b/src/wiki/templates/wiki/forms/select.html new file mode 100644 index 00000000..95bb06ed --- /dev/null +++ b/src/wiki/templates/wiki/forms/select.html @@ -0,0 +1,15 @@ + + + + + + + diff --git a/src/wiki/templates/wiki/forms/select_option.html b/src/wiki/templates/wiki/forms/select_option.html new file mode 100644 index 00000000..30ff9787 --- /dev/null +++ b/src/wiki/templates/wiki/forms/select_option.html @@ -0,0 +1 @@ +
  • {{ widget.label }}
  • -- 2.45.2