[432] | 1 | ## Script (Python) "getBatchList" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##parameters=items=[], columns=1, items_per_page=10, zoom=0, max_items=400 |
---|
| 8 | ##title= |
---|
| 9 | ## |
---|
[486] | 10 | # $Id: getBatchList.py 486 2006-09-06 10:09:39Z joachim $ |
---|
[432] | 11 | """ |
---|
| 12 | Given the desired number of colums, constructs a list of batches to render |
---|
| 13 | as much columns as necessary within a single macro. |
---|
| 14 | As well, return the page results link to display straight for the navigation |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | from math import ceil |
---|
| 18 | from ZTUtils import Batch |
---|
| 19 | |
---|
| 20 | # |
---|
| 21 | # First constructing the batch |
---|
| 22 | # |
---|
| 23 | |
---|
| 24 | len_items = len(items) |
---|
| 25 | |
---|
| 26 | # desperately empty, no need to go further |
---|
| 27 | if not len_items: |
---|
| 28 | return [], {}, [] |
---|
| 29 | |
---|
| 30 | if max_items and max_items < len_items: |
---|
| 31 | items = items[:max_items] |
---|
| 32 | len_batch = max_items |
---|
| 33 | else: |
---|
| 34 | len_batch = len_items |
---|
| 35 | |
---|
| 36 | b_start = int(context.REQUEST.get('b_start', 0)) |
---|
| 37 | |
---|
| 38 | # extract the n first items in a zoomed list |
---|
| 39 | zoomed = [] |
---|
| 40 | if not b_start and zoom: |
---|
| 41 | zoom = int(zoom) |
---|
| 42 | zoomed = Batch(items[:zoom], zoom, 0) |
---|
| 43 | # deal with items left |
---|
| 44 | items = items[zoom:] |
---|
| 45 | |
---|
| 46 | items_per_page = float(items_per_page) |
---|
| 47 | size = int(ceil(items_per_page / columns)) |
---|
| 48 | |
---|
| 49 | b1 = Batch(items, size, b_start, orphan=0) |
---|
| 50 | batches = [b1] |
---|
| 51 | |
---|
| 52 | b_next = b1 |
---|
| 53 | for c in range(columns - 1): |
---|
| 54 | if b_next.next: |
---|
| 55 | b_next = b_next.next |
---|
| 56 | batches.append(b_next) |
---|
| 57 | |
---|
| 58 | # |
---|
| 59 | # Now the page results parameters |
---|
| 60 | # |
---|
| 61 | |
---|
| 62 | # Calculate the number of pages |
---|
| 63 | nb_pages = int(ceil(len_batch / items_per_page)) |
---|
| 64 | |
---|
| 65 | # no more advanced arithmetics |
---|
| 66 | items_per_page = int(items_per_page) |
---|
| 67 | |
---|
| 68 | # Test if we are on the last page |
---|
| 69 | limit = b_start + items_per_page |
---|
| 70 | if limit > len_batch: |
---|
| 71 | limit = len_batch |
---|
| 72 | |
---|
| 73 | batch_info = {'nb_pages': nb_pages, |
---|
| 74 | 'start': b_start + 1, |
---|
| 75 | 'limit': limit, |
---|
| 76 | 'length': len_items, |
---|
| 77 | 'previous': None, |
---|
| 78 | 'next': None, |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | # for the nb of items |
---|
| 82 | j = 0 |
---|
| 83 | # for the current position in the search |
---|
| 84 | current = [0, 1] |
---|
| 85 | # list of b_start values |
---|
| 86 | pages = [] |
---|
| 87 | |
---|
| 88 | # Loop over the number of pages and construct the page link |
---|
| 89 | for i in range(nb_pages): |
---|
| 90 | pages.append(j) |
---|
| 91 | if b_start == j: |
---|
| 92 | current = [i + 1, j] |
---|
| 93 | j += items_per_page |
---|
| 94 | |
---|
| 95 | # list of b_start to other pages |
---|
| 96 | batch_info['pages'] = pages |
---|
| 97 | |
---|
| 98 | # if we are not at the beginning of the file |
---|
| 99 | if current[0] > 1: |
---|
| 100 | batch_info['previous'] = current[1] - items_per_page |
---|
| 101 | |
---|
| 102 | # Adding the next link if we are not at the end of the list |
---|
| 103 | if current[0] != nb_pages: |
---|
| 104 | batch_info['next'] = current[1] + items_per_page |
---|
| 105 | |
---|
| 106 | return batches, batch_info, zoomed |
---|