<s> from __future__ import unicode_literals <EOL> from django . contrib . auth . forms import ReadOnlyPasswordHashWidget <EOL> from django . forms import ( <EOL> TextInput , DateInput , FileInput , CheckboxInput , MultiWidget , <EOL> ClearableFileInput , Select , RadioSelect , CheckboxSelectMultiple <EOL> ) <EOL> from django . forms . extras import SelectDateWidget <EOL> from django . forms . forms import BaseForm , BoundField <EOL> from django . forms . formsets import BaseFormSet <EOL> from django . utils . html import conditional_escape , escape , strip_tags <EOL> from django . utils . safestring import mark_safe <EOL> from . bootstrap import get_bootstrap_setting <EOL> from . exceptions import BootstrapError <EOL> from . forms import ( <EOL> render_form , render_field , render_label , render_form_group , <EOL> is_widget_with_placeholder , is_widget_required_attribute , FORM_GROUP_CLASS <EOL> ) <EOL> from . text import text_value <EOL> from . utils import add_css_class , render_template_file <EOL> class BaseRenderer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . layout = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . form_group_class = kwargs . get ( <EOL> '<STR_LIT>' , FORM_GROUP_CLASS ) <EOL> self . field_class = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . label_class = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . show_help = kwargs . get ( '<STR_LIT>' , True ) <EOL> self . show_label = kwargs . get ( '<STR_LIT>' , True ) <EOL> self . exclude = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . set_required = kwargs . get ( '<STR_LIT>' , True ) <EOL> self . set_disabled = kwargs . get ( '<STR_LIT>' , False ) <EOL> self . size = self . parse_size ( kwargs . get ( '<STR_LIT:size>' , '<STR_LIT>' ) ) <EOL> self . horizontal_label_class = kwargs . get ( <EOL> '<STR_LIT>' , <EOL> get_bootstrap_setting ( '<STR_LIT>' ) <EOL> ) <EOL> self . horizontal_field_class = kwargs . get ( <EOL> '<STR_LIT>' , <EOL> get_bootstrap_setting ( '<STR_LIT>' ) <EOL> ) <EOL> def parse_size ( self , size ) : <EOL> size = text_value ( size ) . lower ( ) . strip ( ) <EOL> if size in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if size in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> if size in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> raise BootstrapError ( '<STR_LIT>' % size ) <EOL> def get_size_class ( self , prefix = '<STR_LIT:input>' ) : <EOL> if self . size == '<STR_LIT>' : <EOL> return prefix + '<STR_LIT>' <EOL> if self . size == '<STR_LIT>' : <EOL> return prefix + '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def _render ( self ) : <EOL> return '<STR_LIT>' <EOL> def render ( self ) : <EOL> return mark_safe ( self . _render ( ) ) <EOL> class FormsetRenderer ( BaseRenderer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , formset , * args , ** kwargs ) : <EOL> if not isinstance ( formset , BaseFormSet ) : <EOL> raise BootstrapError ( <EOL> '<STR_LIT>' ) <EOL> self . formset = formset <EOL> super ( FormsetRenderer , self ) . __init__ ( * args , ** kwargs ) <EOL> def render_management_form ( self ) : <EOL> return text_value ( self . formset . management_form ) <EOL> def render_form ( self , form , ** kwargs ) : <EOL> return render_form ( form , ** kwargs ) <EOL> def render_forms ( self ) : <EOL> rendered_forms = [ ] <EOL> for form in self . formset . forms : <EOL> rendered_forms . append ( self . render_form ( <EOL> form , <EOL> layout = self . layout , <EOL> form_group_class = self . form_group_class , <EOL> field_class = self . field_class , <EOL> label_class = self . label_class , <EOL> show_label = self . show_label , <EOL> show_help = self . show_help , <EOL> exclude = self . exclude , <EOL> set_required = self . set_required , <EOL> set_disabled = self . set_disabled , <EOL> size = self . size , <EOL> horizontal_label_class = self . horizontal_label_class , <EOL> horizontal_field_class = self . horizontal_field_class , <EOL> ) ) <EOL> return '<STR_LIT:\n>' . join ( rendered_forms ) <EOL> def get_formset_errors ( self ) : <EOL> return self . formset . non_form_errors ( ) <EOL> def render_errors ( self ) : <EOL> formset_errors = self . get_formset_errors ( ) <EOL> if formset_errors : <EOL> return render_template_file ( <EOL> '<STR_LIT>' , <EOL> context = { <EOL> '<STR_LIT>' : formset_errors , <EOL> '<STR_LIT>' : self . formset , <EOL> '<STR_LIT>' : self . layout , <EOL> } <EOL> ) <EOL> return '<STR_LIT>' <EOL> def _render ( self ) : <EOL> return '<STR_LIT>' . join ( <EOL> [ <EOL> self . render_errors ( ) , <EOL> self . render_management_form ( ) , <EOL> self . render_forms ( ) , <EOL> ] <EOL> ) <EOL> class FormRenderer ( BaseRenderer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , form , * args , ** kwargs ) : <EOL> if not isinstance ( form , BaseForm ) : <EOL> raise BootstrapError ( <EOL> '<STR_LIT>' ) <EOL> self . form = form <EOL> super ( FormRenderer , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . form . empty_permitted : <EOL> self . set_required = False <EOL> self . error_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> self . required_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> self . bound_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> def render_fields ( self ) : <EOL> rendered_fields = [ ] <EOL> for field in self . form : <EOL> rendered_fields . append ( render_field ( <EOL> field , <EOL> layout = self . layout , <EOL> form_group_class = self . form_group_class , <EOL> field_class = self . field_class , <EOL> label_class = self . label_class , <EOL> show_label = self . show_label , <EOL> show_help = self . show_help , <EOL> exclude = self . exclude , <EOL> set_required = self . set_required , <EOL> set_disabled = self . set_disabled , <EOL> size = self . size , <EOL> horizontal_label_class = self . horizontal_label_class , <EOL> horizontal_field_class = self . horizontal_field_class , <EOL> error_css_class = self . error_css_class , <EOL> required_css_class = self . required_css_class , <EOL> bound_css_class = self . bound_css_class , <EOL> ) ) <EOL> return '<STR_LIT:\n>' . join ( rendered_fields ) <EOL> def get_fields_errors ( self ) : <EOL> form_errors = [ ] <EOL> for field in self . form : <EOL> if field . is_hidden and field . errors : <EOL> form_errors += field . errors <EOL> return form_errors <EOL> def render_errors ( self , type = '<STR_LIT:all>' ) : <EOL> form_errors = None <EOL> if type == '<STR_LIT:all>' : <EOL> form_errors = self . get_fields_errors ( ) + self . form . non_field_errors ( ) <EOL> elif type == '<STR_LIT>' : <EOL> form_errors = self . get_fields_errors ( ) <EOL> elif type == '<STR_LIT>' : <EOL> form_errors = self . form . non_field_errors ( ) <EOL> if form_errors : <EOL> return render_template_file ( <EOL> '<STR_LIT>' , <EOL> context = { <EOL> '<STR_LIT>' : form_errors , <EOL> '<STR_LIT>' : self . form , <EOL> '<STR_LIT>' : self . layout , <EOL> } <EOL> ) <EOL> return '<STR_LIT>' <EOL> def _render ( self ) : <EOL> return self . render_errors ( ) + self . render_fields ( ) <EOL> class FieldRenderer ( BaseRenderer ) : <EOL> """<STR_LIT>""" <EOL> WIDGETS_NO_FORM_CONTROL = ( <EOL> CheckboxInput , <EOL> RadioSelect , <EOL> CheckboxSelectMultiple , <EOL> FileInput , <EOL> ) <EOL> def __init__ ( self , field , * args , ** kwargs ) : <EOL> if not isinstance ( field , BoundField ) : <EOL> raise BootstrapError ( '<STR_LIT>' ) <EOL> self . field = field <EOL> super ( FieldRenderer , self ) . __init__ ( * args , ** kwargs ) <EOL> self . widget = field . field . widget <EOL> self . is_multi_widget = isinstance ( field . field . widget , MultiWidget ) <EOL> self . initial_attrs = self . widget . attrs . copy ( ) <EOL> self . field_help = text_value ( mark_safe ( field . help_text ) ) if self . show_help and field . help_text else '<STR_LIT>' <EOL> self . field_errors = [ conditional_escape ( text_value ( error ) ) for error in field . errors ] <EOL> if get_bootstrap_setting ( '<STR_LIT>' ) : <EOL> self . placeholder = field . label <EOL> else : <EOL> self . placeholder = '<STR_LIT>' <EOL> self . addon_before = kwargs . get ( '<STR_LIT>' , self . widget . attrs . pop ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . addon_after = kwargs . get ( '<STR_LIT>' , self . widget . attrs . pop ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> error_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> required_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> bound_css_class = kwargs . get ( '<STR_LIT>' , None ) <EOL> if error_css_class is not None : <EOL> self . error_css_class = error_css_class <EOL> else : <EOL> self . error_css_class = getattr ( <EOL> field . form , '<STR_LIT>' , <EOL> get_bootstrap_setting ( '<STR_LIT>' ) ) <EOL> if required_css_class is not None : <EOL> self . required_css_class = required_css_class <EOL> else : <EOL> self . required_css_class = getattr ( <EOL> field . form , '<STR_LIT>' , <EOL> get_bootstrap_setting ( '<STR_LIT>' ) ) <EOL> if bound_css_class is not None : <EOL> self . success_css_class = bound_css_class <EOL> else : <EOL> self . success_css_class = getattr ( <EOL> field . form , '<STR_LIT>' , <EOL> get_bootstrap_setting ( '<STR_LIT>' ) ) <EOL> if self . field . form . empty_permitted : <EOL> self . set_required = False <EOL> self . required_css_class = '<STR_LIT>' <EOL> self . set_disabled = kwargs . get ( '<STR_LIT>' , False ) <EOL> def restore_widget_attrs ( self ) : <EOL> self . widget . attrs = self . initial_attrs . copy ( ) <EOL> def add_class_attrs ( self , widget = None ) : <EOL> if widget is None : <EOL> widget = self . widget <EOL> classes = widget . attrs . get ( '<STR_LIT:class>' , '<STR_LIT>' ) <EOL> if isinstance ( widget , ReadOnlyPasswordHashWidget ) : <EOL> classes = add_css_class ( classes , '<STR_LIT>' , prepend = True ) <EOL> elif not isinstance ( widget , self . WIDGETS_NO_FORM_CONTROL ) : <EOL> classes = add_css_class ( classes , '<STR_LIT>' , prepend = True ) <EOL> classes = add_css_class ( classes , self . get_size_class ( ) ) <EOL> widget . attrs [ '<STR_LIT:class>' ] = classes <EOL> def add_placeholder_attrs ( self , widget = None ) : <EOL> if widget is None : <EOL> widget = self . widget <EOL> placeholder = widget . attrs . get ( '<STR_LIT>' , self . placeholder ) <EOL> if placeholder and is_widget_with_placeholder ( widget ) : <EOL> widget . attrs [ '<STR_LIT>' ] = placeholder <EOL> def add_help_attrs ( self , widget = None ) : <EOL> if widget is None : <EOL> widget = self . widget <EOL> if not isinstance ( widget , CheckboxInput ) : <EOL> widget . attrs [ '<STR_LIT:title>' ] = widget . attrs . get ( <EOL> '<STR_LIT:title>' , <EOL> escape ( strip_tags ( self . field_help ) ) <EOL> ) <EOL> def add_required_attrs ( self , widget = None ) : <EOL> if widget is None : <EOL> widget = self . widget <EOL> if self . set_required and is_widget_required_attribute ( widget ) : <EOL> widget . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def add_disabled_attrs ( self , widget = None ) : <EOL> if widget is None : <EOL> widget = self . widget <EOL> if self . set_disabled : <EOL> widget . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def add_widget_attrs ( self ) : <EOL> if self . is_multi_widget : <EOL> widgets = self . widget . widgets <EOL> else : <EOL> widgets = [ self . widget ] <EOL> for widget in widgets : <EOL> self . add_class_attrs ( widget ) <EOL> self . add_placeholder_attrs ( widget ) <EOL> self . add_help_attrs ( widget ) <EOL> self . add_required_attrs ( widget ) <EOL> self . add_disabled_attrs ( widget ) <EOL> def list_to_class ( self , html , klass ) : <EOL> classes = add_css_class ( klass , self . get_size_class ( ) ) <EOL> mapping = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' . format ( klass = classes ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> for k , v in mapping : <EOL> html = html . replace ( k , v ) <EOL> return html <EOL> def put_inside_label ( self , html ) : <EOL> content = '<STR_LIT>' . format ( <EOL> field = html , <EOL> label = self . field . label , <EOL> ) <EOL> return render_label ( <EOL> content = mark_safe ( content ) , <EOL> label_for = self . field . id_for_label , <EOL> label_title = escape ( strip_tags ( self . field_help ) ) <EOL> ) <EOL> def fix_date_select_input ( self , html ) : <EOL> div1 = '<STR_LIT>' <EOL> div2 = '<STR_LIT>' <EOL> html = html . replace ( '<STR_LIT>' , div1 + '<STR_LIT>' ) <EOL> html = html . replace ( '<STR_LIT>' , '<STR_LIT>' + div2 ) <EOL> return '<STR_LIT>' + html + '<STR_LIT>' <EOL> def fix_clearable_file_input ( self , html ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( <EOL> html = html <EOL> ) <EOL> def post_widget_render ( self , html ) : <EOL> if isinstance ( self . widget , RadioSelect ) : <EOL> html = self . list_to_class ( html , '<STR_LIT>' ) <EOL> elif isinstance ( self . widget , CheckboxSelectMultiple ) : <EOL> html = self . list_to_class ( html , '<STR_LIT>' ) <EOL> elif isinstance ( self . widget , SelectDateWidget ) : <EOL> html = self . fix_date_select_input ( html ) <EOL> elif isinstance ( self . widget , ClearableFileInput ) : <EOL> html = self . fix_clearable_file_input ( html ) <EOL> elif isinstance ( self . widget , CheckboxInput ) : <EOL> html = self . put_inside_label ( html ) <EOL> return html <EOL> def wrap_widget ( self , html ) : <EOL> if isinstance ( self . widget , CheckboxInput ) : <EOL> checkbox_class = add_css_class ( '<STR_LIT>' , self . get_size_class ( ) ) <EOL> html = '<STR_LIT>' . format ( <EOL> klass = checkbox_class , <EOL> content = html , <EOL> ) <EOL> return html <EOL> def make_input_group ( self , html ) : <EOL> if ( self . addon_before or self . addon_after ) and isinstance ( self . widget , ( TextInput , DateInput , Select ) ) : <EOL> before = '<STR_LIT>' . format ( <EOL> addon = self . addon_before ) if self . addon_before else '<STR_LIT>' <EOL> after = '<STR_LIT>' . format ( <EOL> addon = self . addon_after ) if self . addon_after else '<STR_LIT>' <EOL> html = '<STR_LIT>' . format ( <EOL> before = before , <EOL> after = after , <EOL> html = html <EOL> ) <EOL> return html <EOL> def append_to_field ( self , html ) : <EOL> help_text_and_errors = [ ] <EOL> if self . field_help : <EOL> help_text_and_errors . append ( self . field_help ) <EOL> help_text_and_errors += self . field_errors <EOL> if help_text_and_errors : <EOL> help_html = render_template_file ( <EOL> '<STR_LIT>' , <EOL> context = { <EOL> '<STR_LIT>' : self . field , <EOL> '<STR_LIT>' : help_text_and_errors , <EOL> '<STR_LIT>' : self . layout , <EOL> } <EOL> ) <EOL> html += '<STR_LIT>' . format ( help = help_html ) <EOL> return html <EOL> def get_field_class ( self ) : <EOL> field_class = self . field_class <EOL> if not field_class and self . layout == '<STR_LIT>' : <EOL> field_class = self . horizontal_field_class <EOL> return field_class <EOL> def wrap_field ( self , html ) : <EOL> field_class = self . get_field_class ( ) <EOL> if field_class : <EOL> html = '<STR_LIT>' . format ( <EOL> klass = field_class , html = html ) <EOL> return html <EOL> def get_label_class ( self ) : <EOL> label_class = self . label_class <EOL> if not label_class and self . layout == '<STR_LIT>' : <EOL> label_class = self . horizontal_label_class <EOL> label_class = text_value ( label_class ) <EOL> if not self . show_label : <EOL> label_class = add_css_class ( label_class , '<STR_LIT>' ) <EOL> return add_css_class ( label_class , '<STR_LIT>' ) <EOL> def get_label ( self ) : <EOL> if isinstance ( self . widget , CheckboxInput ) : <EOL> label = None <EOL> else : <EOL> label = self . field . label <EOL> if self . layout == '<STR_LIT>' and not label : <EOL> return mark_safe ( '<STR_LIT>' ) <EOL> return label <EOL> def add_label ( self , html ) : <EOL> label = self . get_label ( ) <EOL> if label : <EOL> html = render_label ( <EOL> label , <EOL> label_for = self . field . id_for_label , <EOL> label_class = self . get_label_class ( ) ) + html <EOL> return html <EOL> def get_form_group_class ( self ) : <EOL> form_group_class = self . form_group_class <EOL> if self . field . errors and self . error_css_class : <EOL> form_group_class = add_css_class ( <EOL> form_group_class , self . error_css_class ) <EOL> if self . field . field . required and self . required_css_class : <EOL> form_group_class = add_css_class ( <EOL> form_group_class , self . required_css_class ) <EOL> if self . field_errors : <EOL> form_group_class = add_css_class ( form_group_class , '<STR_LIT>' ) <EOL> elif self . field . form . is_bound : <EOL> form_group_class = add_css_class ( <EOL> form_group_class , self . success_css_class ) <EOL> if self . layout == '<STR_LIT>' : <EOL> form_group_class = add_css_class ( <EOL> form_group_class , self . get_size_class ( prefix = '<STR_LIT>' ) ) <EOL> return form_group_class <EOL> def wrap_label_and_field ( self , html ) : <EOL> return render_form_group ( html , self . get_form_group_class ( ) ) <EOL> def _render ( self ) : <EOL> if self . field . name in self . exclude . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) : <EOL> return '<STR_LIT>' <EOL> if self . field . is_hidden : <EOL> return text_value ( self . field ) <EOL> self . add_widget_attrs ( ) <EOL> html = self . field . as_widget ( attrs = self . widget . attrs ) <EOL> self . restore_widget_attrs ( ) <EOL> html = self . post_widget_render ( html ) <EOL> html = self . wrap_widget ( html ) <EOL> html = self . make_input_group ( html ) <EOL> html = self . append_to_field ( html ) <EOL> html = self . wrap_field ( html ) <EOL> html = self . add_label ( html ) <EOL> html = self . wrap_label_and_field ( html ) <EOL> return html <EOL> class InlineFieldRenderer ( FieldRenderer ) : <EOL> """<STR_LIT>""" <EOL> def add_error_attrs ( self ) : <EOL> field_title = self . widget . attrs . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> field_title += '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' . join ( <EOL> [ strip_tags ( e ) for e in self . field_errors ] ) <EOL> self . widget . attrs [ '<STR_LIT:title>' ] = field_title . strip ( ) <EOL> def add_widget_attrs ( self ) : <EOL> super ( InlineFieldRenderer , self ) . add_widget_attrs ( ) <EOL> self . add_error_attrs ( ) <EOL> def append_to_field ( self , html ) : <EOL> return html <EOL> def get_field_class ( self ) : <EOL> return self . field_class <EOL> def get_label_class ( self ) : <EOL> return add_css_class ( self . label_class , '<STR_LIT>' ) </s>
