Skip to content

Commit

Permalink
Return MSFList, not plain list, from form field (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh-rymasheuski authored Feb 14, 2021
1 parent 9c43f19 commit 71f9ae8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
18 changes: 2 additions & 16 deletions multiselectfield/db/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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,):
Expand All @@ -46,21 +46,6 @@ def wrapper(cls):
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. """

Expand Down Expand Up @@ -130,6 +115,7 @@ def formfield(self, **kwargs):
'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():
Expand Down
6 changes: 5 additions & 1 deletion multiselectfield/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -27,10 +27,14 @@ def __init__(self, *args, **kwargs):
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))
if self.max_choices is not None:
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))
15 changes: 15 additions & 0 deletions multiselectfield/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
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:
Expand Down

0 comments on commit 71f9ae8

Please sign in to comment.