def test_render_check_exception(self): """ Calling check_test() shouldn't swallow exceptions (#17888). """ widget = CheckboxInput( check_test=lambda value: value.startswith('hello'), ) with self.assertRaises(AttributeError): widget.render('greeting', True)
def render(self, name, value, attrs=None, choices=(), new_maps=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # Normalize to strings str_values = [force_text(v) for v in value] vals = [] last_uimap = 0 for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = ' for="%s"' % conditional_escape(final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_text(option_label)) item = {'label_for': label_for, 'rendered_cb': rendered_cb, 'option_label': option_label, 'option_value': option_value} vals.append(item) last_uimap += 1 for i, newmap in enumerate(new_maps): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i + last_uimap)) label_for = ' for="%s"' % conditional_escape(final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda l: False) option_value = 't' + str(newmap.id) rendered_cb = cb.render(name, option_value) option_label = newmap.__unicode__() option_label = conditional_escape(force_text(option_label)) item = {'label_for': label_for, 'rendered_cb': rendered_cb, 'option_label': option_label, 'option_value': option_value} vals.append(item) html = render_to_string( 'sortedm2m/sorted_checkbox_select_multiple_widget.html', {'vals': vals, }) return mark_safe(html)
def render_body(self, name, value, attrs): output = ['<tbody>'] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs) final_attrs['class'] = "tableselectmultiple selectable-checkbox" if self.bootstrap_style: final_attrs['class'] += " form-check-input" str_values = set([force_text(v) for v in value]) choice_pks = [pk for (pk, item) in self.choices] choices = self.choices.queryset.filter(pk__in=choice_pks) for i, item in enumerate(choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict( final_attrs, id='{}_{}'.format(attrs['id'], i), ) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(item.pk) rendered_cb = cb.render(name, option_value) output.append('<tr><td>{}</td>'.format(rendered_cb)) for item_attr in self.item_attrs: attr = item_attr \ if isinstance(item_attr, str) \ else item_attr[0] content = get_underscore_attrs(attr, item) output.append('<td>{}</td>'.format(escape(content))) output.append('</tr>') output.append('</tbody>') return ''.join(output)
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] # Normalize to strings. str_values = set([force_unicode(v) for v in value]) for i, (option_value, item) in enumerate(self.choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) output.append(u'<tr><td class="row-select">%s</td>' % rendered_cb) for attr in self.item_attrs: if callable(attr): content = attr(item) elif hasattr(item, attr): if callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) else: content = item[attr] if not isinstance(content, SafeText): content = escape(content) output.append(u'<td>%s</td>' % content) output.append(u'</tr>') return mark_safe(u'\n'.join(output))
def render_body(self, name, value, attrs): output = [] has_id = attrs and "id" in attrs final_attrs = self.build_attrs(attrs, name=name) str_values = set([force_text(v) for v in value]) for i, (pk, item) in enumerate(self.choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id="{}_{}".format(attrs["id"], i)) item = self.choices.queryset.get(pk=pk) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(pk) rendered_cb = cb.render(name, option_value) output.append("<tr><td>{}</td>".format(rendered_cb)) for attr in self.item_attrs: if callable(attr): content = attr(item) elif callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) output.append("<td>{}</td>".format(escape(content))) output.append("</tr>") output.append("</tbody>") return "".join(output)
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<table class="existing_objects_list">'] str_values = set([force_unicode(v) for v in value]) # Normalize to strings. output.append(u'<tr><th>%s</th>' % conditional_escape(self.checkbox_label)) for label, attr in self.item_attrs: output.append(u'<th>%s</th>' % conditional_escape(label)) output.append(u'</tr>') for i, (option_value, item) in enumerate(self.choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) output.append(u'<tr><td>%s</td>' % rendered_cb) for label, attr in self.item_attrs: if callable(attr): content = attr(item) elif callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) output.append(u'<td>%s</td>' % conditional_escape(content)) output.append(u'</tr>') output.append(u'</table>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) id_ = final_attrs.get('id', None) output = [u'<ul>'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if id_: final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) label_for = format_html(u' for="{0}_{1}"', id_, i) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = format_html(option_label) output.append( format_html(u'<li><label{0}>{1} {2}</label></li>', label_for, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) id_ = final_attrs.get('id', None) output = [u'<ul>'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if id_: final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) label_for = format_html(u' for="{0}_{1}"', id_, i) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = format_html(option_label) output.append(format_html(u'<li><label{0}>{1} {2}</label></li>', label_for, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, choices=(), renderer=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) output = [u'<ul>'] global_readonly = 'readonly' in final_attrs str_values = set([v for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): if not global_readonly and 'readonly' in final_attrs: # If the entire group is readonly keep all options readonly del final_attrs['readonly'] if isinstance(option_label, dict): if dict.get(option_label, 'readonly'): final_attrs = dict(final_attrs, readonly='readonly') option_label = option_label['label'] final_attrs = dict(final_attrs, id='{}_{}'.format(attrs['id'], i)) label_for = u' for="{}"'.format(final_attrs['id']) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb = cb.render(name, option_value) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def get_selected_and_unselected(self, name, value, attrs): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, extra_attrs={'name': name}) # Normalize to strings str_values = [force_text(v) for v in value] selected = [] unselected = [] for i, (option_value, option_label) in enumerate(chain(self.choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = ' for="%s"' % conditional_escape(final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_text(option_label)) try: edit_link = reverse( f'admin:{self.model_cls._meta.app_label}_{self.model_cls._meta.model_name}_change', args=[option_value]) except NoReverseMatch: edit_link = None item = { 'label_for': label_for, 'rendered_cb': rendered_cb, 'option_label': option_label, 'option_value': option_value, 'edit_link': edit_link } if option_value in str_values: selected.append(item) else: unselected.append(item) # Reorder `selected` array according str_values which is a set of `option_value`s in the # order they should be shown on screen ordered = [] for s in str_values: for select in selected: if s == select['option_value']: ordered.append(select) selected = ordered return selected, unselected
def render_body(self, name, value, attrs): output = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) str_values = set([force_text(v) for v in value]) for i, (pk, item) in enumerate(self.choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='{}_{}'.format(attrs['id'], i)) item = self.choices.queryset.get(pk=pk) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(pk) rendered_cb = cb.render(name, option_value) # Custom checkbox wrapper for Shopify Styling. pre_rendered_cb = \ """<tr> <td> <label class="Polaris-Choice Polaris-Choice--labelHidden" for="{0}"> <div class="Polaris-Choice__Control"> <div class="Polaris-Checkbox"> {1} <div class="Polaris-Checkbox__Backdrop"></div> <div class="Polaris-Checkbox__Icon"> <span class="Polaris-Icon"> <svg class="Polaris-Icon__Svg" viewBox="0 0 20 20"> <g fill-rule="evenodd"> <path d="M8.315 13.859l-3.182-3.417a.506.506 0 0 1 0-.684l.643-.683a.437.437 0 0 1 .642 0l2.22 2.393 4.942-5.327a.437.437 0 0 1 .643 0l.643.684a.504.504 0 0 1 0 .683l-5.91 6.35a.437.437 0 0 1-.642 0"></path><path d="M8.315 13.859l-3.182-3.417a.506.506 0 0 1 0-.684l.643-.683a.437.437 0 0 1 .642 0l2.22 2.393 4.942-5.327a.437.437 0 0 1 .643 0l.643.684a.504.504 0 0 1 0 .683l-5.91 6.35a.437.437 0 0 1-.642 0"></path> </g> </svg> </span> </div> </div> </div> <div class="Polaris-Choice__Label"></div> </label> </td>""".format('{}_{}'.format(attrs['id'], i), rendered_cb) pre_rendered_cb = pre_rendered_cb.replace('class="tableselectmultiple"','class="Polaris-Checkbox__Input"') output.append(pre_rendered_cb) for attr in self.item_attrs: if callable(attr): content = attr(item) elif callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) output.append('<td>{}</td>'.format(escape(content))) output.append('</tr>') output.append('</tbody>') return ''.join(output)
def render(self, name, value, attrs=None, choices=()): cs = [] for c in self.choices: c = list(c) c.append(True) cs.append(c) dps = Daypart.objects.active() cs = [] t_dp_day = None for dp in dps: row_title = False if not t_dp_day == dp.get_day_display(): row_title = dp.get_day_display() t_dp_day = dp.get_day_display() c = [ dp.pk, '%02d - %02d' % (dp.time_start.hour, dp.time_end.hour), row_title ] cs.append(c) self.choices = cs if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul class="unstyled" style="float: left;">'] str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label, row_title) in enumerate(chain(self.choices, choices)): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) if row_title: output.append( u'</ul><ul class="unstyled" style="float: left;"><li class="title">%s</li>' % row_title) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) no = 0 previous_node_depth = 0 walk_list = getattr(self, "walk_list", []) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' if walk_list and len(walk_list) > no: # special case is special if walk_list[no].tree_path == "": node_depth = 0 else: node_depth = len(walk_list[no].tree_path.split("/")) else: node_depth = 0 if node_depth < previous_node_depth: output.append((u'</ul>'*(previous_node_depth-node_depth))) if node_depth > previous_node_depth: output.append(u'<ul>') cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) previous_node_depth = node_depth no += 1 # It's tree baby! There is no root at the end if 0 < previous_node_depth: output.append((u'</ul>'*(previous_node_depth))) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render_checkbox(self, output, has_id, final_attrs, attrs, i, str_values, option_value, name, option_label): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [_table_header % {"name": attrs["id"]}] # Normalize to strings str_values = set([force_text(v) for v in value]) extra_class = "check_%s" % attrs["id"] if "class" in final_attrs: final_attrs["class"] = "%s %s" % (final_attrs["class"], extra_class) else: final_attrs["class"] = extra_class for i, (option_value, option_object) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) distance = "0 m" description = "?" if len(option_object.runs.all()): distances = list(set([run.distance for run in option_object.runs.all()])) distance = ", ".join(["%d m" % d for d in distances]) description = "; ".join(run.name for run in option_object.runs.all()) distance = force_text(distance) description = smart_text(description) tr_id = "select_line_%s" % final_attrs["id"] output.append( format_html( u'<tr id="{5}" class="entry"><td>{0}</td><td>{1}</td><td><label{2}>{3}</label></td><td>{4}</td></tr>', rendered_cb, force_text(option_object.start_time), label_for, distance, description, tr_id)) output.append(format_html( u'<tr class="lastline"><td colspan="4" class="alert alert-success">bitte erst oben ausfüllen</td></tr>')) output.append(_table_footer) output.append(_check_js % {"name": attrs["id"], "class": extra_class}) return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, choices=()): from itertools import chain import math from django.forms import CheckboxInput from django.utils.encoding import force_unicode from django.utils.html import conditional_escape #print 'render()' if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # Normalize to strings str_values = set([force_unicode(v) for v in value]) choices_per_column = int(math.ceil(len(list(self.choices)) / float(self.columns))) output_items = [] for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output_items.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output = [] for column in range(self.columns): output.append(u'<ul class="checkbox-select-multiple-columns ' u'checkbox-select-multiple-columns-%d">' % self.columns) output.extend(output_items[:choices_per_column]) output.append(u'</ul>') output_items = output_items[choices_per_column:] return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): cs = [] for c in self.choices: c = list(c) c.append(True) cs.append(c) dps = Daypart.objects.active() cs = [] t_dp_day = None for dp in dps: row_title = False if not t_dp_day == dp.get_day_display(): row_title = dp.get_day_display() t_dp_day = dp.get_day_display() c = [dp.pk, '%02d - %02d' % (dp.time_start.hour, dp.time_end.hour), row_title] cs.append(c) self.choices = cs if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul class="unstyled" style="float: left;">'] str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label, row_title) in enumerate(chain(self.choices, choices)): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) if row_title: output.append(u'</ul><ul class="unstyled" style="float: left;"><li class="title">%s</li>' % row_title) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u''] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) output.append(u'<td>%s</td>' % rendered_cb) output.append(u'') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] # Normalize to strings str_values = set([force_unicode(v) for v in value]) # where to drop in the <ul> breaks every = ceil((len(choices) + len(self.choices)) / float(self.columns)) output.append(u'<ul>') for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) # option_label = conditional_escape(force_unicode(option_label)) option_label = force_unicode(option_label) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) if (i % every) == (every - 1): output.append(u'</ul><ul>') output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] # Normalize to strings. str_values = set([force_unicode(v) for v in value]) for i, (option_value, item) in enumerate(self.choices): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) output.append(u'<tr>') output.append(u'<td class="row-select">%s</td>' % rendered_cb) for attr in self.item_attrs: css_name = attr if callable(attr): content = attr(item) css_name = attr.__name__.strip("_") elif hasattr(item, attr): if callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) else: content = item[attr] if not isinstance(content, SafeText): content = escape(content) css = ( ' class="field-%s"' % css_name.lower().replace("_", "-").replace(" ", "-")) output.append(u'<td%s>%s</td>' % (css, content)) output.append(u'</tr>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] str_values = set([force_text(v) for v in value]) # Normalize to strings. for i, (option_value, item) in enumerate(self.choices): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) output.append(u'<tr><td>%s</td>' % rendered_cb) for attr in self.item_attrs: if callable(attr): content = attr(item) elif callable(getattr(item, attr)): content = getattr(item, attr)() else: content = getattr(item, attr) output.append(u'<td>%s</td>' % escape(content)) output.append(u'</tr>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<div class="check-holder"><div class="check-row">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<label%s>%s<span class="label-text">%s</span></label>' % (label_for, rendered_cb, option_label)) output.append(u'</div></div>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = ['<div>'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = format_html(' for="{0}" class="checkbox-inline"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html('<label{0}>{1} <span>{2}</span></label>', label_for, rendered_cb, str(option_label))) output.append('</div>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, renderer=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs attrs = attrs.copy() attrs['name'] = name # set up output attributes html = u'' table_rows = u'' # Normalize to strings str_values = set([force_text(v) for v in value]) # setup the id attr if has_id: id = attrs['id'] else: id = u'' # set up the html for output html += """ <table class="table table-striped" id="%(id)s"> <tr> <th class="header-col-1"> </th> <th class="header-col-2">View</th> <th class="header-col-3">Change</th> </tr> %(table_rows)s </table> """ for i, (group_name, group) in enumerate(groupby(self.choices, lambda x: x[1])): view_input_value = force_text(next(group)[0]) change_input_value = force_text(next(group)[0]) if has_id: final_attrs = dict(attrs, id='%s_%s' % (attrs['id'], i)) cb_view = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) cb_change = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) rendered_cb_view = cb_view.render(name, view_input_value) rendered_cb_change = cb_change.render(name, change_input_value) if (i % 2) == 0: tr_class = ' class="alt" ' else: tr_class = '' table_rows += """ <tr%(tr_class)s> <td>%(group_name)s</td> <td>%(view_checkbox)s</td> <td>%(change_checkbox)s</td> </tr> """ % { 'tr_class': tr_class, 'group_name': conditional_escape(force_text(group_name)), 'view_checkbox': rendered_cb_view, 'change_checkbox': rendered_cb_change } html = html % {'id': id, 'table_rows': table_rows} return mark_safe(html)
def render(self, name, value, attrs=None, choices=(), new_maps=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # Normalize to strings str_values = [force_text(v) for v in value] vals = [] last_uimap = 0 for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = ' for="%s"' % conditional_escape(final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_text(option_label)) item = { 'label_for': label_for, 'rendered_cb': rendered_cb, 'option_label': option_label, 'option_value': option_value } vals.append(item) last_uimap += 1 for i, newmap in enumerate(new_maps): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i + last_uimap)) label_for = ' for="%s"' % conditional_escape(final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda l: False) option_value = 't' + str(newmap.id) rendered_cb = cb.render(name, option_value) option_label = newmap.__unicode__() option_label = conditional_escape(force_text(option_label)) item = { 'label_for': label_for, 'rendered_cb': rendered_cb, 'option_label': option_label, 'option_value': option_value } vals.append(item) html = render_to_string( 'sortedm2m/sorted_checkbox_select_multiple_widget.html', { 'vals': vals, }) return mark_safe(html)
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and "id" in attrs final_attrs = self.build_attrs(attrs, name=name) # set up output attributes html = u"" table_rows = u"" # Normalize to strings str_values = set([force_unicode(v) for v in value]) # setup the id attr if has_id: id = attrs["id"] else: id = u"" # set up the html for output html += """ <table border="0" cellspacing="0" cellpadding="0" id="%(id)s"> <tr> <th class="header-col-1"> </th> <th class="header-col-2">View</th> <th class="header-col-3">Change</th> </tr> %(table_rows)s </table> """ for i, (group_name, group) in enumerate(groupby(self.choices, lambda x: x[1])): view_input_value = force_unicode(group.next()[0]) change_input_value = force_unicode(group.next()[0]) if has_id: final_attrs = dict(final_attrs, id="%s_%s" % (attrs["id"], i)) cb_view = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) cb_change = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb_view = cb_view.render(name, view_input_value) rendered_cb_change = cb_change.render(name, change_input_value) if (i % 2) == 0: tr_class = ' class="alt" ' else: tr_class = "" table_rows += """ <tr%(tr_class)s> <td>%(group_name)s</td> <td>%(view_checkbox)s</td> <td>%(change_checkbox)s</td> </tr> """ % { "tr_class": tr_class, "group_name": conditional_escape(force_unicode(group_name)), "view_checkbox": rendered_cb_view, "change_checkbox": rendered_cb_change, } html = html % {"id": id, "table_rows": table_rows} return mark_safe(html)
def render(self, name, value, attrs=None, choices=()): """ renders the widget. """ def update_dictionary( d, item_label, checkbox, label_for, motherlist = [] ): b = d # item_label = item_label.strip() for mother in motherlist: mother = mother.strip() if mother in b.keys(): b = b[mother]['children'] else: m = { 'label': u'', 'checkbox': u'', 'label_for': u'', 'children': {} } b[mother] = m b = m['children'] if item_label in b.keys(): b[item_label]['label'] = conditional_escape(force_unicode( item_label )) b[item_label]['checkbox'] = checkbox b[item_label]['label_for'] = label_for else: i = { 'label': conditional_escape(force_unicode( item_label )) , 'checkbox': checkbox, 'label_for': label_for, 'children': {} } b[item_label] = i return d def write_output_old( out, item, depth = 0 ): if depth: out.append(u'<li>%s <label%s>%s %s</label></li>' % ( '.' * depth * 3, item['label_for'], item['checkbox'], item['label']) ) else: out.append(u'<li><label%s>%s %s</label></li>' % ( item['label_for'], item['checkbox'], item['label']) ) for i in item['children'].keys(): write_output_old( out, item['children'][i], depth+1) def write_output( out, item, depth = 0 ): out.append(u'<tr><td style="padding: 5px 0 5px %spx;">%s %s</label></td></tr>' % ( str(5 + depth * 20), item['checkbox'], item['label']) ) for i in item['children'].keys(): write_output( out, item['children'][i], depth+1) if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # output = [u'<ul>'] output = ['<table><tbody>'] output_inner = {} # output_debug = "DEBUG: " # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = force_unicode(option_label) temp = option_label.split( self.split_char ) mothers = [] if len(temp) > 1: mothers = temp[:-1] actual_name = temp[-1].strip() # now we search the node we insert this checkbox. # entry = { 'checkbox': cb, 'label': escaped..., children: {} } update_dictionary( output_inner, actual_name, rendered_cb, label_for, mothers ) for i in output_inner.keys(): write_output(output, output_inner[i], 0) output.append(u'</tbody></table>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # set up output attributes html = u'' table_rows = u'' # Normalize to strings str_values = set([force_unicode(v) for v in value]) # setup the id attr if has_id: id = attrs['id'] else: id = u'' # set up the html for output html += """ <table border="0" cellspacing="0" cellpadding="0" id="%(id)s"> <tr> <th class="header-col-1"> </th> <th class="header-col-2">View</th> <th class="header-col-3">Change</th> </tr> %(table_rows)s </table> """ for i, (member_label, member_perm) in enumerate(groupby(self.choices, lambda x: x[1])): view_input_value = force_unicode(member_perm.next()[0]) change_input_value = force_unicode(member_perm.next()[0]) if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) cb_view = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) cb_change = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb_view = cb_view.render(name, view_input_value) rendered_cb_change = cb_change.render(name, change_input_value) if (i % 2) == 0: tr_class = ' class="alt" ' else: tr_class = '' table_rows += """ <tr%(tr_class)s> <td>%(member_label)s</td> <td>%(view_checkbox)s</td> <td>%(change_checkbox)s</td> </tr> """ % {'tr_class': tr_class, 'member_label': conditional_escape(force_unicode(member_label)), 'view_checkbox': rendered_cb_view, 'change_checkbox': rendered_cb_change } html = html % { 'id': id, 'table_rows': table_rows } return mark_safe(html)