app/soc/views/helper/lists.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 22 Nov 2008 14:53:23 +0000
changeset 555 3cdfb42d941b
parent 553 c0cc20b4afc9
child 572 1b3e7280743a
permissions -rw-r--r--
Split the list view code up in three pieces 1. getListContents which returns the required contents dictionary 2. _list which returns the response for a specified list of contents 3. list which constructs just one content dict and passes it to _list This way it is easier to do step 1 and 2 in other code than list(), which makes it possible to display multiple list pages (by calling getListContents multiple times and passing the result to _list).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
"""Helpers used to render lists.
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    21
  '"Chen Lunpeng" <forever.clp@gmail.com>',
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
  ]
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    26
from soc.logic import dicts
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    27
from soc.views import helper
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 277
diff changeset
    28
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    29
import soc.views.helper.forms
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    30
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    31
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    32
DEF_PAGINATION = 10
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    33
MAX_PAGINATION = 100
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    34
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    35
DEF_PAGINATION_CHOICES = (
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    36
  ('10', '10 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    37
  ('25', '25 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    38
  ('50', '50 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    39
  ('100', '100 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    40
)
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    42
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    43
def getPreferredListPagination(user=None):
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    44
  """Returns User's preferred list pagination limit.
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    45
  
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    46
  Args:
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    47
    user: User entity containing the list pagination preference;
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    48
      default is None, to use the current logged-in User
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    49
  """
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    50
  # TODO: eventually this limit should be a User profile preference
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    51
  #   (stored in the site-wide User Model) preference 
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    52
  return DEF_PAGINATION
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    53
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    54
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    55
def cleanListParameters(offset=None, limit=None):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    56
  """Converts and validates offset and limit values of the list.
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
  Args:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
    offset: offset in list which defines first item to return
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
    limit: max amount of items per page
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
  Returns:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
    updated offset and limit values
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
  """
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    65
  # update offset value
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    66
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    67
    offset = int(offset)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    68
  except:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    69
    # also catches offset=None case where offset not supplied
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
    offset = 0
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    71
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
  # update limit value
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    73
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    74
    limit = int(limit)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    75
  except:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    76
    # also catches limit=None case where limit not supplied
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    77
    limit = getPreferredListPagination()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    78
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    79
  return max(0, offset), max(1, min(limit, MAX_PAGINATION))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    80
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    81
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    82
def getListContent(request, params, logic, filter):
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    83
  """Returns a dict with fields used for rendering lists.
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    85
  Args:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
    request: the Django HTTP request object
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    87
    params: a dict with params for the View this list belongs to
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    88
    logic: the logic object for the View this list belongs to
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    89
    filter: a filter for this list
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    91
  Returns:
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    92
    A a dictionary with the following values set:
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
    {
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    95
      'data': list data to be displayed
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    96
      'main': url to list main template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    97
      'pagination': url to list pagination template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    98
      'row': url to list row template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    99
      'heading': url to list heading template
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
      'limit': max amount of items per page,
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   101
      'newest': url to first page of the list 
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
      'prev': url to previous page 
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
      'next': url to next page
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
      'first': offset of the first item in the list
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
      'last': offest of the lst item in the list
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   106
    }
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   107
  """
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   108
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   109
  offset, limit = helper.lists.cleanListParameters(
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   110
      offset=request.GET.get('offset'), limit=request.GET.get('limit'))
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   111
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   112
  # Fetch one more to see if there should be a 'next' link
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   113
  if not filter:
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   114
    data = logic.getForLimitAndOffset(limit+1, offset=offset)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   115
  else:
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   116
    data = logic.getForFields(filter, limit=limit+1, offset=offset)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   117
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   118
  if not data:
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   119
    data = []
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   120
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   121
  more = bool(data[limit:])
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
  if more:
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   123
    del data[limit:]
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   124
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   125
  newest = next = prev = ''
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   126
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   127
  if more:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   128
    next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   129
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   130
  if offset > 0:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   131
    prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit)
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   132
185
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   133
  if offset > limit:
2f3bd84bb106 Add list_helpers functions that are going to be used when rendering list views like (Users List, Sponsors List etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   134
    newest = request.path + '?limit=%d' % limit
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   135
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   136
  content = {
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   137
      'data': data,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   138
      'first': offset+1,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   139
      'last': len(data) > 1 and offset+len(data) or None,
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   140
      'logic': logic,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   141
      'limit': limit,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   142
      'newest': newest, 
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   143
      'next': next,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   144
      'prev': prev, 
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   145
      }
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   146
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   147
  updates = dicts.rename(params, params['list_params'])
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   148
  content.update(updates)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   149
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   150
  return content
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   151
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   152
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   153
def makePaginationForm(
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   154
  request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES,
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   155
  field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   156
  """Returns a customized pagination limit selection form.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   157
  
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   158
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   159
    request: the standard Django HTTP request object
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   160
    limit: the initial value of the selection control
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   161
    arg_name: see helper.forms.makeSelectQueryArgForm(); default is 'limit'
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   162
    choices: see helper.forms.makeSelectQueryArgForm(); default is
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   163
      DEF_PAGINATION_CHOICES
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   164
    field_name_fmt: see helper.forms.makeSelectQueryArgForm()
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   165
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   166
  choices = makeNewPaginationChoices(limit=limit, choices=choices)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   167
  
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   168
  return helper.forms.makeSelectQueryArgForm(
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   169
      request, arg_name, limit, choices)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   170
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   171
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   172
def makeNewPaginationChoices(limit=DEF_PAGINATION,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   173
                             choices=DEF_PAGINATION_CHOICES):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   174
  """Updates the pagination limit selection form.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   175
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   176
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   177
    limit: the initial value of the selection control;
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   178
      default is DEF_PAGINATION
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   179
    choices: see helper.forms.makeSelectQueryArgForm();
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   180
      default is DEF_PAGINATION_CHOICES
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   181
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   182
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   183
    a new pagination choices list if limit is not in
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   184
    DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   185
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   186
  # determine where to insert the new limit into choices
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   187
  new_choices = []
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   188
  
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   189
  for index, (pagination, label) in enumerate(choices):
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   190
    items = int(pagination)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   191
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   192
    if limit == items:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   193
      # limit is already present, so just return existing choices
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   194
      return choices
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   195
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   196
    if limit < items:
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   197
      # limit needs to be inserted before the current pagination,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   198
      # so assemble a new choice tuple and append it 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   199
      choice = (str(limit), '%s items per page' % limit)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   200
      new_choices.append(choice)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   201
      
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   202
      # append the remainder of the original list and exit early
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   203
      # (avoiding unnecessary remaining type conversions, etc.)
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   204
      new_choices.extend(choices[index:])
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   205
      return new_choices
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   206
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   207
    # append the existing choice
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   208
    new_choices.append((pagination, label))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   209
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   210
  # new choice must go last, past all other existing choices
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   211
  choice = (str(limit), '%s items per page' % limit)
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   212
  new_choices.append(choice)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   213
      
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   214
  return new_choices