source: WAeUP_SRP/branches/regebro-noskins/skins/waeup_custom/getBatchList.py @ 11154

Last change on this file since 11154 was 486, checked in by joachim, 18 years ago

svn -R propset svn:keywords "Id" .
added layout_application_/edit/create/view
and cleaned that up.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
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##
10# $Id: getBatchList.py 486 2006-09-06 10:09:39Z joachim $
11"""
12Given the desired number of colums, constructs a list of batches to render
13as much columns as necessary within a single macro.
14As well, return the page results link to display straight for the navigation
15"""
16
17from math import ceil
18from ZTUtils import Batch
19
20#
21# First constructing the batch
22#
23
24len_items = len(items)
25
26# desperately empty, no need to go further
27if not len_items:
28    return [], {}, []
29
30if max_items and max_items < len_items:
31    items = items[:max_items]
32    len_batch = max_items
33else:
34    len_batch = len_items
35
36b_start = int(context.REQUEST.get('b_start', 0))
37
38# extract the n first items in a zoomed list
39zoomed = []
40if 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
46items_per_page = float(items_per_page)
47size = int(ceil(items_per_page / columns))
48
49b1 = Batch(items, size, b_start, orphan=0)
50batches = [b1]
51
52b_next = b1
53for 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
63nb_pages = int(ceil(len_batch / items_per_page))
64
65# no more advanced arithmetics
66items_per_page = int(items_per_page)
67
68# Test if we are on the last page
69limit = b_start + items_per_page
70if  limit > len_batch:
71    limit = len_batch
72
73batch_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
82j = 0
83# for the current position in the search
84current = [0, 1]
85# list of b_start values
86pages = []
87
88# Loop over the number of pages and construct the page link
89for 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
96batch_info['pages'] = pages
97
98# if we are not at the beginning of the file
99if 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
103if current[0] != nb_pages:
104    batch_info['next'] = current[1] + items_per_page
105
106return batches, batch_info, zoomed
Note: See TracBrowser for help on using the repository browser.