Ejemplo n.º 1
0
    def attrs(self):
        '''
        Proxy to `.Column.attrs` but injects some values of our own.

        A ``th`` and ``td`` are guaranteed to be defined (irrespective of
        what's actually defined in the column attrs. This makes writing
        templates easier.
        '''
        # Start with table's attrs; Only 'th' and 'td' attributes will be used
        attrs = dict(self._table.attrs)

        # Update attrs to prefer column's attrs rather than table's
        attrs.update(dict(self.column.attrs))

        # we take the value for 'cell' as the basis for both the th and td attrs
        cell_attrs = attrs.get('cell', {})
        # override with attrs defined specifically for th and td respectively.
        kwargs = {'table': self._table}
        attrs['th'] = computed_values(attrs.get('th', cell_attrs), **kwargs)
        attrs['td'] = computed_values(attrs.get('td', cell_attrs), **kwargs)

        # wrap in AttributeDict
        attrs['th'] = AttributeDict(attrs['th'])
        attrs['td'] = AttributeDict(attrs['td'])

        # Override/add classes
        attrs['th']['class'] = self.get_th_class(attrs['th'])
        attrs['td']['class'] = self.get_td_class(attrs['td'])

        return attrs
Ejemplo n.º 2
0
    def attrs(self):
        '''
        Proxy to `.Column.attrs` but injects some values of our own.

        A ``th`` and ``td`` are guaranteed to be defined (irrespective of
        what's actually defined in the column attrs. This makes writing
        templates easier.
        '''
        # Start with table's attrs; Only 'th' and 'td' attributes will be used
        attrs = dict(self._table.attrs)

        # Update attrs to prefer column's attrs rather than table's
        attrs.update(dict(self.column.attrs))

        # we take the value for 'cell' as the basis for both the th and td attrs
        cell_attrs = attrs.get('cell', {})
        # override with attrs defined specifically for th and td respectively.
        kwargs = {
            'table': self._table
        }
        attrs['th'] = computed_values(attrs.get('th', cell_attrs), **kwargs)
        attrs['td'] = computed_values(attrs.get('td', cell_attrs), **kwargs)

        # wrap in AttributeDict
        attrs['th'] = AttributeDict(attrs['th'])
        attrs['td'] = AttributeDict(attrs['td'])

        # Override/add classes
        attrs['th']['class'] = self.get_th_class(attrs['th'])
        attrs['td']['class'] = self.get_td_class(attrs['td'])

        return attrs
Ejemplo n.º 3
0
    def attrs(self):
        '''
        Proxy to `.Column.attrs` but injects some values of our own.

        A ``th``, ``td`` and ``tf`` are guaranteed to be defined (irrespective
        of what's actually defined in the column attrs. This makes writing
        templates easier. ``tf`` is not actually a HTML tag, but this key name
        will be used for attributes for column's footer, if the column has one.
        '''

        # prepare kwargs for computed_values()
        kwargs = {
            'table': self._table,
            'bound_column': self,
        }
        # BoundRow.items() sets current_record and current_value when iterating over
        # the records in a table.
        if getattr(self, 'current_record', None) is not None and getattr(
                self, 'current_value', None) is not None:
            kwargs.update({
                'record': self.current_record,
                'value': self.current_value
            })

        # Start with table's attrs; Only 'th' and 'td' attributes will be used
        attrs = dict(self._table.attrs)

        # Update attrs to prefer column's attrs rather than table's
        attrs.update(dict(self.column.attrs))

        # we take the value for 'cell' as the basis for both the th and td attrs
        cell_attrs = attrs.get('cell', {})

        # override with attrs defined specifically for th and td respectively.
        attrs['th'] = computed_values(attrs.get('th', cell_attrs),
                                      kwargs=kwargs)
        attrs['td'] = computed_values(attrs.get('td', cell_attrs),
                                      kwargs=kwargs)
        attrs['tf'] = computed_values(attrs.get('tf', cell_attrs),
                                      kwargs=kwargs)

        # wrap in AttributeDict
        attrs['th'] = AttributeDict(attrs['th'])
        attrs['td'] = AttributeDict(attrs['td'])
        attrs['tf'] = AttributeDict(attrs['tf'])

        # Override/add classes
        attrs['th']['class'] = self.get_th_class(attrs['th'])
        attrs['td']['class'] = self.get_td_class(attrs['td'])
        attrs['tf']['class'] = self.get_td_class(attrs['tf'])

        return attrs
Ejemplo n.º 4
0
 def test_with_argument(self):
     x = computed_values({
         'foo': lambda y: {
             'bar': lambda y: 'baz-{}'.format(y)
         }
     }, kwargs=dict(y=2))
     self.assertEqual(x, {'foo': {'bar': 'baz-2'}})
Ejemplo n.º 5
0
 def test_with_argument(self):
     x = computed_values(
         {"foo": lambda y: {
             "bar": lambda y: "baz-{}".format(y)
         }},
         kwargs=dict(y=2))
     self.assertEqual(x, {"foo": {"bar": "baz-2"}})
Ejemplo n.º 6
0
def test_computed_values_with_argument():
    x = computed_values(
        {'foo': lambda y: {
            'bar': lambda y: 'baz-{}'.format(y)
        }},
        kwargs=dict(y=2))
    assert x == {'foo': {'bar': 'baz-2'}}
Ejemplo n.º 7
0
def test_computed_values_with_argument():
    x = computed_values({
        'foo': lambda y: {
            'bar': lambda y: 'baz-{}'.format(y)
        }
    }, y=2)
    assert x == {'foo': {'bar': 'baz-2'}}
Ejemplo n.º 8
0
    def attrs(self):
        """
        Proxy to `.Column.attrs` but injects some values of our own.

        A ``th``, ``td`` and ``tf`` are guaranteed to be defined (irrespective
        of what is actually defined in the column attrs. This makes writing
        templates easier. ``tf`` is not actually a HTML tag, but this key name
        will be used for attributes for column's footer, if the column has one.
        """

        # prepare kwargs for computed_values()
        kwargs = {"table": self._table, "bound_column": self}
        # BoundRow.items() sets current_record and current_value when iterating over
        # the records in a table.
        if (
            getattr(self, "current_record", None) is not None
            and getattr(self, "current_value", None) is not None
        ):
            kwargs.update({"record": self.current_record, "value": self.current_value})

        # Start with table's attrs; Only 'th' and 'td' attributes will be used
        attrs = dict(self._table.attrs)

        # Update attrs to prefer column's attrs rather than table's
        attrs.update(dict(self.column.attrs))

        # we take the value for 'cell' as the basis for both the th and td attrs
        cell_attrs = attrs.get("cell", {})

        # override with attrs defined specifically for th and td respectively.
        attrs["th"] = computed_values(attrs.get("th", cell_attrs), kwargs=kwargs)
        attrs["td"] = computed_values(attrs.get("td", cell_attrs), kwargs=kwargs)
        attrs["tf"] = computed_values(attrs.get("tf", cell_attrs), kwargs=kwargs)

        # wrap in AttributeDict
        attrs["th"] = AttributeDict(attrs["th"])
        attrs["td"] = AttributeDict(attrs["td"])
        attrs["tf"] = AttributeDict(attrs["tf"])

        # Override/add classes
        attrs["th"]["class"] = self.get_th_class(attrs["th"])
        attrs["td"]["class"] = self.get_td_class(attrs["td"])
        attrs["tf"]["class"] = self.get_td_class(attrs["tf"])

        return attrs
    def render(self, value, bound_column, record):
        default = {
            "type": "checkbox",
            "name": bound_column.name,
            "value": value
        }
        if self.is_checked(value, record):
            default.update({"checked": "checked"})

        general = self.attrs.get("input")
        specific = self.attrs.get("td__input")

        attrs = dict(default, **(specific or general or {}))
        attrs = computed_values(attrs,
                                kwargs={
                                    "record": record,
                                    "value": value
                                })
        return mark_safe("<input %s/>" % AttributeDict(attrs).as_html())
Ejemplo n.º 10
0
def test_computed_values_supports_nested_structures():
    x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
    assert x == {'foo': {'bar': 'baz'}}
Ejemplo n.º 11
0
def test_computed_values_supports_shallow_structures():
    x = computed_values({'foo': lambda: 'bar'})
    assert x == {'foo': 'bar'}
Ejemplo n.º 12
0
def test_compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
Ejemplo n.º 13
0
def test_compute_values_supports_nested_structures():
    x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
    assert x == {'foo': {'bar': 'baz'}}
Ejemplo n.º 14
0
 def test_supports_shallow_structures(self):
     x = computed_values({"foo": lambda: "bar"})
     self.assertEqual(x, {"foo": "bar"})
Ejemplo n.º 15
0
def test_compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: "bar"})
    assert x == {"foo": "bar"}
Ejemplo n.º 16
0
 def test_supports_nested_structures(self):
     x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
     self.assertEqual(x, {'foo': {'bar': 'baz'}})
Ejemplo n.º 17
0
 def test_supports_shallow_structures(self):
     x = computed_values({'foo': lambda: 'bar'})
     self.assertEqual(x, {'foo': 'bar'})
Ejemplo n.º 18
0
def test_computed_values_returns_None_if_not_enough_kwargs():
    x = computed_values({'foo': lambda x: 'bar'})
    assert x == {'foo': None}
Ejemplo n.º 19
0
 def test_returns_None_if_not_enough_kwargs(self):
     x = computed_values({'foo': lambda x: 'bar'})
     self.assertEqual(x, {'foo': None})
Ejemplo n.º 20
0
def test_compute_values_supports_nested_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
Ejemplo n.º 21
0
def compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: "bar"})
    assert x == {"foo": "bar"}
Ejemplo n.º 22
0
 def test_supports_nested_structures(self):
     x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
     self.assertEqual(x, {"foo": {"bar": "baz"}})
Ejemplo n.º 23
0
def compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
Ejemplo n.º 24
0
 def test_returns_None_if_not_enough_kwargs(self):
     x = computed_values({"foo": lambda x: "bar"})
     self.assertEqual(x, {"foo": None})
Ejemplo n.º 25
0
def test_compute_values_supports_shallow_structures():
    x = computed_values({'foo': lambda: 'bar'})
    assert x == {'foo': 'bar'}