Only sort a DataGrid if the column's sortable is set True
Review Request #10168 — Created Sept. 22, 2018 and submitted
Information | |
---|---|
gojeffcho | |
Djblets | |
release-2.0.x | |
4607 | |
8867d05... | |
Reviewers | |
djblets, students | |
Previously, all arguments to self.request.GET.get('sort', ...) were
passed toself.sort_list
even if they were invalid. This could cause
issues where columns collided with methods, such as when giving
?sort=fullname
to/users/
which resulted in an Error 500.Now, in
load_state()
, a set of sort keys are computed based on what
columns are set sortable, and the column'sdb_field
is only added to
self.sort_list
if the corresponding column is in this set.
Two unit tests have been added to exercise this functionality in
ascending and descending order, passing in an invalidsort
key and
checking thatself.sort_list
is empty.Two additional unit tests sort on one valid and one invalid key in
ascending and descending order and check thatself.sort_list
only
contains the valid key. All four of these unit tests pass.All tests in
djblets.datagrid.tests
and
reviewboard.datagrids.tests
passed without problems.
Description | From | Last Updated |
---|---|---|
Can you wrap your testing done a 72 characters as well? |
|
|
Something to note is that, while this says these specific test suites passed, you're actually going to need to run … |
|
|
nit: Your summary does not need a period. |
|
|
These should use get_sort_field, since it's not guaranteed to be db_field. (As a heads up, and you probably know this … |
|
|
We don't really use .format() in our codebase. Instead, we generally use %-formatted strings. I know this seems backwards, depending … |
|
|
These all have the same docstring. The docstring should be unique in each case, describing the exact conditions being tested. |
|
|
E501 line too long (83 > 79 characters) |
![]() |
|
E501 line too long (84 > 79 characters) |
![]() |
|
Since this is a function, we should probably pull this out into a temporary, à la: sort_field = column.get_sort_field() sort_keys.add(sort_field) … |
|
|
Let's call column.get_sort_field() once, and reuse the value. That way, if the method does anything remotely expensive, it only has … |
|
|
You don't need the parents around column.get_sort_field() |
|
|
The trailing """ should always be on its own line. Same with the others below. |
|
|
Python variable names should be in lower_snake_case (yes thats really what it is called :) ), as per PEP 8. |
|
|
Something that occurs to me... We should explicitly check to make sure get_sort_field() returned a value, so we don't subtly … |
|
|
nit: no blank line here. |
|
|
nit: no blank line here. |
|
|
Add a blank line between these two. |
|
|
Please use single quotes instead of double. |
|

Change Summary:
Committing the two additional unit tests.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 2 (+37 -1) |
Checks run (2 succeeded)
-
-
djblets/datagrid/grids.py (Diff revision 2) These should use
get_sort_field
, since it's not guaranteed to bedb_field
.(As a heads up, and you probably know this but just to be thorough, you'd want to update this as well when supporting lists later.)
-
djblets/datagrid/grids.py (Diff revision 2) We don't really use
.format()
in our codebase. Instead, we generally use%
-formatted strings. I know this seems backwards, depending on who you learn from, but it's a bit faster and is just what we have in the codebase.Also, you'll want to use single quotes where possible.
-
djblets/datagrid/tests.py (Diff revision 2) These all have the same docstring. The docstring should be unique in each case, describing the exact conditions being tested.

Change Summary:
Use
StatefulColumn
s; useget_sort_key()
instead ofdb_field
; use %-substitution; amend docstrings to be more descriptive and unique.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 3 (+39 -1) |
Checks run (1 failed, 1 succeeded)
flake8

Change Summary:
Fixed docstring length and wrapping.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 4 (+41 -1) |
Checks run (2 succeeded)
-
-
djblets/datagrid/grids.py (Diff revision 4) Let's call
column.get_sort_field()
once, and reuse the value. That way, if the method does anything remotely expensive, it only has to do it once. -
djblets/datagrid/tests.py (Diff revision 4) The trailing
"""
should always be on its own line.Same with the others below.

Change Summary:
Only call get_sort_field() once; fix docstring formatting.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 5 (+46 -1) |
Checks run (2 succeeded)
-
-
-
djblets/datagrid/grids.py (Diff revision 4) Since this is a function, we should probably pull this out into a temporary, à la:
sort_field = column.get_sort_field() sort_keys.add(sort_field) sort_keys.add('-%s' % sort_field)
-
djblets/datagrid/grids.py (Diff revision 4) You don't need the parents around
column.get_sort_field()

Change Summary:
Fixing the description and testing to 72 hard wrap.
Description: |
|
|||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Testing Done: |
|

Change Summary:
Removed unnecessary parentheses.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 6 (+46 -1) |
Checks run (2 succeeded)
-
-
djblets/datagrid/grids.py (Diff revision 6) Python variable names should be in
lower_snake_case
(yes thats really what it is called :) ), as per PEP 8.

Change Summary:
One of the unit tests were failing due to my recent changes which required updating the sort algorithm to map the column's id to its sort_field. All Datagrid tests now pass.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 7 (+49 -3) |
Checks run (2 succeeded)

Change Summary:
Accidentally left column.field_name insted of column.get_sort_field()
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 8 (+49 -3) |
Checks run (2 succeeded)

Change Summary:
Tests updated
Testing Done: |
|
---|
-
-
Something to note is that, while this says these specific test suites passed, you're actually going to need to run the entire test suite for Djblets and Review Board. Changes can often affect things other than in the module they're a part of, and we've had breakages in the past because it was assumed small changes were confined to their suite. While I don't think this change affects much else, it's good habit to run the whole test suite for both and mention that instead.
-
djblets/datagrid/grids.py (Diff revision 8) Something that occurs to me... We should explicitly check to make sure
get_sort_field()
returned a value, so we don't subtly break (addNone=None
and-None=None
to the sort list) in the event that a column issortable=True
with custom logic that intentionally or unintentionally returnsNone
.You can do this with a simple
if sort_field:
before adding.We'd also want a unit test to ensure
None
doesn't get in in any form.

Change Summary:
Updated unit tests.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 9 (+64 -8) |
Checks run (2 succeeded)

Change Summary:
Removed blank lines.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 10 (+62 -8) |
Checks run (2 succeeded)
-
Just a couple nits:
-
-

Change Summary:
Changed double quotes to single quotes, added linebreaks where requested.
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 11 (+67 -11) |