# HG changeset patch # User Marcin Kuzminski # Date 2011-02-27 00:57:17 # Node ID 91ddd4db4614abe8fc999f30d5c0b20e2a9fd640 # Parent ecf25535131156ab4e8d1e7aef5f811ba184879a Added dedicated repo pager to support reversed new way slicing method of vcs and get_changesets method diff --git a/rhodecode/controllers/changelog.py b/rhodecode/controllers/changelog.py --- a/rhodecode/controllers/changelog.py +++ b/rhodecode/controllers/changelog.py @@ -38,8 +38,7 @@ from pylons import request, session, tmp from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator from rhodecode.lib.base import BaseRepoController, render - -from webhelpers.paginate import Page +from rhodecode.lib.helpers import RepoPage log = logging.getLogger(__name__) @@ -68,7 +67,7 @@ class ChangelogController(BaseRepoContro p = int(request.params.get('page', 1)) c.total_cs = len(c.rhodecode_repo) - c.pagination = Page(c.rhodecode_repo, page=p, item_count=c.total_cs, + c.pagination = RepoPage(c.rhodecode_repo, page=p, item_count=c.total_cs, items_per_page=c.size) self._graph(c.rhodecode_repo, c.size, p) diff --git a/rhodecode/controllers/shortlog.py b/rhodecode/controllers/shortlog.py --- a/rhodecode/controllers/shortlog.py +++ b/rhodecode/controllers/shortlog.py @@ -29,13 +29,15 @@ import logging from pylons import tmpl_context as c, request -from webhelpers.paginate import Page - from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator from rhodecode.lib.base import BaseRepoController, render +from rhodecode.lib.helpers import RepoPage log = logging.getLogger(__name__) + + + class ShortlogController(BaseRepoController): @LoginRequired() @@ -46,7 +48,7 @@ class ShortlogController(BaseRepoControl def index(self): p = int(request.params.get('page', 1)) - c.repo_changesets = Page(c.rhodecode_repo, page=p, items_per_page=20) + c.repo_changesets = RepoPage(c.rhodecode_repo, page=p, items_per_page=20) c.shortlog_data = render('shortlog/shortlog_data.html') if request.params.get('partial'): return c.shortlog_data diff --git a/rhodecode/controllers/summary.py b/rhodecode/controllers/summary.py --- a/rhodecode/controllers/summary.py +++ b/rhodecode/controllers/summary.py @@ -43,8 +43,7 @@ from rhodecode.lib.utils import OrderedD from rhodecode.lib.celerylib import run_task from rhodecode.lib.celerylib.tasks import get_commits_stats - -from webhelpers.paginate import Page +from rhodecode.lib.helpers import RepoPage try: import json @@ -70,7 +69,7 @@ class SummaryController(BaseRepoControll def url_generator(**kw): return url('shortlog_home', repo_name=c.repo_name, **kw) - c.repo_changesets = Page(c.repo, page=1, items_per_page=10, + c.repo_changesets = RepoPage(c.repo, page=1, items_per_page=10, url=url_generator) e = request.environ diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -29,7 +29,7 @@ from webhelpers.text import chop_at, col convert_misc_entities, lchop, plural, rchop, remove_formatting, \ replace_whitespace, urlify, truncate, wrap_paragraphs from webhelpers.date import time_ago_in_words - +from webhelpers.paginate import Page from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \ convert_boolean_attrs, NotGiven @@ -575,6 +575,92 @@ def gravatar_url(email_address, size=30) return gravatar_url + +#============================================================================== +# REPO PAGER +#============================================================================== +class RepoPage(Page): + + def __init__(self, collection, page=1, items_per_page=20, + item_count=None, url=None, **kwargs): + + """Create a "RepoPage" instance. special pager for paging + repository + """ + self._url_generator = url + + # Safe the kwargs class-wide so they can be used in the pager() method + self.kwargs = kwargs + + # Save a reference to the collection + self.original_collection = collection + + self.collection = collection + + # The self.page is the number of the current page. + # The first page has the number 1! + try: + self.page = int(page) # make it int() if we get it as a string + except (ValueError, TypeError): + self.page = 1 + + self.items_per_page = items_per_page + + # Unless the user tells us how many items the collections has + # we calculate that ourselves. + if item_count is not None: + self.item_count = item_count + else: + self.item_count = len(self.collection) + + # Compute the number of the first and last available page + if self.item_count > 0: + self.first_page = 1 + self.page_count = ((self.item_count - 1) / self.items_per_page) + 1 + self.last_page = self.first_page + self.page_count - 1 + + # Make sure that the requested page number is the range of valid pages + if self.page > self.last_page: + self.page = self.last_page + elif self.page < self.first_page: + self.page = self.first_page + + # Note: the number of items on this page can be less than + # items_per_page if the last page is not full + self.first_item = max(0, (self.item_count) - (self.page * items_per_page)) + self.last_item = ((self.item_count - 1) - items_per_page * (self.page - 1)) + 1 + + iterator = self.collection.get_changesets(start=self.first_item, + end=self.last_item, + reverse=True) + self.items = list(iterator) + + # Links to previous and next page + if self.page > self.first_page: + self.previous_page = self.page - 1 + else: + self.previous_page = None + + if self.page < self.last_page: + self.next_page = self.page + 1 + else: + self.next_page = None + + # No items available + else: + self.first_page = None + self.page_count = 0 + self.last_page = None + self.first_item = None + self.last_item = None + self.previous_page = None + self.next_page = None + self.items = [] + + # This is a subclass of the 'list' type. Initialise the list now. + list.__init__(self, self.items) + + def safe_unicode(str): """safe unicode function. In case of UnicodeDecode error we try to return unicode with errors replace, if this failes we return unicode with