33 |
33 |
34 DEF_DEFAULT_PAGINATION = 50 |
34 DEF_DEFAULT_PAGINATION = 50 |
35 DEF_MAX_PAGINATION = 100 |
35 DEF_MAX_PAGINATION = 100 |
36 DEF_MAX_DEV_PAGINATION = 1000 |
36 DEF_MAX_DEV_PAGINATION = 1000 |
37 |
37 |
38 DEF_PAGINATION_CHOICES = ( |
38 DEF_PAGINATION_CHOICES = [ |
39 ('10', '10 items per page'), |
39 ('10', '10 items per page'), |
40 ('25', '25 items per page'), |
40 ('25', '25 items per page'), |
41 ('50', '50 items per page'), |
41 ('50', '50 items per page'), |
42 ('100', '100 items per page'), |
42 ('100', '100 items per page'), |
43 ) |
43 ] |
|
44 |
|
45 DEF_DEVELOPER_CHOICES = [ |
|
46 ('500', '500 items per page'), |
|
47 ('1000', '1000 items per page'), |
|
48 ] |
44 |
49 |
45 |
50 |
46 def getPreferredListPagination(user=None): |
51 def getPreferredListPagination(user=None): |
47 """Returns User's preferred list pagination limit. |
52 """Returns User's preferred list pagination limit. |
48 |
53 |
235 |
240 |
236 Returns: |
241 Returns: |
237 a new pagination choices list if limit is not in |
242 a new pagination choices list if limit is not in |
238 DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise |
243 DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise |
239 """ |
244 """ |
240 # determine where to insert the new limit into choices |
245 |
241 new_choices = [] |
246 new_choices = [] |
242 |
247 new_choice = (str(limit), '%s items per page' % limit) |
243 for index, (pagination, label) in enumerate(choices): |
248 |
244 items = int(pagination) |
249 new_choices.append(new_choice) |
245 |
250 new_choices.extend(choices) |
246 if limit == items: |
251 |
247 # limit is already present, so just return existing choices |
252 if user_logic.isDeveloper(): |
248 return choices |
253 new_choices.extend(DEF_DEVELOPER_CHOICES) |
249 |
254 |
250 if limit < items: |
255 new_choices = set(new_choices) |
251 # limit needs to be inserted before the current pagination, |
256 |
252 # so assemble a new choice tuple and append it |
257 return sorted(new_choices, key=lambda (x, y): int(x)) |
253 choice = (str(limit), '%s items per page' % limit) |
|
254 new_choices.append(choice) |
|
255 |
|
256 # append the remainder of the original list and exit early |
|
257 # (avoiding unnecessary remaining type conversions, etc.) |
|
258 new_choices.extend(choices[index:]) |
|
259 return new_choices |
|
260 |
|
261 # append the existing choice |
|
262 new_choices.append((pagination, label)) |
|
263 |
|
264 # new choice must go last, past all other existing choices |
|
265 choice = (str(limit), '%s items per page' % limit) |
|
266 new_choices.append(choice) |
|
267 |
|
268 return new_choices |
|