<s> from __future__ import unicode_literals <EOL> import six <EOL> from django . core . exceptions import ValidationError <EOL> from django . db import models <EOL> from django . forms import CheckboxSelectMultiple <EOL> from django . utils . text import capfirst <EOL> from dynamic_forms . conf import settings <EOL> from dynamic_forms . forms import MultiSelectFormField <EOL> class TextMultiSelectField ( six . with_metaclass ( models . SubfieldBase , <EOL> models . TextField ) ) : <EOL> widget = CheckboxSelectMultiple <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . separate_values_by = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> super ( TextMultiSelectField , self ) . __init__ ( * args , ** kwargs ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( TextMultiSelectField , self ) . contribute_to_class ( cls , name ) <EOL> if self . choices : <EOL> def _func ( self , fieldname = name , choicedict = dict ( self . choices ) ) : <EOL> return self . separate_values_by . join ( [ <EOL> choicedict . get ( value , value ) for value in <EOL> getattr ( self , fieldname ) <EOL> ] ) <EOL> setattr ( cls , '<STR_LIT>' % self . name , _func ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : self . choices , <EOL> '<STR_LIT>' : self . help_text , <EOL> '<STR_LIT:label>' : capfirst ( self . verbose_name ) , <EOL> '<STR_LIT>' : not self . blank , <EOL> '<STR_LIT>' : self . separate_values_by , <EOL> } <EOL> if self . has_default ( ) : <EOL> defaults [ '<STR_LIT>' ] = self . get_default ( ) <EOL> defaults . update ( kwargs ) <EOL> defaults [ '<STR_LIT>' ] = self . widget <EOL> return MultiSelectFormField ( ** defaults ) <EOL> def get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> return value <EOL> elif isinstance ( value , list ) : <EOL> return self . separate_values_by . join ( value ) <EOL> def get_choices_default ( self ) : <EOL> return self . get_choices ( include_blank = False ) <EOL> def get_choices_selected ( self , arr_choices = '<STR_LIT>' ) : <EOL> if not arr_choices : <EOL> return False <EOL> chces = [ ] <EOL> for choice_selected in arr_choices : <EOL> chces . append ( choice_selected [ <NUM_LIT:0> ] ) <EOL> return chces <EOL> def get_prep_value ( self , value ) : <EOL> return value <EOL> def to_python ( self , value ) : <EOL> if value is not None : <EOL> return ( value if isinstance ( value , list ) else <EOL> value . split ( self . separate_values_by ) ) <EOL> return [ ] <EOL> def validate ( self , value , model_instance ) : <EOL> """<STR_LIT>""" <EOL> arr_choices = self . get_choices_selected ( self . get_choices_default ( ) ) <EOL> for opt_select in value : <EOL> if opt_select not in arr_choices : <EOL> raise ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] % value ) <EOL> return <EOL> def value_to_string ( self , obj ) : <EOL> value = self . _get_val_from_obj ( obj ) <EOL> return self . get_db_prep_value ( value ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> if '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( patterns = [ '<STR_LIT>' ] , <EOL> rules = [ ( ( TextMultiSelectField , ) , [ ] , { } ) ] ) </s>
