Update rendering of the readonly multiselect widget to conform to Django's updated field class interface.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-04-06 16:17:51 -04:00
parent bbd7704e2e
commit 90623ed372
5 changed files with 5 additions and 61 deletions

View File

@@ -7,47 +7,6 @@ from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
class DetailSelectMultiple(forms.widgets.SelectMultiple):
def __init__(self, queryset=None, *args, **kwargs):
self.queryset = queryset
super(DetailSelectMultiple, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, choices=(), *args, **kwargs):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
css_class = final_attrs.get('class', 'list')
output = '<ul class="%s">' % css_class
options = None
if value:
if getattr(value, '__iter__', None):
options = [(index, string) for index, string in
self.choices if index in value]
else:
options = [(index, string) for index, string in
self.choices if index == value]
else:
if self.choices:
if self.choices[0] != ('', '---------') and value != []:
options = [(index, string) for index, string in
self.choices]
if options:
for index, string in options:
if self.queryset:
try:
output += '<li><a href="%s">%s</a></li>' % (
self.queryset.get(pk=index).get_absolute_url(),
string)
except AttributeError:
output += '<li>%s</li>' % (string)
else:
output += '<li>%s</li>' % string
else:
output += '<li>%s</li>' % _('None')
return mark_safe(output + '</ul>\n')
class DisableableSelectWidget(forms.SelectMultiple):
allow_multiple_selected = True