39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
# This file is part of dnmgmt, a number resource management system
|
|
# Licensed under GNU General Public License v3 or later
|
|
# Written by Sebastian Lohff (seba@someserver.de)
|
|
|
|
from django import forms
|
|
|
|
|
|
class MultiTextInput(forms.widgets.Input):
|
|
input_type = "text"
|
|
|
|
def render(self, name, value, attrs=None):
|
|
if value is not None:
|
|
selectedOptions = []
|
|
for val in value:
|
|
for k, v in self.choices:
|
|
if val == k:
|
|
selectedOptions.append(v)
|
|
|
|
value = " ".join(selectedOptions)
|
|
|
|
return super(MultiTextInput, self).render(name, value, attrs)
|
|
|
|
def value_from_datadict(self, data, files, name):
|
|
values = list(filter(bool, map(lambda _x: _x.strip(), data.get(name).split(" "))))
|
|
|
|
result = []
|
|
for value in values:
|
|
# FIXME: using value here throws a weird error message at some point
|
|
# could be handled by overriding the messages in ChoiceField
|
|
# or... well, don't know
|
|
kId = value
|
|
for k, v in self.choices:
|
|
if v.lower() == value.lower():
|
|
kId = str(k)
|
|
break
|
|
result.append(kId)
|
|
|
|
return result
|