Ñò {XHc@sdZdZdZdZdZddkZddkZyddklZWn#e j oddk lZnXddk l Z l Z yddkZWn eZn XeiZdd „Zd efd „ƒYZd efd „ƒYZdefd„ƒYZdS(s¯ paginate: a module to help split up lists or results from ORM queries ======================================================================= What is pagination? --------------------- This module helps dividing large lists of items into pages. The user is shown one page at a time and can navigate to other pages. Imagine you are offering a company phonebook and let the user search the entries. If the search result contains 23 entries but you may want to display no more than 10 entries at once. The first page contains entries 1-10, the second 11-20 and the third 21-23. See the documentation of the "Page" class for more information. How do I use it? ------------------ One page of items is represented by the *Page* object. A *Page* gets initalized with two parameters at least: - the collection of items to pick a range from - the page number that is required (default is 1 - the first page) A simple example (ipython session):: # Set up the routes context (only if you are not using a Pylons application) >>> from routes import Mapper; mapper=Mapper(); mapper.connect(':controller') # Create a sample collection of 1000 items >>> my_collection = range(1000) # Create a Page object for the 3rd page (20 items per page is the default) >>> my_page = Page(my_collection, page=3) # The page object can be printed directly to get its details >>> my_page Page: Collection type: (Current) page: 3 First item: 41 Last item: 60 First page: 1 Last page: 50 Previous page: 2 Next page: 4 Items per page: 20 Number of items: 1000 Number of pages: 50 # Print a list of items on the current page >>> my_page.items [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] # The *Page* object can be used as an iterator: >>> for my_item in my_page: print my_item, 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 # On a web page you will want to use a "pager" that creates links that # the user can click on to load other pages in the set. # [The ">>" prompt is to hide untestable examples from doctest.] >> my_page.pager() 1 2 [3] 4 5 .. 50 (this is actually HTML) # The pager can be customized: >> my_page.pager('$link_previous ~3~ $link_next (Page $page of $page_count)') 1 2 [3] 4 5 6 .. 50 > (Page 3 of 50) Please see the documentation on *Page* and *Page.pager()*. There are many parameters that customize the Page's behavior. Can I use AJAX / AJAH? ------------------------ Yes. See *partial_param* and *onclick* in *Page.pager()*. Notes ------- Page numbers and item numbers start at 1. This concept has been used because users expect that the first page has number 1 and the first item on a page also has number 1. So if you want to use the page's items by their index number please note that you have to substract 1. This module is the successor to the deprecated ``webhelpers.pagination`` module. It is *NOT* API compatible. This version of paginate is based on the code from http://workaround.org/cgi-bin/hg-paginate that is known at the "Paginate" module on PyPi. s0.3.6s 2008-05-01s(Christoph Haas s@Copyright (c) 2007,2008 Christoph Haas iÿÿÿÿN(tTemplate(tliteraltHTMLcCs³t|ttfƒo|Stidƒptidƒoht|tiiiƒo t |ƒSt|ti i i ƒpt|ti i i ƒot||ƒSntdƒ‚dS(si Auto-detect the kind of object and return a list/tuple to access items from the collection. s0.4s0.5s³Sorry, your collection type is not supported by the paginate module. You can either provide a list, a tuple, an SQLAlchemy 0.4 select object or an SQLAlchemy 0.4 ORM-query object.N(t isinstancetlistttupletsqlalchemy_availablet startswitht sqlalchemytormtquerytQueryt_SQLAlchemyQuerytsqlt expressiontCompoundSelecttSelectt_SQLAlchemySelectt TypeError(tobjtsqlalchemy_session((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt get_wrappers  RcBs,eZdZdd„Zd„Zd„ZRS(sM Iterable that allows to get slices from an SQLAlchemy Select object cCs?t|tiiiƒptdƒ‚n||_||_dS(Ns¤If you want to page an SQLAlchemy 'select' object then you have to provide a 'sqlalchemy_session' argument. See also: http://www.sqlalchemy.org/docs/04/session.html(RRR tscopingt ScopedSessionRRR(tselfRR((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt__init__«s cCsgt|tƒp td‚n|i}|i|i}|ii|ƒi|ƒ}|ii |ƒi ƒS(Ns)__getitem__ without slicing not supported( Rtslicet ExceptiontstarttstopRtoffsettlimitRtexecutetfetchall(RtrangeRRtselect((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt __getitem__´s   cCs|ii|iƒiS(N(RR Rtrowcount(R((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt__len__¼sN(t__name__t __module__t__doc__tNoneRR$R&(((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyR§s R cBs)eZdZd„Zd„Zd„ZRS(sL Iterable that allows to get slices from an SQLAlchemy Query object cCs ||_dS(N(R(RR((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyRÃscCs(t|tƒp td‚n|i|S(Ns)__getitem__ without slicing not supported(RRRR(RR"((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyR$Æs cCs |iiƒS(N(Rtcount(R((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyR&Ës(R'R(R)RR$R&(((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyR ¿s  tPagecBsqeZdZddddd„Zd„Zdddeddd d d d hd d6hdd6hdd6d„ ZRS(s!A list/iterator of items representing one page in a larger collection. An instance of the "Page" class is created from a collection of things. The instance works as an iterator running from the first item to the last item on the given page. The collection can be: - a sequence - an SQLAlchemy query - e.g.: Session.query(MyModel) - an SQLAlchemy select - e.g.: sqlalchemy.select([my_table]) A "Page" instance maintains pagination logic associated with each page, where it begins, what the first/last item on the page is, etc. The pager() method creates a link list allowing the user to go to other pages. **WARNING:** Unless you pass in an item_count, a count will be performed on the collection every time a Page instance is created. If using an ORM, it's advised to pass in the number of items in the collection if that number is known. Instance attributes: original_collection Points to the collection object being paged through item_count Number of items in the collection page Number of the current page items_per_page Maximal number of items displayed on a page first_page Number of the first page - starts with 1 last_page Number of the last page page_count Number of pages items Sequence/iterator of items on the current page first_item Index of first item on the current page - starts with 1 last_item Index of last item on the current page iicOs¬d|jo"tidƒ|d}|d=nd|jo"tidƒ|d}|d=n||_||_|ot||ƒ|_n g|_yt|ƒ|_Wntj od|_nX||_ |dj o ||_ nt |iƒ|_ |i djo@d|_ |i d|i d|_|i |id|_|i|ijo|i|_n$|i|i jo|i |_n|id|d|_t|i|d|i ƒ|_t|i|id|i!ƒ|_|i|i jo|id|_n d|_|i|ijo|id|_q•d|_nId|_ d|_d|_d|_d|_d|_d|_g|_ti||iƒdS(saCreate a "Page" instance. Parameters: collection Sequence, SQLAlchemy select object or SQLAlchemy ORM-query representing the collection of items to page through. page The requested page number - starts with 1. Default: 1. items_per_page The maximal number of items to be displayed per page. Default: 20. item_count (optional) The total number of items in the collection - if known. If this parameter is not given then the paginator will count the number of elements in the collection every time a "Page" is created. Giving this parameter will speed up things. sqlalchemy_session (optional) If you want to use an SQLAlchemy (0.4) select object as a collection then you need to provide an SQLAlchemy session object. Select objects do not have a database connection attached so it would not be able to execute the SELECT query. Further keyword arguments are used as link arguments in the pager(). tpage_nrs3'page_nr' is deprecated. Please use 'page' instead.t current_pages8'current_page' is deprecated. Please use 'page' instead.iiN(twarningstwarntkwargstoriginal_collectionRt collectiontinttpaget ValueErrortitems_per_pageR*t item_counttlent first_paget page_countt last_paget first_itemtmint last_itemRtitemst previous_paget next_pageR(RR3R5R7R8RtargsR1((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyRs^                #          cCs|dh t|iƒd6|id6|id6|id6|id6|id6|id6|id 6|i d 6|i d 6|i d 6S( NsiPage: Collection type: %(type)s (Current) page: %(page)s First item: %(first_item)s Last item: %(last_item)s First page: %(first_page)s Last page: %(last_page)s Previous page: %(previous_page)s Next page: %(next_page)s Items per page: %(items_per_page)s Number of items: %(item_count)s Number of pages: %(page_count)s ttypeR5R=R?R:R<RARBR7R8R;( RDR3R5R=R?R:R<RARBR7R8R;(R((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt__repr__s          s~2~R5tpartialt s<>tt pager_linktclasst pager_curpaget pager_dotdotc sŒ‡‡‡‡‡‡fd†‰‡‡‡‡‡ fd†}ˆidjpˆidjo | odStid||ƒ}t|ƒih ˆid6ˆid6ˆid 6ˆid 6ˆid 6ˆi d 6ˆi d 6ˆi d6ˆiˆijoˆˆi|ƒpdd6ˆiˆijoˆˆi|ƒpdd6ˆi oˆˆi | ƒpdd6ˆi oˆˆi | ƒpdd6ƒ}t|ƒS(s& Return string with links to other pages (e.g. "1 2 [3] 4 5 6 7"). format: Format string that defines how the pager is rendered. The string can contain the following $-tokens that are substituted by the string.Template module: - $first_page: number of first reachable page - $last_page: number of last reachable page - $page: number of currently selected page - $page_count: number of reachable pages - $items_per_page: maximal number of items per page - $first_item: index of first item on the current page - $last_item: index of last item on the current page - $item_count: total number of items - $link_first: link to first page (unless this is first page) - $link_last: link to last page (unless this is last page) - $link_previous: link to previous page (unless this is first page) - $link_next: link to next page (unless this is last page) To render a range of pages the token '~3~' can be used. The number sets the radius of pages around the current page. Example for a range with radius 3: '1 .. 5 6 7 [8] 9 10 11 .. 500' Default: '~2~' symbol_first String to be displayed as the text for the %(link_first)s link above. Default: '<<' symbol_last String to be displayed as the text for the %(link_last)s link above. Default: '>>' symbol_previous String to be displayed as the text for the %(link_previous)s link above. Default: '<' symbol_next String to be displayed as the text for the %(link_next)s link above. Default: '>' separator: String that is used to seperate page links/numbers in the above range of pages. Default: ' ' page_param: The name of the parameter that will carry the number of the page the user just clicked on. The parameter will be passed to a url_for() call so if you stay with the default ':controller/:action/:id' routing and set page_param='id' then the :id part of the URL will be changed. If you set page_param='page' then url_for() will make it an extra parameters like ':controller/:action/:id?page=1'. You need the page_param in your action to determine the page number the user wants to see. If you do not specify anything else the default will be a parameter called 'page'. partial_param: When using AJAX/AJAH to do partial updates of the page area the application has to know whether a partial update (only the area to be replaced) or a full update (reloading the whole page) is required. So this parameter is the name of the URL parameter that gets set to 1 if the 'onclick' parameter is used. So if the user requests a new page through a Javascript action (onclick) then this parameter gets set and the application is supposed to return a partial content. And without Javascript this parameter is not set. The application thus has to check for the existance of this parameter to determine whether only a partial or a full page needs to be returned. See also the examples in this modules docstring. Default: 'partial' show_if_single_page: if True the navigator will be shown even if there is only one page Default: False link_attr (optional) A dictionary of attributes that get added to A-HREF links pointing to other pages. Can be used to define a CSS style or class to customize the look of links. Example: { 'style':'border: 1px solid green' } Default: { 'class':'pager_link' } curpage_attr (optional) A dictionary of attributes that get added to the current page number in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes. Example: { 'style':'border: 3px solid blue' } Default: { 'class':'pager_curpage' } dotdot_attr (optional) A dictionary of attributes that get added to the '..' string in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes. Example: { 'style':'color: #808080' } Default: { 'class':'pager_dotdot' } onclick (optional) This paramter is a string containing optional Javascript code that will used as the 'onclick' action of each pager link. Use '%s' in your string where the URL linking to the desired page will be inserted. This can be used to enhance your pager with AJAX actions loading another page into a DOM object. Note that the URL to the destination page contains a 'partial_param' parameter so that you can distinguish between AJAX requests (just refreshing the paginated area of your page) and full requests (loading the whole new page). jQuery example: "$('#my-page-area').load('%s'); return false;" Yahoo UI example: "YAHOO.util.Connect.asyncRequest('GET','%s',{ success:function(o){YAHOO.util.Dom.get('#my-page-area').innerHTML=o.responseText;} },null); return false;" scriptaculous example: "new Ajax.Updater('#my-page-area', '%s', {asynchronous:true, evalScripts:true}); return false;" ExtJS example: "Ext.get('#my-page-area').load({url:'%s'}); return false;" Additional keyword arguments are used as arguments in the links. Otherwise the link will be created with url_for() which points to the page you are currently displaying. csªddkl}h}|iˆiƒ|iˆƒ||ˆ<||}d|ˆ<||}ˆo*ˆ|f}ti|d|d|ˆSti|d|ˆSdS(s Create a URL that links to another page using url_for(). Parameters: pagenr Number of the page that the link points to text Text to be printed in the A-HREF tag iÿÿÿÿ(turl_forithreftonclickN(troutesRNtupdateR1Rta(tpagenrttextRNt link_paramstlink_urlt partial_urltonclick_action(t page_paramRt link_attrt partial_paramRPR1(s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt _pagerlink:s       cst|idƒƒ}tˆiˆi|ƒ}tˆiˆi|ƒ}g}ˆiˆijo0ˆi|jo |iˆˆiˆiƒƒn|ˆidjo7d}ˆoti d|ˆ}n|i|ƒnxŒt ||dƒD]w}|ˆijo>d|f}ˆoti d|ˆ}n|i|ƒqïd|f}|iˆ||ƒƒqïWˆi|djo7d}ˆoti d|ˆ}n|i|ƒnˆiˆijo0|ˆijo |iˆˆiˆiƒƒnˆi |ƒS(s· Return range of linked pages (e.g. '1 2 [3] 4 5 6 7 8'). Arguments: regexp_match A "re" (regular expressions) match object containing the radius of linked pages around the current page in regexp_match.group(1) as a string This funtion is supposed to be called as a callable in re.sub. is..tcs%s( R4tgrouptmaxR:R5R>R<tappendRtspantxrangetjoin(t regexp_matchtradiust leftmost_pagetrightmost_paget nav_itemsRUtthispage(R]Rt separatort curpage_attrt dotdot_attr(s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt_rangebs8#   # iits~(\d+)~R:R<R5R;R7R=R?R8t link_firstt link_lastt link_previoust link_next(R;tretsubRtsafe_substituteR:R<R5R7R=R?R8RARBR(RtformatRZR\tshow_if_single_pageRkRPt symbol_firstt symbol_lasttsymbol_previoust symbol_nextR[RlRmR1Rntresult(( R]RZR1RR\RlRkRPR[Rms7/usr/lib/python2.6/site-packages/webhelpers/paginate.pytpagerœs.ž(I(           N(R'R(R)R*RREtFalseR~(((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyR,Ïs6z   (R)t __version__t__date__t __author__t __copyright__RtR/tstringRt ImportErrortwebhelpers.string24twebhelpers.htmlRRRRRR*RtobjectRR RR,(((s7/usr/lib/python2.6/site-packages/webhelpers/paginate.pyt\s(