Mercurial > kallithea
comparison rhodecode/lib/helpers.py @ 1098:91ddd4db4614 beta
Added dedicated repo pager to support reversed new way slicing method of vcs and get_changesets method
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 27 Feb 2011 01:57:17 +0100 |
parents | 51076a2a2b64 |
children | e7d7f05217c1 |
comparison
equal
deleted
inserted
replaced
1097:ecf255351311 | 1098:91ddd4db4614 |
---|---|
27 from webhelpers.pylonslib.secure_form import secure_form | 27 from webhelpers.pylonslib.secure_form import secure_form |
28 from webhelpers.text import chop_at, collapse, convert_accented_entities, \ | 28 from webhelpers.text import chop_at, collapse, convert_accented_entities, \ |
29 convert_misc_entities, lchop, plural, rchop, remove_formatting, \ | 29 convert_misc_entities, lchop, plural, rchop, remove_formatting, \ |
30 replace_whitespace, urlify, truncate, wrap_paragraphs | 30 replace_whitespace, urlify, truncate, wrap_paragraphs |
31 from webhelpers.date import time_ago_in_words | 31 from webhelpers.date import time_ago_in_words |
32 | 32 from webhelpers.paginate import Page |
33 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \ | 33 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \ |
34 convert_boolean_attrs, NotGiven | 34 convert_boolean_attrs, NotGiven |
35 | 35 |
36 def _reset(name, value=None, id=NotGiven, type="reset", **attrs): | 36 def _reset(name, value=None, id=NotGiven, type="reset", **attrs): |
37 """Reset button | 37 """Reset button |
573 gravatar_url = baseurl + hashlib.md5(email_address.lower()).hexdigest() + "?" | 573 gravatar_url = baseurl + hashlib.md5(email_address.lower()).hexdigest() + "?" |
574 gravatar_url += urllib.urlencode({'d':default, 's':str(size)}) | 574 gravatar_url += urllib.urlencode({'d':default, 's':str(size)}) |
575 | 575 |
576 return gravatar_url | 576 return gravatar_url |
577 | 577 |
578 | |
579 #============================================================================== | |
580 # REPO PAGER | |
581 #============================================================================== | |
582 class RepoPage(Page): | |
583 | |
584 def __init__(self, collection, page=1, items_per_page=20, | |
585 item_count=None, url=None, **kwargs): | |
586 | |
587 """Create a "RepoPage" instance. special pager for paging | |
588 repository | |
589 """ | |
590 self._url_generator = url | |
591 | |
592 # Safe the kwargs class-wide so they can be used in the pager() method | |
593 self.kwargs = kwargs | |
594 | |
595 # Save a reference to the collection | |
596 self.original_collection = collection | |
597 | |
598 self.collection = collection | |
599 | |
600 # The self.page is the number of the current page. | |
601 # The first page has the number 1! | |
602 try: | |
603 self.page = int(page) # make it int() if we get it as a string | |
604 except (ValueError, TypeError): | |
605 self.page = 1 | |
606 | |
607 self.items_per_page = items_per_page | |
608 | |
609 # Unless the user tells us how many items the collections has | |
610 # we calculate that ourselves. | |
611 if item_count is not None: | |
612 self.item_count = item_count | |
613 else: | |
614 self.item_count = len(self.collection) | |
615 | |
616 # Compute the number of the first and last available page | |
617 if self.item_count > 0: | |
618 self.first_page = 1 | |
619 self.page_count = ((self.item_count - 1) / self.items_per_page) + 1 | |
620 self.last_page = self.first_page + self.page_count - 1 | |
621 | |
622 # Make sure that the requested page number is the range of valid pages | |
623 if self.page > self.last_page: | |
624 self.page = self.last_page | |
625 elif self.page < self.first_page: | |
626 self.page = self.first_page | |
627 | |
628 # Note: the number of items on this page can be less than | |
629 # items_per_page if the last page is not full | |
630 self.first_item = max(0, (self.item_count) - (self.page * items_per_page)) | |
631 self.last_item = ((self.item_count - 1) - items_per_page * (self.page - 1)) + 1 | |
632 | |
633 iterator = self.collection.get_changesets(start=self.first_item, | |
634 end=self.last_item, | |
635 reverse=True) | |
636 self.items = list(iterator) | |
637 | |
638 # Links to previous and next page | |
639 if self.page > self.first_page: | |
640 self.previous_page = self.page - 1 | |
641 else: | |
642 self.previous_page = None | |
643 | |
644 if self.page < self.last_page: | |
645 self.next_page = self.page + 1 | |
646 else: | |
647 self.next_page = None | |
648 | |
649 # No items available | |
650 else: | |
651 self.first_page = None | |
652 self.page_count = 0 | |
653 self.last_page = None | |
654 self.first_item = None | |
655 self.last_item = None | |
656 self.previous_page = None | |
657 self.next_page = None | |
658 self.items = [] | |
659 | |
660 # This is a subclass of the 'list' type. Initialise the list now. | |
661 list.__init__(self, self.items) | |
662 | |
663 | |
578 def safe_unicode(str): | 664 def safe_unicode(str): |
579 """safe unicode function. In case of UnicodeDecode error we try to return | 665 """safe unicode function. In case of UnicodeDecode error we try to return |
580 unicode with errors replace, if this failes we return unicode with | 666 unicode with errors replace, if this failes we return unicode with |
581 string_escape decoding """ | 667 string_escape decoding """ |
582 | 668 |