Return MSFList, not plain list, from form field (#118)

This commit is contained in:
Alieh Rymašeŭski 2021-02-14 14:47:47 +00:00 committed by GitHub
parent 9c43f195ba
commit 71f9ae84e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 17 deletions

View File

@ -23,7 +23,7 @@ from django.utils.text import capfirst
from django.core import exceptions
from ..forms.fields import MultiSelectFormField, MinChoicesValidator, MaxChoicesValidator
from ..utils import get_max_length
from ..utils import MSFList, get_max_length
from ..validators import MaxValueMultiFieldValidator
if sys.version_info < (3,):
@ -46,21 +46,6 @@ def add_metaclass(metaclass):
return wrapper
class MSFList(list):
def __init__(self, choices, *args, **kwargs):
self.choices = choices
super(MSFList, self).__init__(*args, **kwargs)
def __str__(msgl):
msg_list = [msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i) for i in msgl]
return u', '.join([string_type(s) for s in msg_list])
if sys.version_info < (3,):
def __unicode__(self, msgl):
return self.__str__(msgl)
class MultiSelectField(models.CharField):
""" Choice values can not contain commas. """
@ -130,6 +115,7 @@ class MultiSelectField(models.CharField):
'label': capfirst(self.verbose_name),
'help_text': self.help_text,
'choices': self.choices,
'flat_choices': self.flatchoices,
'max_length': self.max_length,
'max_choices': self.max_choices}
if self.has_default():

View File

@ -16,7 +16,7 @@
from django import forms
from ..utils import get_max_length
from ..utils import MSFList, get_max_length
from ..validators import MaxValueMultiFieldValidator, MinChoicesValidator, MaxChoicesValidator
@ -27,6 +27,7 @@ class MultiSelectFormField(forms.MultipleChoiceField):
self.min_choices = kwargs.pop('min_choices', None)
self.max_choices = kwargs.pop('max_choices', None)
self.max_length = kwargs.pop('max_length', None)
self.flat_choices = kwargs.pop('flat_choices')
super(MultiSelectFormField, self).__init__(*args, **kwargs)
self.max_length = get_max_length(self.choices, self.max_length)
self.validators.append(MaxValueMultiFieldValidator(self.max_length))
@ -34,3 +35,6 @@ class MultiSelectFormField(forms.MultipleChoiceField):
self.validators.append(MaxChoicesValidator(self.max_choices))
if self.min_choices is not None:
self.validators.append(MinChoicesValidator(self.min_choices))
def to_python(self, value):
return MSFList(dict(self.flat_choices), super(MultiSelectFormField, self).to_python(value))

View File

@ -25,6 +25,21 @@ else:
string_type = string
class MSFList(list):
def __init__(self, choices, *args, **kwargs):
self.choices = choices
super(MSFList, self).__init__(*args, **kwargs)
def __str__(msgl):
msg_list = [msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i) for i in msgl]
return u', '.join([string_type(s) for s in msg_list])
if sys.version_info < (3,):
def __unicode__(self, msgl):
return self.__str__(msgl)
def get_max_length(choices, max_length, default=200):
if max_length is None:
if choices: