<s> import six <EOL> from django import forms <EOL> from django . core import urlresolvers <EOL> from django . core . urlresolvers import reverse_lazy <EOL> from django . forms import fields , widgets <EOL> from django . forms . utils import flatatt <EOL> from django . utils import html <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import Promise <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django_select2 . forms import ModelSelect2Widget , Select2Widget <EOL> class SelectWidget ( widgets . Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , choices = ( ) , data_attrs = ( ) , transform = None , <EOL> transform_html_attrs = None ) : <EOL> self . data_attrs = data_attrs <EOL> self . transform = transform <EOL> self . transform_html_attrs = transform_html_attrs <EOL> super ( SelectWidget , self ) . __init__ ( attrs , choices ) <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> option_value = force_text ( option_value ) <EOL> other_html = ( u'<STR_LIT>' <EOL> if option_value in selected_choices else '<STR_LIT>' ) <EOL> if callable ( self . transform_html_attrs ) : <EOL> html_attrs = self . transform_html_attrs ( option_label ) <EOL> other_html += flatatt ( html_attrs ) <EOL> if not isinstance ( option_label , ( six . string_types , Promise ) ) : <EOL> for data_attr in self . data_attrs : <EOL> data_value = html . conditional_escape ( <EOL> force_text ( getattr ( option_label , <EOL> data_attr , "<STR_LIT>" ) ) ) <EOL> other_html += '<STR_LIT>' % ( data_attr , data_value ) <EOL> if callable ( self . transform ) : <EOL> option_label = self . transform ( option_label ) <EOL> return u'<STR_LIT>' % ( <EOL> html . escape ( option_value ) , other_html , <EOL> html . conditional_escape ( force_text ( option_label ) ) ) <EOL> class DynamicSelectWidget ( Select2Widget ) : <EOL> """<STR_LIT>""" <EOL> _data_add_url_attr = "<STR_LIT>" <EOL> _data_edit_url_attr = "<STR_LIT>" <EOL> def render ( self , * args , ** kwargs ) : <EOL> add_item_url = self . get_add_item_url ( ) <EOL> if add_item_url is not None : <EOL> self . attrs [ self . _data_add_url_attr ] = add_item_url <EOL> content = super ( DynamicSelectWidget , self ) . render ( * args , ** kwargs ) <EOL> if self . get_cls_name ( ) or self . get_update_view_name ( ) : <EOL> content += self . get_edit_handler ( ) <EOL> return content <EOL> def get_edit_handler ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . generic = '<STR_LIT:false>' <EOL> return '''<STR_LIT>''' % { '<STR_LIT>' : self . get_update_view_name ( ) , <EOL> '<STR_LIT>' : self . get_cls_name ( ) , <EOL> '<STR_LIT:url>' : self . get_add_item_url ( ) , <EOL> '<STR_LIT>' : self . get_form_cls ( ) , <EOL> '<STR_LIT:id>' : self . __hash__ ( ) , <EOL> '<STR_LIT>' : self . generic } <EOL> def get_update_view_name ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _edit_url <EOL> self . _edit_url = self . get_edit_item_url ( ) <EOL> if not self . _edit_url : <EOL> if self . get_form_cls ( ) : <EOL> self . _edit_url = '<STR_LIT>' <EOL> else : <EOL> self . _edit_url = '<STR_LIT>' <EOL> self . generic = '<STR_LIT:true>' <EOL> return self . _edit_url <EOL> def get_add_item_url ( self ) : <EOL> if callable ( self . add_item_link ) : <EOL> return self . add_item_link ( ) <EOL> try : <EOL> if self . add_item_link_args : <EOL> return urlresolvers . reverse ( self . add_item_link , <EOL> args = self . add_item_link_args ) <EOL> else : <EOL> return urlresolvers . reverse ( self . add_item_link ) <EOL> except urlresolvers . NoReverseMatch : <EOL> return self . add_item_link <EOL> def get_edit_item_url ( self ) : <EOL> if callable ( self . edit_item_link ) : <EOL> return self . edit_item_link ( ) <EOL> try : <EOL> if self . add_item_link_args : <EOL> return urlresolvers . reverse ( self . edit_item_link , <EOL> args = self . add_item_link_args ) <EOL> else : <EOL> return urlresolvers . reverse ( self . edit_item_link ) <EOL> except urlresolvers . NoReverseMatch : <EOL> return self . edit_item_link <EOL> def get_cls_name ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . cls_name <EOL> return '<STR_LIT>' <EOL> def get_form_cls ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . form_cls <EOL> return '<STR_LIT>' <EOL> class DynamicChoiceField ( fields . ChoiceField ) : <EOL> """<STR_LIT>""" <EOL> widget = DynamicSelectWidget <EOL> def __init__ ( self , <EOL> add_item_link = None , <EOL> add_item_link_args = None , <EOL> edit_item_link = None , <EOL> edit_item_link_args = None , <EOL> cls_name = None , <EOL> form_cls = None , <EOL> search_fields = None , <EOL> * args , <EOL> ** kwargs ) : <EOL> super ( DynamicChoiceField , self ) . __init__ ( * args , ** kwargs ) <EOL> if search_fields : <EOL> self . widget . search_fields = search_fields <EOL> if cls_name or hasattr ( self , '<STR_LIT>' ) : <EOL> self . widget . cls_name = cls_name or self . cls_name <EOL> cls_name = cls_name or self . cls_name <EOL> if form_cls or hasattr ( self , '<STR_LIT>' ) : <EOL> self . widget . form_cls = form_cls or self . form_cls <EOL> form_cls = form_cls or self . form_cls <EOL> if search_fields : <EOL> self . widget . search_fields = search_fields <EOL> if cls_name and not add_item_link and not form_cls : <EOL> self . widget . add_item_link = '<STR_LIT>' <EOL> self . widget . add_item_link_args = ( cls_name , ) <EOL> if cls_name and form_cls and not add_item_link : <EOL> self . widget . add_item_link = '<STR_LIT>' <EOL> self . widget . add_item_link_args = ( cls_name , form_cls ) <EOL> if not form_cls : <EOL> self . widget . add_item_link = add_item_link <EOL> self . widget . add_item_link_args = add_item_link_args <EOL> self . widget . edit_item_link = edit_item_link or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> self . widget . edit_item_link_args = edit_item_link_args or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> DEFAULT_SEARCH_FIELDS = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> class DynamicModelSelect2Widget ( DynamicSelectWidget , ModelSelect2Widget ) : <EOL> pass <EOL> class DynamicModelChoiceField ( forms . ModelChoiceField ) : <EOL> """<STR_LIT>""" <EOL> widget = DynamicModelSelect2Widget <EOL> def __init__ ( self , <EOL> add_item_link = None , <EOL> add_item_link_args = None , <EOL> edit_item_link = None , <EOL> edit_item_link_args = None , <EOL> cls_name = None , <EOL> form_cls = None , <EOL> search_fields = None , <EOL> * args , <EOL> ** kwargs ) : <EOL> super ( DynamicModelChoiceField , self ) . __init__ ( * args , ** kwargs ) <EOL> if cls_name or hasattr ( self , '<STR_LIT>' ) : <EOL> self . widget . cls_name = cls_name or self . cls_name <EOL> cls_name = cls_name or self . cls_name <EOL> if form_cls or hasattr ( self , '<STR_LIT>' ) : <EOL> self . widget . form_cls = form_cls or self . form_cls <EOL> form_cls = form_cls or self . form_cls <EOL> if cls_name and not add_item_link and not form_cls : <EOL> self . widget . add_item_link = '<STR_LIT>' <EOL> self . widget . add_item_link_args = ( cls_name , ) <EOL> if cls_name and form_cls and not add_item_link : <EOL> self . widget . add_item_link = '<STR_LIT>' <EOL> self . widget . add_item_link_args = ( cls_name , form_cls ) <EOL> if search_fields : <EOL> self . widget . search_fields = search_fields <EOL> if not cls_name : <EOL> self . widget . add_item_link = add_item_link or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> self . widget . add_item_link_args = add_item_link_args or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> self . widget . edit_item_link = edit_item_link or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> self . widget . edit_item_link_args = edit_item_link_args or getattr ( <EOL> self , '<STR_LIT>' , None ) <EOL> class DynamicTypedChoiceField ( DynamicChoiceField , fields . TypedChoiceField ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
