<s> from __future__ import unicode_literals <EOL> import json <EOL> from django . forms . widgets import TextInput <EOL> from django . utils . safestring import mark_safe <EOL> class ColorFieldWidget ( TextInput ) : <EOL> class Media : <EOL> css = { <EOL> '<STR_LIT:all>' : ( "<STR_LIT>" , ) <EOL> } <EOL> js = ( "<STR_LIT>" , ) <EOL> input_type = '<STR_LIT>' <EOL> def __init__ ( self , colors = None , attrs = None ) : <EOL> self . colors = colors <EOL> super ( ColorFieldWidget , self ) . __init__ ( attrs ) <EOL> def render_datalist ( self , list_id ) : <EOL> return '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' % list_id , <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' % color for color in self . colors ] ) , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> def render_script ( self , id ) : <EOL> options = { } <EOL> if self . colors : <EOL> options [ '<STR_LIT>' ] = [ c . replace ( '<STR_LIT:#>' , '<STR_LIT>' ) for c in self . colors ] <EOL> return '''<STR_LIT>''' % ( id , json . dumps ( options ) ) <EOL> def render ( self , name , value , attrs = { } ) : <EOL> parts = [ ] <EOL> if '<STR_LIT:id>' not in attrs : <EOL> attrs [ '<STR_LIT:id>' ] = "<STR_LIT>" % name <EOL> if self . colors : <EOL> attrs [ '<STR_LIT:list>' ] = '<STR_LIT>' % attrs [ '<STR_LIT:id>' ] <EOL> parts . append ( self . render_datalist ( attrs [ '<STR_LIT:list>' ] ) ) <EOL> parts . append ( super ( ColorFieldWidget , self ) . render ( name , value , attrs ) ) <EOL> parts . append ( self . render_script ( attrs [ '<STR_LIT:id>' ] ) ) <EOL> return mark_safe ( '<STR_LIT>' . join ( parts ) ) </s>
