185 a new pagination choices list if limit is not in |
185 a new pagination choices list if limit is not in |
186 DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise |
186 DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise |
187 """ |
187 """ |
188 # determine where to insert the new limit into choices |
188 # determine where to insert the new limit into choices |
189 new_choices = [] |
189 new_choices = [] |
190 inserted = False |
190 |
191 |
191 for index, (pagination, label) in enumerate(choices): |
192 for pagination, label in choices: |
|
193 items = int(pagination) |
192 items = int(pagination) |
194 |
193 |
195 if limit == items: |
194 if limit == items: |
196 # limit is already present, so just return existing choices |
195 # limit is already present, so just return existing choices |
197 return choices |
196 return choices |
198 |
197 |
199 if (not inserted) and (limit < items): |
198 if limit < items: |
200 # limit needs to be inserted before the current pagination, |
199 # limit needs to be inserted before the current pagination, |
201 # so assemble a new choice tuple and append it |
200 # so assemble a new choice tuple and append it |
202 choice = (str(limit), '%s items per page' % limit) |
201 choice = (str(limit), '%s items per page' % limit) |
203 new_choices.append(choice) |
202 new_choices.append(choice) |
204 inserted = True |
|
205 |
203 |
|
204 # append the remainder of the original list and exit early |
|
205 # (avoiding unnecessary remaining type conversions, etc.) |
|
206 new_choices.extend(choices[index:]) |
|
207 return new_choices |
|
208 |
206 # append the existing choice |
209 # append the existing choice |
207 new_choices.append((pagination, label)) |
210 new_choices.append((pagination, label)) |
208 |
211 |
209 if not inserted: |
212 # new choice must go last, past all other existing choices |
210 # new choice must go last, past all other existing choices |
213 choice = (str(limit), '%s items per page' % limit) |
211 choice = (str(limit), '%s items per page' % limit) |
214 new_choices.append(choice) |
212 new_choices.append(choice) |
|
213 |
215 |
214 return new_choices |
216 return new_choices |