Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/local/bin/python
2# encoding: utf-8
3"""
4*The pagintation for pages displaying a ticket table*
6:Author:
7 David Young
8"""
9from __future__ import division
10from builtins import range
11from past.utils import old_div
12import sys
13import os
14import re
15import math
16import khufu
18def ticket_table_pagination(
19 log,
20 totalTickets,
21 request,
22 limit,
23 previousPageStart,
24 numberOfButtonsToDisplay=5
25):
26 """ticket_table_pagination
28 **Key Arguments**
30 - ``log`` -- the logger
31 - ``totalTickets`` -- the total number of tickets to be listed in pagination
32 - ``request`` -- the request
33 - ``limit`` -- the limit of tickets to display on the page
34 - ``previousPageStart`` -- the index of the previous page's first ticket
35 - ``numberOfButtonsToDisplay`` -- the total number of pagination buttons to display on any one page (must be odd number)
38 **Return**
40 - ``pagination`` -- the pagination to be displayed
42 """
43 routename = request.matched_route.name
44 if "elementId" in request.matchdict:
45 elementId = request.matchdict["elementId"]
46 else:
47 elementId = False
49 theseParams = dict(request.params)
51 alist = ["limit", "pageStart"]
52 for i in alist:
53 if i in theseParams:
54 del theseParams[i]
56 # PLACE VARIABLES IN CORRECT FORMAT
57 limit = float(limit)
58 previousPageStart = float(previousPageStart)
59 # CALULATE OTHER VARIABLES
60 totalButtons = int(math.ceil(totalTickets * 1. / limit))
61 thisButtonIndex = int(math.ceil(old_div(previousPageStart, limit))) + 1
62 previousPageStart = int(
63 math.floor(old_div((previousPageStart - 1.), limit))) * limit
64 # CALULATE THE GENERAL RANGE OF THE BUTTONS TO DISPLAY
65 displayRangeStart = thisButtonIndex - 1 - int(old_div(numberOfButtonsToDisplay, 2))
66 displayRangeEnd = thisButtonIndex + int(old_div(numberOfButtonsToDisplay, 2))
68 numberOfButtonsBeforeThis = thisButtonIndex - 1
69 numberOfButtonsAfterThis = totalButtons - thisButtonIndex
70 if numberOfButtonsBeforeThis < old_div(numberOfButtonsToDisplay, 2):
71 displayRangeStart = 0
72 displayRangeEnd = numberOfButtonsToDisplay
73 if numberOfButtonsAfterThis <= old_div(numberOfButtonsToDisplay, 2):
74 displayRangeEnd = totalButtons
75 if numberOfButtonsBeforeThis > old_div(numberOfButtonsToDisplay, 2):
76 displayRangeStart = totalButtons - numberOfButtonsToDisplay + \
77 int(old_div(numberOfButtonsAfterThis, 2) - 0.5)
79 if displayRangeStart < 0:
80 displayRangeStart = 0
81 if displayRangeEnd > totalButtons:
82 displayRangeEnd = totalButtons
84 limit = int(limit)
85 allButtons = ""
86 pageStart = (thisButtonIndex) * limit
87 theseParams["limit"] = limit
88 theseParams["pageStart"] = pageStart
89 nextButtonUrl = request.route_path(
90 routename, elementId=elementId, _query=theseParams)
91 disabled = False
92 if thisButtonIndex == totalButtons:
93 disabled = True
94 nextButton = khufu.a(
95 content="»",
96 href=nextButtonUrl,
97 tableIndex=-1,
98 )
99 nextButton = khufu.li(
100 content=nextButton, # if a subMenu for dropdown this should be <ul>
101 span=False, # [ False | 1-12 ]
102 disabled=disabled
103 )
104 pageStart = (thisButtonIndex - 2) * limit
105 theseParams["limit"] = limit
106 theseParams["pageStart"] = pageStart
107 prevButtonUrl = request.route_path(
108 routename, elementId=elementId, _query=theseParams)
109 disabled = False
110 if thisButtonIndex == 1:
111 disabled = True
112 prevButton = khufu.a(
113 content="«",
114 href=prevButtonUrl,
115 tableIndex=-1
116 )
117 prevButton = khufu.li(
118 content=prevButton, # if a subMenu for dropdown this should be <ul>
119 span=False, # [ False | 1-12 ]
120 disabled=disabled
121 )
123 for i in range(displayRangeStart, displayRangeEnd):
124 buttonNumber = i + 1
125 disabled = False
126 if buttonNumber == thisButtonIndex:
127 disabled = True
128 pageStart = i * limit
129 theseParams["limit"] = limit
130 theseParams["pageStart"] = pageStart
131 buttonUrl = request.route_path(
132 routename, elementId=elementId, _query=theseParams)
133 link = khufu.a(
134 content=buttonNumber,
135 href=buttonUrl,
136 tableIndex=-1
137 )
138 linkListItem = khufu.li(
139 content=link, # if a subMenu for dropdown this should be <ul>
140 span=False, # [ False | 1-12 ]
141 disabled=disabled
142 )
143 allButtons = """%(allButtons)s%(linkListItem)s""" % locals()
145 allButtons = """%(prevButton)s%(allButtons)s%(nextButton)s""" % locals()
147 pagination = khufu.pagination(
148 listItems=allButtons,
149 size='default',
150 align=False
151 )
153 return pagination