app/soc/views/helper/lists.py
author Todd Larsen <tlarsen@google.com>
Sat, 22 Nov 2008 09:22:44 +0000
changeset 548 2ceb3b14349c
parent 539 e30462354e26
child 553 c0cc20b4afc9
permissions -rw-r--r--
Fix for 404 occuring when signing out during the creation of, for example, a new Document. This patch makes sure that the request.path is not altered when requesting the create page (alteration of which caused the 404). This also results in the seeds now being remembered when being forced to log in. The editPost method now correctly switches to the edit path after a successful edit. Patch by: Lennard de Rijk
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
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    26
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
    27
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    28
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
    29
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
DEF_PAGINATION = 10
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    32
MAX_PAGINATION = 100
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    33
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    34
DEF_PAGINATION_CHOICES = (
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    35
  ('10', '10 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    36
  ('25', '25 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    37
  ('50', '50 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    38
  ('100', '100 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    39
)
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
    40
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    41
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    42
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
    43
  """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
    44
  
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    45
  Args:
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    46
    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
    47
      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
    48
  """
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    49
  # 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
    50
  #   (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
    51
  return DEF_PAGINATION
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    52
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    53
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    54
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
    55
  """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
    56
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
  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
    58
    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
    59
    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
    60
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
  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
    62
    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
    63
  """
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    64
  # update offset value
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    65
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    66
    offset = int(offset)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    67
  except:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    68
    # 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
    69
    offset = 0
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    70
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
    71
  # update limit value
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    72
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    73
    limit = int(limit)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    74
  except:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    75
    # 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
    76
    limit = getPreferredListPagination()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    77
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    78
  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
    79
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
    80
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    81
def getList(request, list_data, list_templates, description, offset=0, limit=0):
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    82
  """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
    83
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
  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
    85
    request: the Django HTTP request object
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
    list_data: array of data to be displayed 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
    87
    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
    88
    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
    89
    list_templates: templates that are used when rendering 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
    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
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
   109
  if not list_data:
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
   110
    list_data = []
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   111
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
   112
  more = bool(list_data[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
   113
  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
   114
    del list_data[limit:]
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   115
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   116
  newest = ''
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   117
  next = ''
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   118
  prev = ''
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   119
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
   120
  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
   121
    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
   122
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
   123
  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
   124
    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
   125
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
   126
  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
   127
    newest = request.path + '?limit=%d' % limit
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   128
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   129
  content = {
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   130
      'data': list_data,
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   131
      'description': description,
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   132
     'main': list_templates['list_main'],
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   133
     'pagination': list_templates['list_pagination'],
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   134
     'row': list_templates['list_row'],
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   135
     'heading': list_templates['list_heading'],
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
   136
     'limit': 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
   137
     'newest': newest, 
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
   138
     'prev': prev, 
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
   139
     'next': next,
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
   140
     'first': offset+1,
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   141
     'last': len(list_data) > 1 and offset+len(list_data) or None
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   142
     }
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   143
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   144
  return content
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   145
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   146
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   147
def makePaginationForm(
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   148
  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
   149
  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
   150
  """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
   151
  
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   152
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   153
    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
   154
    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
   155
    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
   156
    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
   157
      DEF_PAGINATION_CHOICES
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   158
    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
   159
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   160
  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
   161
  
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   162
  return helper.forms.makeSelectQueryArgForm(
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   163
      request, arg_name, limit, choices)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   164
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
def makeNewPaginationChoices(limit=DEF_PAGINATION,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   167
                             choices=DEF_PAGINATION_CHOICES):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   168
  """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
   169
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   170
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   171
    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
   172
      default is DEF_PAGINATION
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   173
    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
   174
      default is DEF_PAGINATION_CHOICES
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
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   177
    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
   178
    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
   179
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   180
  # 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
   181
  new_choices = []
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   182
  
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   183
  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
   184
    items = int(pagination)
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
    if limit == items:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   187
      # 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
   188
      return choices
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   189
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   190
    if limit < items:
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   191
      # 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
   192
      # 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
   193
      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
   194
      new_choices.append(choice)
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
      # 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
   197
      # (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
   198
      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
   199
      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
   200
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   201
    # append the existing choice
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   202
    new_choices.append((pagination, label))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   203
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   204
  # 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
   205
  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
   206
  new_choices.append(choice)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   207
      
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   208
  return new_choices