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

Support customized pagination in class-based views #65

Merged
merged 9 commits into from
Mar 26, 2012
Merged

Support customized pagination in class-based views #65

merged 9 commits into from
Mar 26, 2012

Conversation

christhekeele
Copy link
Contributor

Customization of a table's pagination is performed in the tables.paginate() method, through a set of arguments passed in to it (klass, per_page, and page) along with extra _args and *_kwargs getting shuttled to the provided Pagination klass. These values are given to tables.paginate() from the RequestConfig class, through a dictionary instantiated as 'paginate', allowing pagination to be customized.

Previously, there was no easy way to populate the paginate dictionary from the class based views, since people using the new views don't instantiate RequestConfig themselves. By implementing the SingleTableMixin.get_paginate() method, I've exposed the paginate dictionary to the SingleTableView.as_view() function, so people can use the standard class based plug in work flow.

get_paginate() behaves like the other get_ methods within the view, uses duck typing to match dictionary-like objects (if for some reason people get really fancy), and acts the way RequestConfig expects it to:

  • if the paginate dictionary isn't set, it defaults to true
    • example: SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable)
  • if set to true (or defaulting to it), default pagination will be used
    • example: SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=True)
  • if set to false, tables.paginate()'s default of paginating will be disabled.
    • example: SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=False)
  • if set to a dictionary, tables.paginate() will receive it when called and behave however people ask it to.
    • example: SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=dict(per_page=42, klass=SuperPaginator, page=some_start_page, extra=will_get_passed_to_super_paginator)

I've written this because django-tables2 seems preferable to other ordered paginated table plugins, gives you tons of control over styling, is actually maintained, and bradleyayers is awesomely responsive; however I'm dealing with large datasets and class-based views, so I wanted to implement it with an ajaxified Paginator. I'm toying with a modified version of the standard tables.html file that plays nicely with django-endless-pagination. If I succeed, I'll throw that up into a push request, perhaps with logic in the djangotables2.RenderTableNode class to pick it if django-endless-pagination is discovered. From there it should be pretty easy to add defaults for all the major pagination plugins that inherit from django's Paginator.

@bradleyayers
Copy link
Collaborator

This looks good to me. My only reservation is whether the variables should be prefixed with table_ or not. I'm concerned about possible name clashes if other classes are mixed, which is why I have table_class and table_data -- thoughts?

@christhekeele
Copy link
Contributor Author

Agreed. Since that gets passed straight to the config and is used internally, I think just changing the CBV's nomenclature will suffice.

Returns pagination options: True for standard pagination (default),
False for no pagination, and a dictionary for custom pagination.
"""
if isinstance(self.table_pagination, int):
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about if isinstance(self.table_pagination, bool):? Then the next line doesn't need bool().

…y checks, keep pagination set to true by default.
Returns pagination options: True for standard pagination (default),
False for no pagination, and a dictionary for custom pagination.
"""
return self.table_pagination or True
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd probably just leave this as return self.table_pagination.

@bradleyayers bradleyayers merged commit 16df453 into jieter:master Mar 26, 2012
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