<s> from __future__ import unicode_literals <EOL> from django import VERSION <EOL> from django . forms . widgets import Media , Widget , CheckboxInput <EOL> from django . utils . html import conditional_escape <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class ClearableWidgetWrapper ( Widget ) : <EOL> """<STR_LIT>""" <EOL> clear_checkbox_label = _ ( "<STR_LIT:None>" ) <EOL> template = '<STR_LIT>' <EOL> class Media : <EOL> js = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , widget , empty_value = None ) : <EOL> """<STR_LIT>""" <EOL> self . widget = widget <EOL> self . checkbox = CheckboxInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . empty_value = empty_value <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name != '<STR_LIT>' : <EOL> return getattr ( self . widget , name ) <EOL> raise AttributeError <EOL> @ property <EOL> def media ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . widget . media + self . checkbox . media + Media ( self . Media ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> """<STR_LIT>""" <EOL> wrapped = self . widget . render ( name , value , attrs ) <EOL> checkbox_name = self . clear_checkbox_name ( name ) <EOL> checkbox_id = self . clear_checkbox_id ( checkbox_name ) <EOL> checkbox_label = self . clear_checkbox_label <EOL> checkbox = self . checkbox . render ( <EOL> checkbox_name , value == self . empty_value , attrs = { '<STR_LIT:id>' : checkbox_id } ) <EOL> return mark_safe ( self . template . format ( <EOL> conditional_escape ( wrapped ) , <EOL> conditional_escape ( checkbox_id ) , <EOL> conditional_escape ( checkbox_label ) , <EOL> conditional_escape ( checkbox ) ) ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> """<STR_LIT>""" <EOL> clear = self . checkbox . value_from_datadict ( data , files , self . clear_checkbox_name ( name ) ) <EOL> if clear : <EOL> return self . empty_value <EOL> return self . widget . value_from_datadict ( data , files , name ) <EOL> if VERSION < ( <NUM_LIT:1> , <NUM_LIT:6> ) : <EOL> def _has_changed ( self , initial , data ) : <EOL> """<STR_LIT>""" <EOL> if ( initial is None and data is not None ) or ( initial is not None and data is None ) : <EOL> return True <EOL> return self . widget . _has_changed ( initial , data ) <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>' </s>
