Exemple #1
0
 def test_get(self):
     obt = OrderByTuple(('a', 'b', 'c'))
     sentinel = object()
     assert obt.get('b', sentinel) is obt['b']  # keying
     assert obt.get('-', sentinel) is sentinel
     assert obt.get(0, sentinel) is obt['a']  # indexing
     assert obt.get(3, sentinel) is sentinel
Exemple #2
0
 def test_get(self):
     obt = OrderByTuple(('a', 'b', 'c'))
     sentinel = object()
     assert obt.get('b', sentinel) is obt['b']  # keying
     assert obt.get('-', sentinel) is sentinel
     assert obt.get(0, sentinel) is obt['a']  # indexing
     assert obt.get(3, sentinel) is sentinel
Exemple #3
0
 def test_get(self):
     obt = OrderByTuple(("a", "b", "c"))
     sentinel = object()
     assert obt.get("b", sentinel) is obt["b"]  # keying
     assert obt.get("-", sentinel) is sentinel
     assert obt.get(0, sentinel) is obt["a"]  # indexing
     assert obt.get(3, sentinel) is sentinel
Exemple #4
0
    def __init__(self, verbose_name=None, accessor=None, default=None,
                 visible=True, orderable=None, attrs=None, order_by=None,
                 empty_values=None, localize=None, footer=None):
        if not (accessor is None or isinstance(accessor, six.string_types) or
                callable(accessor)):
            raise TypeError('accessor must be a string or callable, not %s' %
                            type(accessor).__name__)
        if callable(accessor) and default is not None:
            raise TypeError('accessor must be string when default is used, not callable')
        self.accessor = Accessor(accessor) if accessor else None
        self._default = default
        self.verbose_name = verbose_name
        self.visible = visible
        self.orderable = orderable
        self.attrs = attrs or {}
        # massage order_by into an OrderByTuple or None
        order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by
        self.order_by = OrderByTuple(order_by) if order_by is not None else None
        if empty_values is not None:
            self.empty_values = empty_values

        self.localize = localize

        self.creation_counter = Column.creation_counter
        Column.creation_counter += 1

        self._footer = footer
Exemple #5
0
    def __init__(self, verbose_name=None, accessor=None, default=None,
                 visible=True, orderable=None, attrs=None, order_by=None,
                 sortable=None, empty_values=None, localize=None):
        if not (accessor is None or isinstance(accessor, six.string_types) or
                callable(accessor)):
            raise TypeError('accessor must be a string or callable, not %s' %
                            type(accessor).__name__)
        if callable(accessor) and default is not None:
            raise TypeError('accessor must be string when default is used, not callable')
        self.accessor = A(accessor) if accessor else None
        self._default = default
        self.verbose_name = verbose_name
        self.visible = visible
        if sortable is not None:
            warnings.warn('`sortable` is deprecated, use `orderable` instead.',
                          DeprecationWarning)
            # if orderable hasn't been specified, we'll use sortable's value
            if orderable is None:
                orderable = sortable
        self.orderable = orderable
        self.attrs = attrs or {}
        # massage order_by into an OrderByTuple or None
        order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by
        self.order_by = OrderByTuple(order_by) if order_by is not None else None
        if empty_values is not None:
            self.empty_values = empty_values

        self.localize = localize

        self.creation_counter = Column.creation_counter
        Column.creation_counter += 1
Exemple #6
0
    def __init__(
        self,
        verbose_name=None,
        accessor=None,
        default=None,
        visible=True,
        orderable=None,
        attrs=None,
        order_by=None,
        empty_values=None,
        localize=None,
        footer=None,
        exclude_from_export=False,
        linkify=False,
        initial_sort_descending=False,
    ):
        if not (accessor is None or isinstance(accessor, six.string_types)
                or callable(accessor)):
            raise TypeError("accessor must be a string or callable, not %s" %
                            type(accessor).__name__)
        if callable(accessor) and default is not None:
            raise TypeError(
                "accessor must be string when default is used, not callable")
        self.accessor = Accessor(accessor) if accessor else None
        self._default = default
        self.verbose_name = verbose_name
        self.visible = visible
        self.orderable = orderable
        self.attrs = attrs or getattr(self, "attrs", {})

        # massage order_by into an OrderByTuple or None
        order_by = (order_by, ) if isinstance(order_by,
                                              six.string_types) else order_by
        self.order_by = OrderByTuple(
            order_by) if order_by is not None else None
        if empty_values is not None:
            self.empty_values = empty_values

        self.localize = localize
        self._footer = footer
        self.exclude_from_export = exclude_from_export

        link_kwargs = None
        if callable(linkify) or hasattr(self, "get_url"):
            link_kwargs = dict(
                url=linkify if callable(linkify) else self.get_url)
        elif isinstance(linkify, (dict, tuple)):
            link_kwargs = dict(reverse_args=linkify)
        elif linkify is True:
            link_kwargs = dict(accessor=self.accessor)

        if link_kwargs is not None:
            self.link = LinkTransform(attrs=self.attrs.get("a", {}),
                                      **link_kwargs)

        self.initial_sort_descending = initial_sort_descending

        self.creation_counter = Column.creation_counter
        Column.creation_counter += 1
Exemple #7
0
 def test_intexing(self):
     obt = OrderByTuple(('a', 'b', 'c'))
     assert obt[0] == OrderBy('a')
     assert obt['b'] == OrderBy('b')
     with self.assertRaises(KeyError):
         obt['d']
     with self.assertRaises(TypeError):
         obt[('tuple', )]
Exemple #8
0
 def test_intexing(self):
     obt = OrderByTuple(("a", "b", "c"))
     assert obt[0] == OrderBy("a")
     assert obt["b"] == OrderBy("b")
     with self.assertRaises(KeyError):
         obt["d"]
     with self.assertRaises(TypeError):
         obt[("tuple", )]
Exemple #9
0
def orderbytuple():
    obt = OrderByTuple('abc')
    Assert(obt) == (OrderBy('a'), OrderBy('b'), OrderBy('c'))
    Assert(obt[0]) == OrderBy('a')
    Assert(obt['b']) == OrderBy('b')
    with Assert.raises(IndexError) as error:
        obt['d']
    with Assert.raises(TypeError) as error:
        obt[('tuple', )]
Exemple #10
0
def test_orderbytuple_sort_key_multiple():
    obt = OrderByTuple(('a', '-b'))
    items = [
        {"a": 1, "b": 2},
        {"a": 1, "b": 3},
    ]
    assert sorted(items, key=obt.key) == [
        {"a": 1, "b": 3},
        {"a": 1, "b": 2},
    ]
Exemple #11
0
 def test_sort_key_multiple(self):
     obt = OrderByTuple(('a', '-b'))
     items = [
         {'a': 1, 'b': 2},
         {'a': 1, 'b': 3},
     ]
     assert sorted(items, key=obt.key) == [
         {'a': 1, 'b': 3},
         {'a': 1, 'b': 2},
     ]
Exemple #12
0
 def test_sort_key_multiple(self):
     obt = OrderByTuple(("a", "-b"))
     items = [{"a": 1, "b": 2}, {"a": 1, "b": 3}]
     assert sorted(items, key=obt.key) == [{
         "a": 1,
         "b": 3
     }, {
         "a": 1,
         "b": 2
     }]
Exemple #13
0
    def order_by(self):
        """
        Returns an `.OrderByTuple` of appropriately prefixed data source
        keys used to sort this column.

        See `.order_by_alias` for details.
        """
        if self.column.order_by is not None:
            order_by = self.column.order_by
        else:
            # default to using column accessor as data source sort key
            order_by = OrderByTuple((self.accessor, ))
        return order_by.opposite if self.order_by_alias.is_descending else order_by
Exemple #14
0
def orderbytuple():
    obt = OrderByTuple(('a', 'b', 'c'))
    assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c'))

    # indexing
    assert obt[0] == OrderBy('a')
    assert obt['b'] == OrderBy('b')
    with raises(KeyError):
        obt['d']
    with raises(TypeError):
        obt[('tuple', )]

    # .get
    sentinel = object()
    assert obt.get('b', sentinel) is obt['b'] # keying
    assert obt.get('-', sentinel) is sentinel
    assert obt.get(0,   sentinel) is obt['a'] # indexing
    assert obt.get(3,   sentinel) is sentinel

    # .opposite
    assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c')

    # in
    assert 'a' in obt and '-a' in obt
Exemple #15
0
def test_orderbytuple():
    obt = OrderByTuple(('a', 'b', 'c'))
    assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c'))

    # indexing
    assert obt[0] == OrderBy('a')
    assert obt['b'] == OrderBy('b')
    with pytest.raises(KeyError):
        obt['d']
    with pytest.raises(TypeError):
        obt[('tuple', )]

    # .get
    sentinel = object()
    assert obt.get('b', sentinel) is obt['b']  # keying
    assert obt.get('-', sentinel) is sentinel
    assert obt.get(0, sentinel) is obt['a']  # indexing
    assert obt.get(3, sentinel) is sentinel

    # .opposite
    assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c')

    # in
    assert 'a' in obt and '-a' in obt
Exemple #16
0
 def test_sort_key_empty_comes_first(self):
     obt = OrderByTuple(('a'))
     items = [
         {'a': 1},
         {'a': ''},
         {'a': 2},
     ]
     if six.PY3:
         assert sorted(items, key=obt.key) == [
             {'a': ''},
             {'a': 1},
             {'a': 2},
         ]
     else:
         assert sorted(items, key=obt.key) == [
             {'a': 1},
             {'a': 2},
             {'a': ''},
         ]
Exemple #17
0
def test_orderbytuple_sort_key_empty_comes_first():
    obt = OrderByTuple(('a'))
    items = [
        {"a": 1},
        {"a": ""},
        {"a": 2},
    ]
    if six.PY3:
        assert sorted(items, key=obt.key) == [
            {"a": ""},
            {"a": 1},
            {"a": 2},
        ]
    else:
        assert sorted(items, key=obt.key) == [
            {"a": 1},
            {"a": 2},
            {"a": ""},
        ]
Exemple #18
0
 def test_sort_key_empty_comes_first(self):
     obt = OrderByTuple(("a"))
     items = [{"a": 1}, {"a": ""}, {"a": 2}]
     if six.PY3:
         assert sorted(items, key=obt.key) == [{
             "a": ""
         }, {
             "a": 1
         }, {
             "a": 2
         }]
     else:
         assert sorted(items, key=obt.key) == [{
             "a": 1
         }, {
             "a": 2
         }, {
             "a": ""
         }]
Exemple #19
0
 def test_in(self):
     obt = OrderByTuple(('a', 'b', 'c'))
     assert 'a' in obt and '-a' in obt
Exemple #20
0
 def test_basic(self):
     obt = OrderByTuple(('a', 'b', 'c'))
     assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c'))
Exemple #21
0
 def test_in(self):
     obt = OrderByTuple(("a", "b", "c"))
     assert "a" in obt and "-a" in obt
Exemple #22
0
 def test_opposite(self):
     assert OrderByTuple(("a", "-b", "c")).opposite == ("-a", "b", "-c")
Exemple #23
0
 def test_opposite(self):
     assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c')
Exemple #24
0
 def test_basic(self):
     obt = OrderByTuple(("a", "b", "c"))
     assert obt == (OrderBy("a"), OrderBy("b"), OrderBy("c"))