Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
    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_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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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))
Ejemplo n.º 7
0
    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))
Ejemplo n.º 8
0
    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))
Ejemplo n.º 9
0
    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))
Ejemplo n.º 10
0
    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))
Ejemplo n.º 11
0
 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))
Ejemplo n.º 12
0
 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))
Ejemplo n.º 13
0
    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))
Ejemplo n.º 14
0
    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))
Ejemplo n.º 15
0
    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))
Ejemplo n.º 16
0
    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">&nbsp;</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)