Back to index

plone3  3.1.7
batching.py
Go to the documentation of this file.
00001 from zope import interface
00002 from plone.app.content.interfaces import IBatch
00003 
00004 class Batch(object):
00005     interface.implements(IBatch)
00006 
00007     def __init__(self, items, pagesize=20, pagenumber=1, navlistsize=5):
00008         self.items = items
00009         self.pagesize = pagesize
00010         self.pagenumber = pagenumber
00011         self.navlistsize = navlistsize
00012 
00013     def __len__(self):
00014         return len(self.items)
00015 
00016     def __iter__(self):
00017         start = (self.pagenumber-1) * self.pagesize
00018         end = start + self.pagesize
00019         return self.items[start:end].__iter__()
00020 
00021     @property
00022     def size(self):
00023         return len(self)
00024 
00025     @property
00026     def firstpage(self):
00027         return 1
00028 
00029     @property
00030     def lastpage(self):
00031         pages = self.size / self.pagesize
00032         if self.size % self.pagesize:
00033             pages += 1
00034         return pages
00035 
00036     @property
00037     def items_not_on_page(self):
00038         items_on_page = list(self)
00039         return [item for item in self.items if item not in
00040                 items_on_page]
00041 
00042     @property
00043     def multiple_pages(self):
00044         return bool(self.size / self.pagesize)
00045 
00046     @property
00047     def has_next(self):
00048         return (self.pagenumber * self.pagesize) < self.size
00049 
00050     @property
00051     def has_previous(self):
00052         return self.pagenumber > 1
00053 
00054     @property
00055     def previouspage(self):
00056         return self.pagenumber - 1
00057 
00058     @property
00059     def nextpage(self):
00060         return self.pagenumber + 1
00061 
00062     @property
00063     def next_item_count(self):
00064         nextitems = self.size - (self.pagenumber * self.pagesize)
00065         if nextitems > self.pagesize:
00066             return self.pagesize
00067         return nextitems
00068 
00069     @property
00070     def navlist(self):
00071         start = self.pagenumber - (self.navlistsize / 2)
00072         if start < 1:
00073             start = 1
00074         end = start + self.navlistsize - 1
00075         if end > self.lastpage:
00076             end = self.lastpage
00077 
00078         return range(start, end+1)
00079 
00080     @property
00081     def show_link_to_first(self):
00082         return 1 not in self.navlist
00083 
00084     @property
00085     def show_link_to_last(self):
00086         return self.lastpage not in self.navlist
00087 
00088     @property
00089     def second_page_not_in_navlist(self):
00090         return 2 not in self.navlist
00091 
00092     @property
00093     def islastpage(self):
00094         return self.lastpage == self.pagenumber
00095 
00096     @property
00097     def previous_pages(self):
00098         return self.navlist[:self.navlist.index(self.pagenumber)]
00099 
00100     @property
00101     def next_pages(self):
00102         return self.navlist[self.navlist.index(self.pagenumber)+1:]
00103 
00104     @property
00105     def before_last_page_not_in_navlist(self):
00106         return (self.lastpage - 1) not in self.navlist
00107 
00108     @property
00109     def items_on_page(self):
00110         if self.islastpage:
00111             remainder = self.size % self.pagesize
00112             if remainder == 0:
00113                 return self.pagesize
00114             else:
00115                 return remainder
00116         else:
00117             return self.pagesize