-
Notifications
You must be signed in to change notification settings - Fork 428
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
Pinned rows #406 #411
Pinned rows #406 #411
Conversation
if you looked at my PR? |
Sorry, I was a bit busy the last couple of days, I'll try to look at it tomorrow. |
Ok, no problem. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @djk2,
First impression: looks good! I added a couple of comments. I did not have much time, so not really in-depth, but something to work on.
I'll take a more in-depth ook later this week, wednesday/friday night I guess.
django_tables2/rows.py
Outdated
""" | ||
Get raw value from record render | ||
this value using by render_func | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use single quotes for comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem
django_tables2/rows.py
Outdated
row_attrs = computed_values(self._table.pinned_row_attrs, self._record) | ||
cssClass = "pinned-row" | ||
cssClass = " ".join([ | ||
'even' if next(self._table._counter) % 2 == 0 else 'odd', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic seems like it might be cleaner in a separate method (also used by BoundRow.attrs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created separate method for this code
django_tables2/rows.py
Outdated
def generator_pined_row(self, data): | ||
""" | ||
Generator for top and bottom pinned rows | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo pined
should be pinned
, and please use single quotes for comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course
django_tables2/rows.py
Outdated
self.data = data | ||
self.table = table | ||
self.top_pinned_data = pinned_data.get('top') | ||
self.bottom_pinned_data = pinned_data.get('bottom') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why split here in top/bottom? Does dict().get('top')
return None
if the key does not exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced by one line.
That was only for convenience.
Does not matter.
tests/test_pinned_rows.py
Outdated
def test_bound_rows_getitem(): | ||
""" | ||
Testing __getitem__ from BoundRows | ||
Sprawdzenie zwracanej klasy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amended
Great, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just few small comments. I didn't have enough time to go through whole PR.
django_tables2/rows.py
Outdated
@@ -71,12 +71,20 @@ def table(self): | |||
''' | |||
return self._table | |||
|
|||
def get_even_odd_css_class(self): | |||
''' | |||
Return css class `even` or `odd` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dot is missing: Return css class `even` or `odd`.
django_tables2/rows.py
Outdated
''' | ||
Return css class `even` or `odd` | ||
''' | ||
if next(self._table._counter) % 2 == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary &
operation instead of % 2
may be small optimization (about 25% for 10^6 rows):
if next(self._table._counter) & 1:
return 'odd'
return 'even'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by '25% for 10^6 rows', but 10^6 rows is a very unrealistic scenario: pagination should be used for datasets for more than about 100 rows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary notation is more efficient but is less readable for other people.
PEP 20 -- The Zen of Python:
- Simple is better than complex.
- Readability counts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right in this scale it's not worth to change it.
django_tables2/rows.py
Outdated
cssClass, | ||
row_attrs.get('class', "") | ||
]) | ||
row_attrs['class'] = cssClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO shorter construction is more readable e.g.:
row_attrs['class'] = " ".join([
self.get_even_odd_css_class(),
"pinned-row",
row_attrs.get('class', ""),
])
django_tables2/rows.py
Outdated
def attrs(self): | ||
''' | ||
Return the attributes for a certain pinned row. | ||
Add 'pinned-row' to css class attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dot is missing (like above).
django_tables2/rows.py
Outdated
Under the hood this method just makes a call to | ||
`.BoundPinnedRow.__getitem__` for each cell. | ||
''' | ||
for column, value in self.items(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can iterate over self.values()
instead of self.items()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks felixxm for very constructive suggestions. I will try to apply them
django_tables2/rows.py
Outdated
row_attrs['class'] = cssClass | ||
return AttributeDict(row_attrs) | ||
|
||
def __iter__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BoundPinnedRow
inherits from BoundRow
that has exactly the same __iter__
method, hence I think it's unnecessary to replicate it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fact, I didn't see before. Thanks
self.data = data | ||
self.table = table | ||
self.pinned_data = pinned_data or {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dictionary is mutable, so maybe it will be better to use:
self.pinned_data = pinned_data.copy() or {}
to avoid changes pinned_data
outside __init__
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it correct way.
Both solutions have pros and cons.
django_tables2/rows.py
Outdated
|
||
def generator_pinned_row(self, data): | ||
''' | ||
Generator for top and bottom pinned rows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Top and bottom pinned rows generator.
instead of current comment.
django_tables2/tables.py
Outdated
''' | ||
Return data for top pinned rows containing data for each row. | ||
Iterable type like: queryset, list of dicts, list of objects. | ||
Default return None. This method should be overriden in subclass of ~.Table\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overriden
-> overridden
~.Table\
->~.Table
django_tables2/tables.py
Outdated
''' | ||
Return data for bottom pinned rows containing data for each row. | ||
Iterable type like: queryset, list of dicts, list of objects. | ||
Default return None. This method should be overriden in subclass of ~.Table\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overriden
-> overridden
~.Table\
->~.Table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@djk2 thanks so far, looks good.
I would like to see some explicit documentation for this feature. Can you add a page to the documentation explaining how to achieve pinned rows? Don't be embarrassed by your English skills, we'll progressively improve on the text if needed.
django_tables2/tables.py
Outdated
Return data for top pinned rows containing data for each row. | ||
Iterable type like: queryset, list of dicts, list of objects. | ||
Default return None. This method should be overridden in subclass of ~.Table | ||
For None value, top pinned rows are not rendered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check the output for this in the documentation? I'd like to see backticks around None
to make it appear like code.
Please also use the more specific docstring syntax for the return value, to allow sphinx to render it like a return value. For example:
returns:
`None` (default) no pinned rows at the top, iterable, data for pinned rows at the top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I try prepare some documentation for this feature,
I also make fixes formatting docstring.
tests/test_rows.py
Outdated
count = 0 | ||
|
||
for row in simple_table.rows: | ||
css_class = 'even' if count % 2 == 0 else 'odd' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say repeating the logic in a test is not the way to go. also: this test tests some different things as well. Maybe just check that the next value differs from the previous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, your proposal is definitely better.
django_tables2/rows.py
Outdated
''' | ||
if next(self._table._counter) % 2 == 0: | ||
return 'even' | ||
return 'odd' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be condensed to a one-liner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem.
django_tables2/rows.py
Outdated
@@ -71,12 +71,20 @@ def table(self): | |||
''' | |||
return self._table | |||
|
|||
def get_even_odd_css_class(self): | |||
''' | |||
Return css class `even` or `odd`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe the word alternatingly or something like that should be added to this docstring?
23dc84c
to
b3568e2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@djk2 thanks, looks good. I commented on some typo's.
Did you verify the rendered version of the docstrings using tox -e docs
and inspecting the resulting html?
docs/pages/pinned-rows.rst
Outdated
|
||
Pinned rows attributes | ||
======================== | ||
If you wont override HTML attributes for pinned rows you can use: ``pinned_row_attrs``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wont -> won't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wont -> want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was not paying enough attention:
I think it should be "If you want to override..."
docs/pages/pinned-rows.rst
Outdated
Pinned rows attributes | ||
======================== | ||
If you wont override HTML attributes for pinned rows you can use: ``pinned_row_attrs``. | ||
Pined row attributes can be specified using a `dict` defining the HTML attributes for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pined -> Pinned
docs/pages/pinned-rows.rst
Outdated
=========== | ||
|
||
By using Pinned Rows, you can pin particular rows to the top or bottom of your table. | ||
To add pinned rows to your table, you must overridden `get_top_pinned_data` and/or `get_bottom_pinned_data` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overridden -> override
I ran tests for documentation using |
Hmm, apart from the 'want' -> 'want to' which I commented on, I see nothing preventing me to merge this. Unfortunately, some of my work on master makes that github cannot automatically merge this. Can you rebase your branch on current master? |
@djk2 Merged, thanks! |
released 1.4.2 |
I'm honored. Thank for cooperation. |
No description provided.