django-multiselectfield/src/multiselectfield/db/fields.py

110 lines
3.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright (c) 2012-2013 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
2012-09-27 14:02:41 +00:00
from django.db import models
from django.utils.text import capfirst
from django.core import exceptions
2012-09-27 14:12:55 +00:00
from ..forms.fields import MultiSelectFormField
2012-09-27 14:02:41 +00:00
if sys.version_info[0] == 2:
string = basestring
string_type = unicode
else:
string = str
string_type = string
# 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 get_choices_default(self):
return self.get_choices(include_blank=False)
def get_choices_selected(self, arr_choices=''):
if not arr_choices:
return False
list = []
for choice_selected in arr_choices:
list.append(string_type(choice_selected[0]))
2012-09-27 14:02:41 +00:00
return list
def _get_FIELD_display(self, field):
value = getattr(self, field.attname)
choicedict = dict(field.choices)
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):
raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
return
def formfield(self, **kwargs):
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name),
'help_text': self.help_text, 'choices': self.choices}
if self.has_default():
defaults['initial'] = self.get_default()
defaults.update(kwargs)
return MultiSelectFormField(**defaults)
def get_prep_value(self, value):
if isinstance(value, string):
2012-09-27 14:02:41 +00:00
return value
elif isinstance(value, list):
return ",".join(value)
def to_python(self, value):
if value is not None:
return value if isinstance(value, list) else value.split(',')
return ''
def contribute_to_class(self, cls, name):
super(MultiSelectField, self).contribute_to_class(cls, name)
if self.choices:
func = lambda self, fieldname = name, choicedict = dict(self.choices): ",".join([string_type(choicedict.get(value, value)) for value in getattr(self, fieldname)])
2012-09-27 14:02:41 +00:00
setattr(cls, 'get_%s_display' % self.name, func)
2012-09-27 14:12:55 +00:00
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