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
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
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
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'}})
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"}})
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'}}
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'}}
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())
def test_computed_values_supports_nested_structures(): x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}}) assert x == {'foo': {'bar': 'baz'}}
def test_computed_values_supports_shallow_structures(): x = computed_values({'foo': lambda: 'bar'}) assert x == {'foo': 'bar'}
def test_compute_values_supports_shallow_structures(): x = computed_values({"foo": lambda: {"bar": lambda: "baz"}}) assert x == {"foo": {"bar": "baz"}}
def test_compute_values_supports_nested_structures(): x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}}) assert x == {'foo': {'bar': 'baz'}}
def test_supports_shallow_structures(self): x = computed_values({"foo": lambda: "bar"}) self.assertEqual(x, {"foo": "bar"})
def test_compute_values_supports_shallow_structures(): x = computed_values({"foo": lambda: "bar"}) assert x == {"foo": "bar"}
def test_supports_nested_structures(self): x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}}) self.assertEqual(x, {'foo': {'bar': 'baz'}})
def test_supports_shallow_structures(self): x = computed_values({'foo': lambda: 'bar'}) self.assertEqual(x, {'foo': 'bar'})
def test_computed_values_returns_None_if_not_enough_kwargs(): x = computed_values({'foo': lambda x: 'bar'}) assert x == {'foo': None}
def test_returns_None_if_not_enough_kwargs(self): x = computed_values({'foo': lambda x: 'bar'}) self.assertEqual(x, {'foo': None})
def test_compute_values_supports_nested_structures(): x = computed_values({"foo": lambda: {"bar": lambda: "baz"}}) assert x == {"foo": {"bar": "baz"}}
def compute_values_supports_shallow_structures(): x = computed_values({"foo": lambda: "bar"}) assert x == {"foo": "bar"}
def test_supports_nested_structures(self): x = computed_values({"foo": lambda: {"bar": lambda: "baz"}}) self.assertEqual(x, {"foo": {"bar": "baz"}})
def compute_values_supports_shallow_structures(): x = computed_values({"foo": lambda: {"bar": lambda: "baz"}}) assert x == {"foo": {"bar": "baz"}}
def test_returns_None_if_not_enough_kwargs(self): x = computed_values({"foo": lambda x: "bar"}) self.assertEqual(x, {"foo": None})
def test_compute_values_supports_shallow_structures(): x = computed_values({'foo': lambda: 'bar'}) assert x == {'foo': 'bar'}