# Path: select_multiple_field/codecs.py
# def decode_csv_to_list(encoded):
#     """
#     Decodes a delimiter separated string to a Python list
#     """
#     delimiter = getattr(
#         settings, 'SELECTMULTIPLEFIELD_DELIMITER', DEFAULT_DELIMITER)
#     if encoded == '':
#         return []
# 
#     decoded = sorted(set(encoded.split(delimiter)))
#     return decoded
# 
# def encode_list_to_csv(decoded):
#     """
#     Encodes a Python list to a delimiter separated string
# 
#     Note: This sorts the list lexicographically
#     """
#     delimiter = getattr(
#         settings, 'SELECTMULTIPLEFIELD_DELIMITER', DEFAULT_DELIMITER)
#     decoded = sorted(set(decoded))
#     return delimiter.join(decoded)
# 
# Path: select_multiple_field/validators.py
# class MaxChoicesValidator(validators.BaseValidator):
# 
#     message = ungettext_lazy(
#         'Ensure this value has at most %(limit_value)d choice (it has %(show_value)d).',  # NOQA
#         'Ensure this value has at most %(limit_value)d choices (it has %(show_value)d).',  # NOQA
#         'limit_value')
#     code = 'max_choices'
# 
#     def compare(self, a, b):
#         return a > b
# 
#     def clean(self, x):
#         return len(x)
# 
# class MaxLengthValidator(validators.BaseValidator):
# 
#     message = ungettext_lazy(
#         'Ensure this value has at most %(limit_value)d character (it has %(show_value)d).',  # NOQA
#         'Ensure this value has at most %(limit_value)d characters (it has %(show_value)d).',  # NOQA
#         'limit_value')
#     code = 'max_length'
# 
#     def compare(self, a, b):
#         return a > b
# 
#     def clean(self, value):
#         return len(force_text(encode_list_to_csv(value)))


from django.core import exceptions, validators
from django.db import models
from django.utils import six
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from .codecs import decode_csv_to_list, encode_list_to_csv
from .validators import MaxChoicesValidator, MaxLengthValidator
            from south.modelsinspector import introspector
import select_multiple_field.forms as forms
        """
        if value is None:
            return value

        elif isinstance(value, (list, tuple)):
            self.validate_options_list(value)
            return value

        elif isinstance(value, six.string_types):
            #
            # Strings are always encoded choices
            #
            native = decode_csv_to_list(value)
            return native

        msg = self.error_messages['invalid_type'] % {'value': type(value)}
        raise exceptions.ValidationError(msg)

    def get_prep_value(self, value):
        """
        Perform preliminary non-db specific value checks and conversions.

        This takes a Python list and encodes it into a form storable in the
        database

        Returns a string or None
        """
        if value is None:
            return None


# Path: select_multiple_field/codecs.py
# def decode_csv_to_list(encoded):
#     """
#     Decodes a delimiter separated string to a Python list
#     """
#     delimiter = getattr(
#         settings, 'SELECTMULTIPLEFIELD_DELIMITER', DEFAULT_DELIMITER)
#     if encoded == '':
#         return []
# 
#     decoded = sorted(set(encoded.split(delimiter)))
#     return decoded
# 
# def encode_list_to_csv(decoded):
#     """
#     Encodes a Python list to a delimiter separated string
# 
#     Note: This sorts the list lexicographically
#     """
#     delimiter = getattr(
#         settings, 'SELECTMULTIPLEFIELD_DELIMITER', DEFAULT_DELIMITER)
#     decoded = sorted(set(decoded))
#     return delimiter.join(decoded)
# 
# Path: select_multiple_field/validators.py
# class MaxChoicesValidator(validators.BaseValidator):
# 
#     message = ungettext_lazy(
#         'Ensure this value has at most %(limit_value)d choice (it has %(show_value)d).',  # NOQA
#         'Ensure this value has at most %(limit_value)d choices (it has %(show_value)d).',  # NOQA
#         'limit_value')
#     code = 'max_choices'
# 
#     def compare(self, a, b):
#         return a > b
# 
#     def clean(self, x):
#         return len(x)
# 
# class MaxLengthValidator(validators.BaseValidator):
# 
#     message = ungettext_lazy(
#         'Ensure this value has at most %(limit_value)d character (it has %(show_value)d).',  # NOQA
#         'Ensure this value has at most %(limit_value)d characters (it has %(show_value)d).',  # NOQA
#         'limit_value')
#     code = 'max_length'
# 
#     def compare(self, a, b):
#         return a > b
# 
#     def clean(self, value):
#         return len(force_text(encode_list_to_csv(value)))


# Path: select_multiple_field/models.py
from django.core import exceptions, validators
from django.db import models
from django.utils import six
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from .codecs import decode_csv_to_list, encode_list_to_csv
from .validators import MaxChoicesValidator, MaxLengthValidator
            from south.modelsinspector import introspector
import select_multiple_field.forms as forms

        """
        if value is None:
            return value

        elif isinstance(value, (list, tuple)):
            self.validate_options_list(value)
            return value

        elif isinstance(value, six.string_types):
            #
            # Strings are always encoded choices
            #
            native = decode_csv_to_list(value)
            return native

        msg = self.error_messages['invalid_type'] % {'value': type(value)}
        raise exceptions.ValidationError(msg)

    def get_prep_value(self, value):
        """
        Perform preliminary non-db specific value checks and conversions.

        This takes a Python list and encodes it into a form storable in the
        database

        Returns a string or None
        """
        if value is None:
            return None


        return encode_list_to_csv(value)