Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow column to be ordered by ascending by default #566

Closed
wants to merge 4 commits into from
Closed

Allow column to be ordered by ascending by default #566

wants to merge 4 commits into from

Conversation

karech
Copy link

@karech karech commented Apr 12, 2018

Not sure about:

  1. name of param;
  2. do I need to make changes for readme/docs.

Copy link
Owner

@jieter jieter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @karech, interesting use case, thanks for the pull!

Just to be sure, doesn't using order_by='-name' give the desired result?

Anyway, we need to have this properly documented (at least in the Column docstring and tested before it's acceptable.

if self.column.default_descending_ordering:
order_by.next = order_by if self.is_ordered else order_by.opposite
else:
order_by.next = order_by.opposite if self.is_ordered else order_by
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about:

order_by.next = order_by.opposite if self.is_ordered else order_by
if self.column.default_descending_ordering:
   order_by.next = order_by.opposite

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I think this will break.

@@ -111,7 +113,7 @@ class Column(object):
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):
exclude_from_export=False, default_descending_ordering=False):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is certainly a good question.

default_ordering_desc?
default_ordering='-' for descending?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about "default", because this only affects BoundColumn.order_by_alias.next.
Maybe first_ordering_descending=True ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say initial then.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial_descending_ordering ?

@karech
Copy link
Author

karech commented Apr 12, 2018

@jieter Let me try to explain, what I wanna achieve.

I have tables, and custom columns (lets say NumberColumn, DecimalColumn(NunberColumn) and etc). And I wanna them always be sorted by descending on first user click.
That's why I decided to make this option, so I can do:

class NumberColumn(django_tables2.Column):
    def __init__(*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.default_descending_ordering = True

And on first click on column they (columns with numbers) will be sorted by descending, but other columns (like text columns) - by ascending.

Copy link
Owner

@jieter jieter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karech implementation looks good now, but before I'm going to merge this, it needs to have a good test. Especially with these less commonly used features, it's easy for a future contributor to break it accidentally if no tests cover it.

Let me know if you need any help.

@@ -96,6 +96,8 @@ class Column(object):
- If `True`, force localization
- If `False`, values are not localized
- If `None` (default), localization depends on the ``USE_L10N`` setting.
default_descending_ordering (bool): If 'True',
column.order_by_alias.next will return descending ordering if not ordered.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add to this sentence that it defaults to False?

@karech
Copy link
Author

karech commented Apr 23, 2018

@jieter Hello! I'v added tests (tried to make same styling as other tests).

And I'm still not sure about naming of parameter. Looks not very clear.

Copy link
Owner

@jieter jieter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @karech nice work. I added a comment about the style of test.

It would also be nice to actually assert that data is ordered descending initially.

table_desc = TableDescOrd(data)

assert table.columns[1].order_by_alias.next == 'field'
assert table_desc.columns[1].order_by_alias.next == '-field'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use a TestCase-derived test class? Using its assertion methods (like self.assertEqual()) results in much more informative failed assertion messages.

@jieter
Copy link
Owner

jieter commented Aug 2, 2018

Closing this after merging #597

@jieter jieter closed this Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants