From 1878345992d0272c146cbeec2e62f3ed24907f1e Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 2 Nov 2021 18:19:03 +0100 Subject: JSON types: multi valued choices --- ishtar_common/widgets.py | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'ishtar_common/widgets.py') diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index b80faee6d..efea07065 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -126,16 +126,23 @@ class Select2Media(object): return media -class Select2Dynamic(Select2Media, forms.Select): +class Select2DynamicBase(Select2Media): """ Select input using select, allowing dynamic creation. """ + MULTIPLE = False def render(self, name, value, attrs=None, choices=()): choices = choices or getattr(self, "choices", []) - if value and value not in [key for key, v in choices]: - choices.insert(1, (value, value)) - self.choices = choices + if value: + values = [value] + if self.MULTIPLE: + value = value[0] + values = value + for va in values: + if va not in [key for key, va in choices]: + choices.insert(1, (va, va)) + self.choices = choices klass = attrs and attrs.get("class") or "" klass += " " if klass else "" + "js-select2" if not attrs: @@ -161,7 +168,9 @@ class Select2Dynamic(Select2Media, forms.Select): if attrs.get("full-width", None): options.append("containerCssClass: 'full-width'") - html = super(Select2Dynamic, self).render(name, value, attrs) + if self.MULTIPLE: + options.append("multiple: 'true'") + html = super(Select2DynamicBase, self).render(name, value, attrs) html += """