コード例 #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
コード例 #2
0
ファイル: base.py プロジェクト: dyve/django-tables2
    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
コード例 #3
0
ファイル: base.py プロジェクト: amit988684/django-tables2
    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
コード例 #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'}})
コード例 #5
0
ファイル: test_utils.py プロジェクト: zqian/django-tables2
 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"}})
コード例 #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'}}
コード例 #7
0
ファイル: test_utils.py プロジェクト: Barolina/django-tables2
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'}}
コード例 #8
0
ファイル: base.py プロジェクト: bradleyayers/django-tables2
    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
コード例 #9
0
    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())
コード例 #10
0
def test_computed_values_supports_nested_structures():
    x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
    assert x == {'foo': {'bar': 'baz'}}
コード例 #11
0
def test_computed_values_supports_shallow_structures():
    x = computed_values({'foo': lambda: 'bar'})
    assert x == {'foo': 'bar'}
コード例 #12
0
def test_compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
コード例 #13
0
ファイル: test_utils.py プロジェクト: Anlim/django-tables2
def test_compute_values_supports_nested_structures():
    x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
    assert x == {'foo': {'bar': 'baz'}}
コード例 #14
0
ファイル: test_utils.py プロジェクト: zqian/django-tables2
 def test_supports_shallow_structures(self):
     x = computed_values({"foo": lambda: "bar"})
     self.assertEqual(x, {"foo": "bar"})
コード例 #15
0
ファイル: test_utils.py プロジェクト: brianmay/django-tables2
def test_compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: "bar"})
    assert x == {"foo": "bar"}
コード例 #16
0
 def test_supports_nested_structures(self):
     x = computed_values({'foo': lambda: {'bar': lambda: 'baz'}})
     self.assertEqual(x, {'foo': {'bar': 'baz'}})
コード例 #17
0
 def test_supports_shallow_structures(self):
     x = computed_values({'foo': lambda: 'bar'})
     self.assertEqual(x, {'foo': 'bar'})
コード例 #18
0
def test_computed_values_returns_None_if_not_enough_kwargs():
    x = computed_values({'foo': lambda x: 'bar'})
    assert x == {'foo': None}
コード例 #19
0
 def test_returns_None_if_not_enough_kwargs(self):
     x = computed_values({'foo': lambda x: 'bar'})
     self.assertEqual(x, {'foo': None})
コード例 #20
0
ファイル: test_utils.py プロジェクト: brianmay/django-tables2
def test_compute_values_supports_nested_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
コード例 #21
0
ファイル: utils.py プロジェクト: tdruez/django-tables2
def compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: "bar"})
    assert x == {"foo": "bar"}
コード例 #22
0
ファイル: test_utils.py プロジェクト: zqian/django-tables2
 def test_supports_nested_structures(self):
     x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
     self.assertEqual(x, {"foo": {"bar": "baz"}})
コード例 #23
0
ファイル: utils.py プロジェクト: CptLemming/django-tables2
def compute_values_supports_shallow_structures():
    x = computed_values({"foo": lambda: {"bar": lambda: "baz"}})
    assert x == {"foo": {"bar": "baz"}}
コード例 #24
0
ファイル: test_utils.py プロジェクト: zqian/django-tables2
 def test_returns_None_if_not_enough_kwargs(self):
     x = computed_values({"foo": lambda x: "bar"})
     self.assertEqual(x, {"foo": None})
コード例 #25
0
ファイル: test_utils.py プロジェクト: Anlim/django-tables2
def test_compute_values_supports_shallow_structures():
    x = computed_values({'foo': lambda: 'bar'})
    assert x == {'foo': 'bar'}