<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> import copy <EOL> from itertools import chain <EOL> import warnings <EOL> from django . conf import settings <EOL> from django . forms . util import flatatt , to_current_timezone <EOL> from django . utils . datastructures import MultiValueDict , MergeDict <EOL> from django . utils . html import conditional_escape , format_html <EOL> from django . utils . translation import ugettext_lazy <EOL> from django . utils . encoding import force_text , python_2_unicode_compatible <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils import datetime_safe , formats , six <EOL> from django . utils . six . moves . urllib . parse import urljoin <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> MEDIA_TYPES = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ python_2_unicode_compatible <EOL> class Media ( object ) : <EOL> def __init__ ( self , media = None , ** kwargs ) : <EOL> if media : <EOL> media_attrs = media . __dict__ <EOL> else : <EOL> media_attrs = kwargs <EOL> self . _css = { } <EOL> self . _js = [ ] <EOL> for name in MEDIA_TYPES : <EOL> getattr ( self , '<STR_LIT>' + name ) ( media_attrs . get ( name , None ) ) <EOL> def __str__ ( self ) : <EOL> return self . render ( ) <EOL> def render ( self ) : <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( chain ( * [ getattr ( self , '<STR_LIT>' + name ) ( ) for name in MEDIA_TYPES ] ) ) ) <EOL> def render_js ( self ) : <EOL> return [ format_html ( '<STR_LIT>' , self . absolute_path ( path ) ) for path in self . _js ] <EOL> def render_css ( self ) : <EOL> media = sorted ( self . _css . keys ( ) ) <EOL> return chain ( * [ <EOL> [ format_html ( '<STR_LIT>' , self . absolute_path ( path ) , medium ) <EOL> for path in self . _css [ medium ] ] <EOL> for medium in media ] ) <EOL> def absolute_path ( self , path , prefix = None ) : <EOL> if path . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:/>' ) ) : <EOL> return path <EOL> if prefix is None : <EOL> if settings . STATIC_URL is None : <EOL> prefix = settings . MEDIA_URL <EOL> else : <EOL> prefix = settings . STATIC_URL <EOL> return urljoin ( prefix , path ) <EOL> def __getitem__ ( self , name ) : <EOL> "<STR_LIT>" <EOL> if name in MEDIA_TYPES : <EOL> return Media ( ** { str ( name ) : getattr ( self , '<STR_LIT:_>' + name ) } ) <EOL> raise KeyError ( '<STR_LIT>' % name ) <EOL> def add_js ( self , data ) : <EOL> if data : <EOL> for path in data : <EOL> if path not in self . _js : <EOL> self . _js . append ( path ) <EOL> def add_css ( self , data ) : <EOL> if data : <EOL> for medium , paths in data . items ( ) : <EOL> for path in paths : <EOL> if not self . _css . get ( medium ) or path not in self . _css [ medium ] : <EOL> self . _css . setdefault ( medium , [ ] ) . append ( path ) <EOL> def __add__ ( self , other ) : <EOL> combined = Media ( ) <EOL> for name in MEDIA_TYPES : <EOL> getattr ( combined , '<STR_LIT>' + name ) ( getattr ( self , '<STR_LIT:_>' + name , None ) ) <EOL> getattr ( combined , '<STR_LIT>' + name ) ( getattr ( other , '<STR_LIT:_>' + name , None ) ) <EOL> return combined <EOL> def media_property ( cls ) : <EOL> def _media ( self ) : <EOL> sup_cls = super ( cls , self ) <EOL> try : <EOL> base = sup_cls . media <EOL> except AttributeError : <EOL> base = Media ( ) <EOL> definition = getattr ( cls , '<STR_LIT>' , None ) <EOL> if definition : <EOL> extend = getattr ( definition , '<STR_LIT>' , True ) <EOL> if extend : <EOL> if extend == True : <EOL> m = base <EOL> else : <EOL> m = Media ( ) <EOL> for medium in extend : <EOL> m = m + base [ medium ] <EOL> return m + Media ( definition ) <EOL> else : <EOL> return Media ( definition ) <EOL> else : <EOL> return base <EOL> return property ( _media ) <EOL> class MediaDefiningClass ( type ) : <EOL> "<STR_LIT>" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> new_class = super ( MediaDefiningClass , cls ) . __new__ ( cls , name , bases , <EOL> attrs ) <EOL> if '<STR_LIT>' not in attrs : <EOL> new_class . media = media_property ( new_class ) <EOL> return new_class <EOL> @ python_2_unicode_compatible <EOL> class SubWidget ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent_widget , name , value , attrs , choices ) : <EOL> self . parent_widget = parent_widget <EOL> self . name , self . value = name , value <EOL> self . attrs , self . choices = attrs , choices <EOL> def __str__ ( self ) : <EOL> args = [ self . name , self . value , self . attrs ] <EOL> if self . choices : <EOL> args . append ( self . choices ) <EOL> return self . parent_widget . render ( * args ) <EOL> class Widget ( six . with_metaclass ( MediaDefiningClass ) ) : <EOL> is_hidden = False <EOL> needs_multipart_form = False <EOL> is_localized = False <EOL> is_required = False <EOL> def __init__ ( self , attrs = None ) : <EOL> if attrs is not None : <EOL> self . attrs = attrs . copy ( ) <EOL> else : <EOL> self . attrs = { } <EOL> def __deepcopy__ ( self , memo ) : <EOL> obj = copy . copy ( self ) <EOL> obj . attrs = self . attrs . copy ( ) <EOL> memo [ id ( self ) ] = obj <EOL> return obj <EOL> def subwidgets ( self , name , value , attrs = None , choices = ( ) ) : <EOL> """<STR_LIT>""" <EOL> yield SubWidget ( self , name , value , attrs , choices ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def build_attrs ( self , extra_attrs = None , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> attrs = dict ( self . attrs , ** kwargs ) <EOL> if extra_attrs : <EOL> attrs . update ( extra_attrs ) <EOL> return attrs <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> """<STR_LIT>""" <EOL> return data . get ( name , None ) <EOL> def id_for_label ( self , id_ ) : <EOL> """<STR_LIT>""" <EOL> return id_ <EOL> class Input ( Widget ) : <EOL> """<STR_LIT>""" <EOL> input_type = None <EOL> def _format_value ( self , value ) : <EOL> if self . is_localized : <EOL> return formats . localize_input ( value ) <EOL> return value <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs , type = self . input_type , name = name ) <EOL> if value != '<STR_LIT>' : <EOL> final_attrs [ '<STR_LIT:value>' ] = force_text ( self . _format_value ( value ) ) <EOL> return format_html ( '<STR_LIT>' , flatatt ( final_attrs ) ) <EOL> class TextInput ( Input ) : <EOL> input_type = '<STR_LIT:text>' <EOL> def __init__ ( self , attrs = None ) : <EOL> if attrs is not None : <EOL> self . input_type = attrs . pop ( '<STR_LIT:type>' , self . input_type ) <EOL> super ( TextInput , self ) . __init__ ( attrs ) <EOL> class NumberInput ( TextInput ) : <EOL> input_type = '<STR_LIT>' <EOL> class EmailInput ( TextInput ) : <EOL> input_type = '<STR_LIT:email>' <EOL> class URLInput ( TextInput ) : <EOL> input_type = '<STR_LIT:url>' <EOL> class PasswordInput ( TextInput ) : <EOL> input_type = '<STR_LIT:password>' <EOL> def __init__ ( self , attrs = None , render_value = False ) : <EOL> super ( PasswordInput , self ) . __init__ ( attrs ) <EOL> self . render_value = render_value <EOL> def render ( self , name , value , attrs = None ) : <EOL> if not self . render_value : value = None <EOL> return super ( PasswordInput , self ) . render ( name , value , attrs ) <EOL> class HiddenInput ( Input ) : <EOL> input_type = '<STR_LIT>' <EOL> is_hidden = True <EOL> class MultipleHiddenInput ( HiddenInput ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , choices = ( ) ) : <EOL> super ( MultipleHiddenInput , self ) . __init__ ( attrs ) <EOL> self . choices = choices <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> if value is None : value = [ ] <EOL> final_attrs = self . build_attrs ( attrs , type = self . input_type , name = name ) <EOL> id_ = final_attrs . get ( '<STR_LIT:id>' , None ) <EOL> inputs = [ ] <EOL> for i , v in enumerate ( value ) : <EOL> input_attrs = dict ( value = force_text ( v ) , ** final_attrs ) <EOL> if id_ : <EOL> input_attrs [ '<STR_LIT:id>' ] = '<STR_LIT>' % ( id_ , i ) <EOL> inputs . append ( format_html ( '<STR_LIT>' , flatatt ( input_attrs ) ) ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( inputs ) ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> if isinstance ( data , ( MultiValueDict , MergeDict ) ) : <EOL> return data . getlist ( name ) <EOL> return data . get ( name , None ) <EOL> class FileInput ( Input ) : <EOL> input_type = '<STR_LIT:file>' <EOL> needs_multipart_form = True <EOL> def render ( self , name , value , attrs = None ) : <EOL> return super ( FileInput , self ) . render ( name , None , attrs = attrs ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> "<STR_LIT>" <EOL> return files . get ( name , None ) <EOL> FILE_INPUT_CONTRADICTION = object ( ) <EOL> class ClearableFileInput ( FileInput ) : <EOL> initial_text = ugettext_lazy ( '<STR_LIT>' ) <EOL> input_text = ugettext_lazy ( '<STR_LIT>' ) <EOL> clear_checkbox_label = ugettext_lazy ( '<STR_LIT>' ) <EOL> template_with_initial = '<STR_LIT>' <EOL> template_with_clear = '<STR_LIT>' <EOL> url_markup_template = '<STR_LIT>' <EOL> def clear_checkbox_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name + '<STR_LIT>' <EOL> def clear_checkbox_id ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name + '<STR_LIT>' <EOL> def render ( self , name , value , attrs = None ) : <EOL> substitutions = { <EOL> '<STR_LIT>' : self . initial_text , <EOL> '<STR_LIT>' : self . input_text , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . clear_checkbox_label , <EOL> } <EOL> template = '<STR_LIT>' <EOL> substitutions [ '<STR_LIT:input>' ] = super ( ClearableFileInput , self ) . render ( name , value , attrs ) <EOL> if value and hasattr ( value , "<STR_LIT:url>" ) : <EOL> template = self . template_with_initial <EOL> substitutions [ '<STR_LIT>' ] = format_html ( self . url_markup_template , <EOL> value . url , <EOL> force_text ( value ) ) <EOL> if not self . is_required : <EOL> checkbox_name = self . clear_checkbox_name ( name ) <EOL> checkbox_id = self . clear_checkbox_id ( checkbox_name ) <EOL> substitutions [ '<STR_LIT>' ] = conditional_escape ( checkbox_name ) <EOL> substitutions [ '<STR_LIT>' ] = conditional_escape ( checkbox_id ) <EOL> substitutions [ '<STR_LIT>' ] = CheckboxInput ( ) . render ( checkbox_name , False , attrs = { '<STR_LIT:id>' : checkbox_id } ) <EOL> substitutions [ '<STR_LIT>' ] = self . template_with_clear % substitutions <EOL> return mark_safe ( template % substitutions ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> upload = super ( ClearableFileInput , self ) . value_from_datadict ( data , files , name ) <EOL> if not self . is_required and CheckboxInput ( ) . value_from_datadict ( <EOL> data , files , self . clear_checkbox_name ( name ) ) : <EOL> if upload : <EOL> return FILE_INPUT_CONTRADICTION <EOL> return False <EOL> return upload <EOL> class Textarea ( Widget ) : <EOL> def __init__ ( self , attrs = None ) : <EOL> default_attrs = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if attrs : <EOL> default_attrs . update ( attrs ) <EOL> super ( Textarea , self ) . __init__ ( default_attrs ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value is None : value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs , name = name ) <EOL> return format_html ( '<STR_LIT>' , <EOL> flatatt ( final_attrs ) , <EOL> force_text ( value ) ) <EOL> class DateInput ( TextInput ) : <EOL> def __init__ ( self , attrs = None , format = None ) : <EOL> super ( DateInput , self ) . __init__ ( attrs ) <EOL> if format : <EOL> self . format = format <EOL> self . manual_format = True <EOL> else : <EOL> self . format = formats . get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . manual_format = False <EOL> def _format_value ( self , value ) : <EOL> if self . is_localized and not self . manual_format : <EOL> return formats . localize_input ( value ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> value = datetime_safe . new_date ( value ) <EOL> return value . strftime ( self . format ) <EOL> return value <EOL> class DateTimeInput ( TextInput ) : <EOL> def __init__ ( self , attrs = None , format = None ) : <EOL> super ( DateTimeInput , self ) . __init__ ( attrs ) <EOL> if format : <EOL> self . format = format <EOL> self . manual_format = True <EOL> else : <EOL> self . format = formats . get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . manual_format = False <EOL> def _format_value ( self , value ) : <EOL> if self . is_localized and not self . manual_format : <EOL> return formats . localize_input ( value ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> value = datetime_safe . new_datetime ( value ) <EOL> return value . strftime ( self . format ) <EOL> return value <EOL> class TimeInput ( TextInput ) : <EOL> def __init__ ( self , attrs = None , format = None ) : <EOL> super ( TimeInput , self ) . __init__ ( attrs ) <EOL> if format : <EOL> self . format = format <EOL> self . manual_format = True <EOL> else : <EOL> self . format = formats . get_format ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . manual_format = False <EOL> def _format_value ( self , value ) : <EOL> if self . is_localized and not self . manual_format : <EOL> return formats . localize_input ( value ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> return value . strftime ( self . format ) <EOL> return value <EOL> def boolean_check ( v ) : <EOL> return not ( v is False or v is None or v == '<STR_LIT>' ) <EOL> class CheckboxInput ( Widget ) : <EOL> def __init__ ( self , attrs = None , check_test = None ) : <EOL> super ( CheckboxInput , self ) . __init__ ( attrs ) <EOL> self . check_test = boolean_check if check_test is None else check_test <EOL> def render ( self , name , value , attrs = None ) : <EOL> final_attrs = self . build_attrs ( attrs , type = '<STR_LIT>' , name = name ) <EOL> if self . check_test ( value ) : <EOL> final_attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not ( value is True or value is False or value is None or value == '<STR_LIT>' ) : <EOL> final_attrs [ '<STR_LIT:value>' ] = force_text ( value ) <EOL> return format_html ( '<STR_LIT>' , flatatt ( final_attrs ) ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> if name not in data : <EOL> return False <EOL> value = data . get ( name ) <EOL> values = { '<STR_LIT:true>' : True , '<STR_LIT:false>' : False } <EOL> if isinstance ( value , six . string_types ) : <EOL> value = values . get ( value . lower ( ) , value ) <EOL> return bool ( value ) <EOL> class Select ( Widget ) : <EOL> allow_multiple_selected = False <EOL> def __init__ ( self , attrs = None , choices = ( ) ) : <EOL> super ( Select , self ) . __init__ ( attrs ) <EOL> self . choices = list ( choices ) <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> if value is None : value = '<STR_LIT>' <EOL> final_attrs = self . build_attrs ( attrs , name = name ) <EOL> output = [ format_html ( '<STR_LIT>' , flatatt ( final_attrs ) ) ] <EOL> options = self . render_options ( choices , [ value ] ) <EOL> if options : <EOL> output . append ( options ) <EOL> output . append ( '<STR_LIT>' ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( output ) ) <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> option_value = force_text ( option_value ) <EOL> if option_value in selected_choices : <EOL> selected_html = mark_safe ( '<STR_LIT>' ) <EOL> if not self . allow_multiple_selected : <EOL> selected_choices . remove ( option_value ) <EOL> else : <EOL> selected_html = '<STR_LIT>' <EOL> return format_html ( '<STR_LIT>' , <EOL> option_value , <EOL> selected_html , <EOL> force_text ( option_label ) ) <EOL> def render_options ( self , choices , selected_choices ) : <EOL> selected_choices = set ( force_text ( v ) for v in selected_choices ) <EOL> output = [ ] <EOL> for option_value , option_label in chain ( self . choices , choices ) : <EOL> if isinstance ( option_label , ( list , tuple ) ) : <EOL> output . append ( format_html ( '<STR_LIT>' , force_text ( option_value ) ) ) <EOL> for option in option_label : <EOL> output . append ( self . render_option ( selected_choices , * option ) ) <EOL> output . append ( '<STR_LIT>' ) <EOL> else : <EOL> output . append ( self . render_option ( selected_choices , option_value , option_label ) ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> class NullBooleanSelect ( Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> choices = ( ( '<STR_LIT:1>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:2>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:3>' , ugettext_lazy ( '<STR_LIT>' ) ) ) <EOL> super ( NullBooleanSelect , self ) . __init__ ( attrs , choices ) <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> try : <EOL> value = { True : '<STR_LIT:2>' , False : '<STR_LIT:3>' , '<STR_LIT:2>' : '<STR_LIT:2>' , '<STR_LIT:3>' : '<STR_LIT:3>' } [ value ] <EOL> except KeyError : <EOL> value = '<STR_LIT:1>' <EOL> return super ( NullBooleanSelect , self ) . render ( name , value , attrs , choices ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> value = data . get ( name , None ) <EOL> return { '<STR_LIT:2>' : True , <EOL> True : True , <EOL> '<STR_LIT:True>' : True , <EOL> '<STR_LIT:3>' : False , <EOL> '<STR_LIT:False>' : False , <EOL> False : False } . get ( value , None ) <EOL> class SelectMultiple ( Select ) : <EOL> allow_multiple_selected = True <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> if value is None : value = [ ] <EOL> final_attrs = self . build_attrs ( attrs , name = name ) <EOL> output = [ format_html ( '<STR_LIT>' , flatatt ( final_attrs ) ) ] <EOL> options = self . render_options ( choices , value ) <EOL> if options : <EOL> output . append ( options ) <EOL> output . append ( '<STR_LIT>' ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( output ) ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> if isinstance ( data , ( MultiValueDict , MergeDict ) ) : <EOL> return data . getlist ( name ) <EOL> return data . get ( name , None ) <EOL> @ python_2_unicode_compatible <EOL> class ChoiceInput ( SubWidget ) : <EOL> """<STR_LIT>""" <EOL> input_type = None <EOL> def __init__ ( self , name , value , attrs , choice , index ) : <EOL> self . name = name <EOL> self . value = value <EOL> self . attrs = attrs <EOL> self . choice_value = force_text ( choice [ <NUM_LIT:0> ] ) <EOL> self . choice_label = force_text ( choice [ <NUM_LIT:1> ] ) <EOL> self . index = index <EOL> def __str__ ( self ) : <EOL> return self . render ( ) <EOL> def render ( self , name = None , value = None , attrs = None , choices = ( ) ) : <EOL> name = name or self . name <EOL> value = value or self . value <EOL> attrs = attrs or self . attrs <EOL> if '<STR_LIT:id>' in self . attrs : <EOL> label_for = format_html ( '<STR_LIT>' , self . attrs [ '<STR_LIT:id>' ] , self . index ) <EOL> else : <EOL> label_for = '<STR_LIT>' <EOL> return format_html ( '<STR_LIT>' , label_for , self . tag ( ) , self . choice_label ) <EOL> def is_checked ( self ) : <EOL> return self . value == self . choice_value <EOL> def tag ( self ) : <EOL> if '<STR_LIT:id>' in self . attrs : <EOL> self . attrs [ '<STR_LIT:id>' ] = '<STR_LIT>' % ( self . attrs [ '<STR_LIT:id>' ] , self . index ) <EOL> final_attrs = dict ( self . attrs , type = self . input_type , name = self . name , value = self . choice_value ) <EOL> if self . is_checked ( ) : <EOL> final_attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return format_html ( '<STR_LIT>' , flatatt ( final_attrs ) ) <EOL> class RadioChoiceInput ( ChoiceInput ) : <EOL> input_type = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RadioChoiceInput , self ) . __init__ ( * args , ** kwargs ) <EOL> self . value = force_text ( self . value ) <EOL> class RadioInput ( RadioChoiceInput ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> msg = "<STR_LIT>" <EOL> warnings . warn ( msg , PendingDeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> super ( RadioInput , self ) . __init__ ( * args , ** kwargs ) <EOL> class CheckboxChoiceInput ( ChoiceInput ) : <EOL> input_type = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CheckboxChoiceInput , self ) . __init__ ( * args , ** kwargs ) <EOL> self . value = set ( force_text ( v ) for v in self . value ) <EOL> def is_checked ( self ) : <EOL> return self . choice_value in self . value <EOL> @ python_2_unicode_compatible <EOL> class ChoiceFieldRenderer ( object ) : <EOL> """<STR_LIT>""" <EOL> choice_input_class = None <EOL> def __init__ ( self , name , value , attrs , choices ) : <EOL> self . name = name <EOL> self . value = value <EOL> self . attrs = attrs <EOL> self . choices = choices <EOL> def __iter__ ( self ) : <EOL> for i , choice in enumerate ( self . choices ) : <EOL> yield self . choice_input_class ( self . name , self . value , self . attrs . copy ( ) , choice , i ) <EOL> def __getitem__ ( self , idx ) : <EOL> choice = self . choices [ idx ] <EOL> return self . choice_input_class ( self . name , self . value , self . attrs . copy ( ) , choice , idx ) <EOL> def __str__ ( self ) : <EOL> return self . render ( ) <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> id_ = self . attrs . get ( '<STR_LIT:id>' , None ) <EOL> start_tag = format_html ( '<STR_LIT>' , id_ ) if id_ else '<STR_LIT>' <EOL> output = [ start_tag ] <EOL> for widget in self : <EOL> output . append ( format_html ( '<STR_LIT>' , force_text ( widget ) ) ) <EOL> output . append ( '<STR_LIT>' ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( output ) ) <EOL> class RadioFieldRenderer ( ChoiceFieldRenderer ) : <EOL> choice_input_class = RadioChoiceInput <EOL> class CheckboxFieldRenderer ( ChoiceFieldRenderer ) : <EOL> choice_input_class = CheckboxChoiceInput <EOL> class RendererMixin ( object ) : <EOL> renderer = None <EOL> _empty_value = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> renderer = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if renderer : <EOL> self . renderer = renderer <EOL> super ( RendererMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> def subwidgets ( self , name , value , attrs = None , choices = ( ) ) : <EOL> for widget in self . get_renderer ( name , value , attrs , choices ) : <EOL> yield widget <EOL> def get_renderer ( self , name , value , attrs = None , choices = ( ) ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> value = self . _empty_value <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> choices = list ( chain ( self . choices , choices ) ) <EOL> return self . renderer ( name , value , final_attrs , choices ) <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> return self . get_renderer ( name , value , attrs , choices ) . render ( ) <EOL> def id_for_label ( self , id_ ) : <EOL> if id_ : <EOL> id_ += '<STR_LIT>' <EOL> return id_ <EOL> class RadioSelect ( RendererMixin , Select ) : <EOL> renderer = RadioFieldRenderer <EOL> _empty_value = '<STR_LIT>' <EOL> class CheckboxSelectMultiple ( RendererMixin , SelectMultiple ) : <EOL> renderer = CheckboxFieldRenderer <EOL> _empty_value = [ ] <EOL> class MultiWidget ( Widget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , widgets , attrs = None ) : <EOL> self . widgets = [ w ( ) if isinstance ( w , type ) else w for w in widgets ] <EOL> super ( MultiWidget , self ) . __init__ ( attrs ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> if self . is_localized : <EOL> for widget in self . widgets : <EOL> widget . is_localized = self . is_localized <EOL> if not isinstance ( value , list ) : <EOL> value = self . decompress ( value ) <EOL> output = [ ] <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> id_ = final_attrs . get ( '<STR_LIT:id>' , None ) <EOL> for i , widget in enumerate ( self . widgets ) : <EOL> try : <EOL> widget_value = value [ i ] <EOL> except IndexError : <EOL> widget_value = None <EOL> if id_ : <EOL> final_attrs = dict ( final_attrs , id = '<STR_LIT>' % ( id_ , i ) ) <EOL> output . append ( widget . render ( name + '<STR_LIT>' % i , widget_value , final_attrs ) ) <EOL> return mark_safe ( self . format_output ( output ) ) <EOL> def id_for_label ( self , id_ ) : <EOL> if id_ : <EOL> id_ += '<STR_LIT>' <EOL> return id_ <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> return [ widget . value_from_datadict ( data , files , name + '<STR_LIT>' % i ) for i , widget in enumerate ( self . widgets ) ] <EOL> def format_output ( self , rendered_widgets ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( rendered_widgets ) <EOL> def decompress ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _get_media ( self ) : <EOL> "<STR_LIT>" <EOL> media = Media ( ) <EOL> for w in self . widgets : <EOL> media = media + w . media <EOL> return media <EOL> media = property ( _get_media ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> obj = super ( MultiWidget , self ) . __deepcopy__ ( memo ) <EOL> obj . widgets = copy . deepcopy ( self . widgets ) <EOL> return obj <EOL> @ property <EOL> def needs_multipart_form ( self ) : <EOL> return any ( w . needs_multipart_form for w in self . widgets ) <EOL> class SplitDateTimeWidget ( MultiWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , date_format = None , time_format = None ) : <EOL> widgets = ( DateInput ( attrs = attrs , format = date_format ) , <EOL> TimeInput ( attrs = attrs , format = time_format ) ) <EOL> super ( SplitDateTimeWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> if value : <EOL> value = to_current_timezone ( value ) <EOL> return [ value . date ( ) , value . time ( ) . replace ( microsecond = <NUM_LIT:0> ) ] <EOL> return [ None , None ] <EOL> class SplitHiddenDateTimeWidget ( SplitDateTimeWidget ) : <EOL> """<STR_LIT>""" <EOL> is_hidden = True <EOL> def __init__ ( self , attrs = None , date_format = None , time_format = None ) : <EOL> super ( SplitHiddenDateTimeWidget , self ) . __init__ ( attrs , date_format , time_format ) <EOL> for widget in self . widgets : <EOL> widget . input_type = '<STR_LIT>' <EOL> widget . is_hidden = True </s>
