<s> import copy <EOL> from datetime import datetime <EOL> from django . forms import ( <EOL> CharField , FileInput , MultipleChoiceField , MultiValueField , MultiWidget , <EOL> RadioSelect , SelectMultiple , SplitDateTimeField , SplitDateTimeWidget , <EOL> TextInput , <EOL> ) <EOL> from . base import WidgetTest <EOL> class MyMultiWidget ( MultiWidget ) : <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> return value . split ( '<STR_LIT>' ) <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class ComplexMultiWidget ( MultiWidget ) : <EOL> def __init__ ( self , attrs = None ) : <EOL> widgets = ( <EOL> TextInput ( ) , <EOL> SelectMultiple ( choices = WidgetTest . beatles ) , <EOL> SplitDateTimeWidget ( ) , <EOL> ) <EOL> super ( ComplexMultiWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> data = value . split ( '<STR_LIT:U+002C>' ) <EOL> return [ <EOL> data [ <NUM_LIT:0> ] , list ( data [ <NUM_LIT:1> ] ) , datetime . strptime ( data [ <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> ] <EOL> return [ None , None , None ] <EOL> def format_output ( self , rendered_widgets ) : <EOL> return '<STR_LIT:\n>' . join ( rendered_widgets ) <EOL> class ComplexField ( MultiValueField ) : <EOL> def __init__ ( self , required = True , widget = None , label = None , initial = None ) : <EOL> fields = ( <EOL> CharField ( ) , <EOL> MultipleChoiceField ( choices = WidgetTest . beatles ) , <EOL> SplitDateTimeField ( ) , <EOL> ) <EOL> super ( ComplexField , self ) . __init__ ( <EOL> fields , required , widget , label , initial , <EOL> ) <EOL> def compress ( self , data_list ) : <EOL> if data_list : <EOL> return '<STR_LIT>' % ( <EOL> data_list [ <NUM_LIT:0> ] , '<STR_LIT>' . join ( data_list [ <NUM_LIT:1> ] ) , data_list [ <NUM_LIT:2> ] , <EOL> ) <EOL> return None <EOL> class DeepCopyWidget ( MultiWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , choices = [ ] ) : <EOL> widgets = [ <EOL> RadioSelect ( choices = choices ) , <EOL> TextInput , <EOL> ] <EOL> super ( DeepCopyWidget , self ) . __init__ ( widgets ) <EOL> def _set_choices ( self , choices ) : <EOL> """<STR_LIT>""" <EOL> self . widgets [ <NUM_LIT:0> ] . choices = choices <EOL> def _get_choices ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . widgets [ <NUM_LIT:0> ] . choices <EOL> choices = property ( _get_choices , _set_choices ) <EOL> class MultiWidgetTest ( WidgetTest ) : <EOL> def test_text_inputs ( self ) : <EOL> widget = MyMultiWidget ( <EOL> widgets = ( <EOL> TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , <EOL> TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , <EOL> ) <EOL> ) <EOL> self . check_html ( widget , '<STR_LIT:name>' , [ '<STR_LIT>' , '<STR_LIT>' ] , html = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> self . check_html ( widget , '<STR_LIT:name>' , '<STR_LIT>' , html = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> self . check_html ( widget , '<STR_LIT:name>' , '<STR_LIT>' , attrs = { '<STR_LIT:id>' : '<STR_LIT:foo>' } , html = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> def test_constructor_attrs ( self ) : <EOL> widget = MyMultiWidget ( <EOL> widgets = ( <EOL> TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , <EOL> TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , <EOL> ) , <EOL> attrs = { '<STR_LIT:id>' : '<STR_LIT:bar>' } , <EOL> ) <EOL> self . check_html ( widget , '<STR_LIT:name>' , [ '<STR_LIT>' , '<STR_LIT>' ] , html = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> def test_needs_multipart_true ( self ) : <EOL> """<STR_LIT>""" <EOL> widget = MyMultiWidget ( widgets = ( TextInput ( ) , FileInput ( ) ) ) <EOL> self . assertTrue ( widget . needs_multipart_form ) <EOL> def test_needs_multipart_false ( self ) : <EOL> """<STR_LIT>""" <EOL> widget = MyMultiWidget ( widgets = ( TextInput ( ) , TextInput ( ) ) ) <EOL> self . assertFalse ( widget . needs_multipart_form ) <EOL> def test_nested_multiwidget ( self ) : <EOL> """<STR_LIT>""" <EOL> widget = ComplexMultiWidget ( ) <EOL> self . check_html ( widget , '<STR_LIT:name>' , '<STR_LIT>' , html = ( <EOL> """<STR_LIT>""" <EOL> ) ) <EOL> def test_deepcopy ( self ) : <EOL> """<STR_LIT>""" <EOL> w1 = DeepCopyWidget ( choices = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> w2 = copy . deepcopy ( w1 ) <EOL> w2 . choices = [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] <EOL> self . assertEqual ( w1 . choices , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) </s>
