django-multiselectfield/multiselectfield/db/fields.py

147 lines
5.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-10-13 13:57:44 +00:00
# Copyright (c) 2012 by Pablo Martín <goinnn@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this programe. If not, see <http://www.gnu.org/licenses/>.
import sys
2013-11-26 19:45:46 +00:00
import django
2012-09-27 14:02:41 +00:00
from django.db import models
from django.utils.text import capfirst
from django.core import exceptions
from ..forms.fields import MultiSelectFormField, MaxChoicesValidator
from ..utils import get_max_length
from ..validators import MaxValueMultiFieldValidator
2012-09-27 14:02:41 +00:00
if sys.version_info[0] == 2:
string_type = unicode
else:
2013-11-30 18:31:14 +00:00
string_type = str
# Code from six egg https://bitbucket.org/gutworth/six/src/a3641cb211cc360848f1e2dd92e9ae6cd1de55dd/six.py?at=default
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
for slots_var in orig_vars.get('__slots__', ()):
orig_vars.pop(slots_var)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
2012-09-27 14:02:41 +00:00
class MultiSelectField(models.CharField):
2012-09-27 14:02:41 +00:00
""" Choice values can not contain commas. """
def __init__(self, *args, **kwargs):
self.max_choices = kwargs.pop('max_choices', None)
super(MultiSelectField, self).__init__(*args, **kwargs)
self.max_length = get_max_length(self.choices, self.max_length)
self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
if self.max_choices is not None:
self.validators.append(MaxChoicesValidator(self.max_choices))
@property
def flatchoices(self):
return None
2012-09-27 14:02:41 +00:00
def get_choices_default(self):
return self.get_choices(include_blank=False)
2013-11-30 18:57:20 +00:00
def get_choices_selected(self, arr_choices):
choices_selected = []
2012-09-27 14:02:41 +00:00
for choice_selected in arr_choices:
2013-11-30 18:57:20 +00:00
choices_selected.append(string_type(choice_selected[0]))
return choices_selected
2012-09-27 14:02:41 +00:00
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
2012-09-27 14:02:41 +00:00
def validate(self, value, model_instance):
arr_choices = self.get_choices_selected(self.get_choices_default())
for opt_select in value:
if (opt_select not in arr_choices):
2014-10-13 17:57:40 +00:00
if django.VERSION[0] == 1 and django.VERSION[1] >= 6:
2013-11-26 19:45:46 +00:00
raise exceptions.ValidationError(self.error_messages['invalid_choice'] % {"value": value})
else:
raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
2013-11-30 18:25:11 +00:00
def get_default(self):
default = super(MultiSelectField, self).get_default()
if isinstance(default, int):
default = string_type(default)
return default
2012-09-27 14:02:41 +00:00
def formfield(self, **kwargs):
defaults = {'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text,
'choices': self.choices,
'max_length': self.max_length,
'max_choices': self.max_choices}
2012-09-27 14:02:41 +00:00
if self.has_default():
defaults['initial'] = self.get_default()
defaults.update(kwargs)
return MultiSelectFormField(**defaults)
def get_prep_value(self, value):
return '' if value is None else ",".join(value)
2012-09-27 14:02:41 +00:00
def get_db_prep_value(self, value, connection, prepared=False):
if not prepared and not isinstance(value, string_type):
value = self.get_prep_value(value)
return value
2012-09-27 14:02:41 +00:00
def to_python(self, value):
if value:
2012-09-27 14:02:41 +00:00
return value if isinstance(value, list) else value.split(',')
def contribute_to_class(self, cls, name):
super(MultiSelectField, self).contribute_to_class(cls, name)
if self.choices:
def get_list(obj):
2013-11-26 19:04:41 +00:00
fieldname = name
choicedict = dict(self.choices)
display = []
if getattr(obj, fieldname):
for value in getattr(obj, fieldname):
item_display = choicedict.get(value, None)
if item_display is None:
try:
item_display = choicedict.get(int(value), value)
except (ValueError, TypeError):
item_display = value
display.append(string_type(item_display))
return display
def get_display(obj):
return ", ".join(get_list(obj))
setattr(cls, 'get_%s_list' % self.name, get_list)
2013-11-26 19:04:41 +00:00
setattr(cls, 'get_%s_display' % self.name, get_display)
MultiSelectField = add_metaclass(models.SubfieldBase)(MultiSelectField)
2012-09-27 14:12:55 +00:00
try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ['^multiselectfield\.db.fields\.MultiSelectField'])
except ImportError:
pass