-
Notifications
You must be signed in to change notification settings - Fork 3
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
Use server-side paging in aips page. (#209) #215
Conversation
57f1273
to
fc3fe8d
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.
Hi @mcantelon! This looks good to me. I haven't tested but I can see that what you're doing is going to bring a big improvement. I've made some small suggestions, but nothing important functionally speaking. Thank you!
AIPscan/Reporter/templates/aips.html
Outdated
<li class="paginate_button page-item previous {% if pager.prev_num is none %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.prev_num }}&{{ state_querystring }}" class="page-link">Previous</a></li> | ||
<li class="paginate_button page-item active"><a href="#" class="page-link">{{ pager.page }}</a></li> | ||
<li class="paginate_button page-item next {% if pager.next_num is none %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.next_num }}&{{ state_querystring }}" class="page-link">Next</a></li> | ||
<li class="paginate_button page-item last {% if pager.page == pager.pages %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.pages }}&{{ state_querystring }}" class="page-link">Last</a></li> |
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.
Do you know if we can include query parameters directly in url_for, e.g. something like the following:
url_for('reporter.view_aips', page=pager.next_num, **state_querystring)
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.
Nice! Yeah, that's cleaner.
def test_calculate_paging_window(): | ||
class MockPagination(object): | ||
pass | ||
|
||
pagination = MockPagination() | ||
pagination.per_page = 5 | ||
pagination.total = 17 | ||
|
||
# Test paging window at start of results | ||
first_item, last_item = helpers.calculate_paging_window(1, pagination) | ||
|
||
assert first_item == 1 | ||
assert last_item == 5 | ||
|
||
# Test paging window on an arbitrary page of results | ||
first_item, last_item = helpers.calculate_paging_window(3, pagination) | ||
|
||
assert first_item == 11 | ||
assert last_item == 15 | ||
|
||
# Test paging window with last item being set to the total of items | ||
first_item, last_item = helpers.calculate_paging_window(4, pagination) | ||
|
||
assert first_item == 16 | ||
assert last_item == 17 |
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 may want to use pytest.mark.parametrize, but I must say that sometimes I find pytest's parametrize not as legible as I wish. Yours is fine.
class MockPagination(object):
pass
@pytest.mark.parametrize("page, per_page, total, expected_first_item, expected_last_item", [
# Test paging window at start of results.
(1, 5, 17, 1, 5),
# ...
(3, 5, 17, 11, 15),
# ...
(4, 5, 17, 16, 17),
# add more scenarios as needed
])
def test_calculate_paging_window(page, per_page, total, expected_first_item, expected_last_item):
pagination = MockPagination()
pagination.per_page = per_page
pagination.total = total
first_item, last_item = helpers.calculate_paging_window(page, pagination)
assert first_item == expected_first_item
assert last_item == expected_last_item
AIPscan/Reporter/helpers.py
Outdated
def urlencode_without_none_values(values): | ||
for key in values: | ||
if values[key] is None: | ||
values[key] = "" | ||
|
||
return urlencode(values) |
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 a good use case for values.items()
, e.g.:
def urlencode_without_none_values(values):
"""URL encode the values dict converting any None values to empty strings."""
for key, value in values.items():
if value is None:
values[key] = ""
return urlencode(values)
Here's an alternative using a dictionary comprenhension, I think this can be useful if you want to avoid mutating the input dictionary or doing it during iteration.
def urlencode_without_none_values(values):
"""URL encode the values dict converting any None values to empty strings."""
return urlencode(
{
key: (value if value is not None else '')
for key, value in values.items()
}
)
AIPscan/Reporter/helpers.py
Outdated
last_item = page * pagination.per_page | ||
|
||
if last_item > pagination.total: | ||
last_item = pagination.total |
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.
An alternative to calculate last_item that is more concise by using min()
:
last_item = page * pagination.per_page | |
if last_item > pagination.total: | |
last_item = pagination.total | |
last_item = min(page * pagination.per_page, pagination.total) |
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.
Very handy!
AIPscan/Reporter/views.py
Outdated
@@ -85,10 +89,24 @@ def view_aips(): | |||
except Exception as e: | |||
print(e) | |||
|
|||
aips = AIP.query.filter_by(storage_service_id=storage_service.id) | |||
page = int(request.args.get(request_params.PAGE, default="1")) |
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.
Should we handle ValueError
when int doesn't receive a valid base 10 literal?
Thanks @sevein ! |
I've updated the PR @sevein .... thanks for the tips! |
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.
LGTM
To avoid slow page responses when there are a large amount of AIPs use server-side paging to limit the number of AIP HTML sent to the browser.
6cd0ce5
to
6df7478
Compare
To avoid slow page responses when there are a large amount of AIPs use server-side paging to limit the number of AIP HTML sent to the browser.