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

Prevent raising exception when limit is 0 #4098

Merged
merged 1 commit into from
May 10, 2016

Conversation

jpadilla
Copy link
Member

@jpadilla jpadilla commented May 4, 2016

Description

This fixes #4079 which raised exception in the Browsable API when using LimitOffsetPagination and limit is set to 0.

Thanks to @awbacker for raising the issue.

@jpadilla jpadilla self-assigned this May 4, 2016
@jpadilla jpadilla added the Bug label May 4, 2016
@jpadilla jpadilla added this to the 3.3.4 Release milestone May 4, 2016
"""
if a == 0 or b == 0:
Copy link
Member

Choose a reason for hiding this comment

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

Just b == 0 is sufficient.

Copy link
Member

@tomchristie tomchristie May 4, 2016

Choose a reason for hiding this comment

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

Not totally obv if we should do the check in here (less chance of missing the check elsewhere), or in the place where this is used (more semantically correct to the name of this func).

Copy link
Member Author

Choose a reason for hiding this comment

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

@tomchristie this seems to just be used in get_html_context(). I can just check if self.limit is 0 there and take care of things without changing _divide_with_ceil.

Copy link
Member Author

Choose a reason for hiding this comment

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

@tomchristie something along the lines of...

    def get_html_context(self):
        base_url = self.request.build_absolute_uri()
        current = 1
        final = 1

        if self.limit > 0:
            current = _divide_with_ceil(self.offset, self.limit) + 1

            # The number of pages is a little bit fiddly.
            # We need to sum both the number of pages from current offset to end
            # plus the number of pages up to the current offset.
            # When offset is not strictly divisible by the limit then we may
            # end up introducing an extra page as an artifact.
            final = (
                _divide_with_ceil(self.count - self.offset, self.limit) +
                _divide_with_ceil(self.offset, self.limit)
            )

            if final < 1:
                final = 1

        if current > final:
            current = final

        def page_number_to_url(page_number):
            if page_number == 1:
                return remove_query_param(base_url, self.offset_query_param)
            else:
                offset = self.offset + ((page_number - current) * self.limit)
                return replace_query_param(base_url, self.offset_query_param, offset)

        page_numbers = _get_displayed_page_numbers(current, final)
        page_links = _get_page_links(page_numbers, current, page_number_to_url)

        return {
            'previous_url': self.get_previous_link(),
            'next_url': self.get_next_link(),
            'page_links': page_links
        }

Copy link
Member

@tomchristie tomchristie May 4, 2016

Choose a reason for hiding this comment

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

Yeah, something like that.
I'd probably do...

if self.limit:
    ...  # determine current/final
else:
    ...  # set default values for current/final

Copy link
Member Author

Choose a reason for hiding this comment

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

@tomchristie done.

@codecov-io
Copy link

codecov-io commented May 4, 2016

Current coverage is 91.41%

Merging #4098 into master will increase coverage by +<.01%

@@             master      #4098   diff @@
==========================================
  Files            51         51          
  Lines          5473       5474     +1   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           5002       5004     +2   
+ Misses          471        470     -1   
  Partials          0          0          
  1. File ...mework/pagination.py was modified. more
    • Misses -1
    • Partials 0
    • Hits +1
  2. File ...rk/authentication.py (not in diff) was modified. more
    • Misses 0
    • Partials 0
    • Hits -1

Powered by Codecov. Last updated by ffdac0d...c85954c

@awbacker
Copy link

awbacker commented May 4, 2016

@jpadilla Thanks for taking this on, and to drf for finally introducing coverage.io to me :)

)
if final < 1:

if self.limit > 0:
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd prefer just 'if self.limit' here

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@tomchristie tomchristie merged commit 0795f73 into encode:master May 10, 2016
@tomchristie
Copy link
Member

👍 Thanks folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LimitOffset pagination crashes Browseable API when limit=0
4 participants