def __init__(self, field, layout='', form_group_class=FORM_GROUP_CLASS, field_class=None, label_class=None, show_label=True, show_help=True, exclude='', set_required=True, addon_before=None, addon_after=None): # Only allow BoundField if not isinstance(field, BoundField): raise BootstrapError('Parameter "field" should contain a valid Django BoundField.') self.field = field self.layout = layout self.form_group_class = form_group_class self.field_class = field_class self.label_class = label_class self.show_label = show_label self.exclude = exclude self.set_required = set_required self.widget = field.field.widget self.initial_attrs = self.widget.attrs.copy() self.field_help = text_value(mark_safe(field.help_text)) if show_help and field.help_text else '' self.field_errors = [conditional_escape(text_value(error)) for error in field.errors] self.placeholder = field.label self.form_error_class = getattr(field.form, 'error_css_class', '') self.form_required_class = getattr(field.form, 'required_css_class', '') self.addon_before = addon_before self.addon_after = addon_after
def render_label(content, label_for=None, label_class=None, label_title='', optional=False): """ Render a label with content """ attrs = {} if label_for: attrs['for'] = label_for if label_class: attrs['class'] = label_class if label_title: attrs['title'] = label_title if text_value(content) == ' ': # Empty label, e.g. checkbox attrs.setdefault('class', '') attrs['class'] += ' label-empty' builder = '<{tag}{attrs}>{content}{opt}</{tag}>' return format_html( builder, tag='label', attrs=mark_safe(flatatt(attrs)) if attrs else '', opt=mark_safe('<br><span class="optional">{}</span>'.format( pgettext('form', 'Optional'))) if optional else '', content=text_value(content), )
def __init__(self, field, layout='', form_group_class=FORM_GROUP_CLASS, field_class=None, label_class=None, show_label=True, show_help=True, exclude='', set_required=True, addon_before=None, addon_after=None, error_css_class='', required_css_class=''): # Only allow BoundField if not isinstance(field, BoundField): raise BootstrapError( 'Parameter "field" should contain a valid Django BoundField.') self.field = field self.layout = layout self.form_group_class = form_group_class self.field_class = field_class self.label_class = label_class self.show_label = show_label self.exclude = exclude self.set_required = set_required self.widget = field.field.widget self.initial_attrs = self.widget.attrs.copy() self.field_help = text_value(mark_safe( field.help_text)) if show_help and field.help_text else '' self.field_errors = [ conditional_escape(text_value(error)) for error in field.errors ] self.placeholder = field.label self.addon_before = addon_before self.addon_after = addon_after # These are set in Django or in the global BOOTSTRAP3 settings, and they can be overwritten in the template if error_css_class: self.form_error_class = error_css_class else: self.form_error_class = getattr( field.form, 'error_css_class', get_bootstrap_setting('error_css_class')) if required_css_class: self.form_required_class = required_css_class else: self.form_required_class = getattr( field.form, 'required_css_class', get_bootstrap_setting('required_css_class'))
def render_label(content, label_for=None, label_class=None, label_title='', label_id='', optional=False, is_valid=None, attrs=None): """ Render a label with content """ attrs = attrs or {} if label_for: attrs['for'] = label_for if label_class: attrs['class'] = label_class if label_title: attrs['title'] = label_title if label_id: attrs['id'] = label_id opt = "" if is_valid is not None: if is_valid: validation_text = pgettext('form', 'is valid') else: validation_text = pgettext('form', 'has errors') opt += '<strong class="sr-only"> {}</strong>'.format(validation_text) if text_value(content) == ' ': # Empty label, e.g. checkbox attrs.setdefault('class', '') attrs['class'] += ' label-empty' # usually checkboxes have overall empty labels and special labels per checkbox # => remove for-attribute as well as "required"-text appended to label if 'for' in attrs: del (attrs['for']) else: opt += '<i class="sr-only label-required">, {}</i>'.format( pgettext('form', 'required')) if not optional else '' builder = '<{tag}{attrs}>{content}{opt}</{tag}>' return format_html( builder, tag='label', attrs=mark_safe(flatatt(attrs)) if attrs else '', opt=mark_safe(opt), content=text_value(content), )
def render_alert(content, alert_type=None, dismissable=True): """ Render a Bootstrap alert """ button = '' if not alert_type: alert_type = 'info' css_classes = ['alert', 'alert-' + text_value(alert_type)] if dismissable: css_classes.append('alert-dismissable') button = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' return '<div class="{css_classes}">{button}{content}</div>'.format( css_classes=' '.join(css_classes), button=button, content=text_value(content), )
def get_label_class(self): label_class = self.label_class if not label_class and self.layout == 'horizontal': label_class = get_bootstrap_setting('horizontal_label_class') label_class = text_value(label_class) if not self.show_label: label_class = add_css_class(label_class, 'sr-only') return add_css_class(label_class, 'control-label')
def render(self): # Hidden input requires no special treatment if self.field.is_hidden: return text_value(self.field) # Render the widget self.add_widget_attrs() html = self.field.as_widget(attrs=self.widget.attrs) return html
def get_label_class(self): label_class = self.label_class if not label_class and self.layout == "horizontal": label_class = get_bootstrap_setting("horizontal_label_class") label_class = text_value(label_class) if not self.show_label: label_class = add_css_class(label_class, "sr-only") return add_css_class(label_class, "control-label")
def render_label(content, label_for=None, label_class=None, label_title='', optional=False): """ Render a label with content """ attrs = {} if label_for: attrs['for'] = label_for if label_class: attrs['class'] = label_class if label_title: attrs['title'] = label_title if text_value(content) == ' ': # Empty label, e.g. checkbox attrs.setdefault('class', '') attrs['class'] += ' label-empty' # usually checkboxes have overall empty labels and special labels per checkbox # => remove for-attribute as well as "required"-text appended to label del (attrs['for']) opt = "" else: opt = mark_safe('<i class="sr-only"> {}</i>'.format( pgettext('form', 'required'))) if not optional else '' builder = '<{tag}{attrs}>{content}{opt}</{tag}>' return format_html( builder, tag='label', attrs=mark_safe(flatatt(attrs)) if attrs else '', opt=opt, content=text_value(content), )
def render(self): # See if we're not excluded if self.field.name in self.exclude.replace(' ', '').split(','): return '' # Hidden input requires no special treatment if self.field.is_hidden: return text_value(self.field) self.add_widget_attrs() html = self.field.as_widget(attrs=self.widget.attrs) self.restore_widget_attrs() html = self.post_widget_render(html) html = self.wrap_widget(html) html = self.make_input_group(html) html = self.append_to_field(html) html = self.wrap_field(html) html = self.add_label(html) html = self.wrap_label_and_field(html) return html
def render_label(content, label_for=None, label_class=None, label_title='', optional=False): """ Render a label with content """ attrs = {} if label_for: attrs['for'] = label_for if label_class: attrs['class'] = label_class if label_title: attrs['title'] = label_title builder = '<{tag}{attrs}>{content}{opt}</{tag}>' return format_html( builder, tag='label', attrs=mark_safe(flatatt(attrs)) if attrs else '', opt=mark_safe('<br><span class="optional">{}</span>'.format(pgettext('form', 'Optional'))) if optional else '', content=text_value(content), )
def _render(self): """Overrides for root renderer. Adds helptext (append_to_field) later than usual, and adds class 'select' when rendering a select box. """ # See if we're not excluded if self.field.name in self.exclude.replace(' ', '').split(','): return '' # Hidden input requires no special treatment if self.field.is_hidden: return text_value(self.field) # Render the widget self.add_widget_attrs() if (isinstance(self.widget, Select)): new_classes = add_css_class(self.widget.attrs['class'], 'select') self.widget.attrs['class'] = new_classes if isinstance(self.widget, Textarea): self.widget.attrs['autocomplete'] = 'nope' # self.widget.attrs['autocorrect'] = 'off' # self.widget.attrs['autocapitalize'] = 'off' # self.widget.attrs['spellcheck'] = 'false' self.widget.attrs['rows'] = 6 html = self.field.as_widget(attrs=self.widget.attrs) self.restore_widget_attrs() # Start post render html = self.post_widget_render(html) html = self.wrap_widget(html) html = self.make_input_group(html) html = self.wrap_field(html) html = self.add_label(html) html = self.append_to_field(html) html = self.wrap_label_and_field(html) return html
def test_text_value(self): self.assertEqual(text_value(''), "") self.assertEqual(text_value(' '), " ") self.assertEqual(text_value(None), "") self.assertEqual(text_value(1), "1")