app/soc/views/helper/lists.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 15 Feb 2009 22:29:22 +0000
changeset 1360 f62c462037b6
parent 1308 35b75ffcbb37
child 1459 2584724403ff
permissions -rw-r--r--
Added grouping support to all forms, converted role as example Any field that has the 'group' property set be placed in the specified group. If no such proprty is set, the 'General' group will be used instead. Patch by: Sverre Rabbelier
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
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
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
     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
723
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
    26
import itertools
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
    27
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    28
from soc.logic import dicts
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 277
diff changeset
    29
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    30
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
    31
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    32
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    33
DEF_PAGINATION = 10
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    34
MAX_PAGINATION = 100
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    35
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    36
DEF_PAGINATION_CHOICES = (
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    37
  ('10', '10 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    38
  ('25', '25 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    39
  ('50', '50 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    40
  ('100', '100 items per page'),
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    41
)
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
    42
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    43
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    44
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
    45
  """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
    46
  
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    47
  Args:
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    48
    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
    49
      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
    50
  """
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    51
  # 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
    52
  #   (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
    53
  return DEF_PAGINATION
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    54
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    55
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    56
def getLimitAndOffset(request, offset_key, limit_key):
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    57
  """Retrieves, converts and validates offset and limit values
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
    58
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
  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
    60
    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
    61
    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
    62
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
  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
    64
    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
    65
  """
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    66
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    67
  offset = request.GET.get(offset_key)
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    68
  limit = request.GET.get(limit_key)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    69
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    70
  if offset is None:
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    71
    offset = ''
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    72
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    73
  if limit is None:
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    74
    limit = ''
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    75
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    76
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    77
    offset = int(offset)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    78
  except ValueError:
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
    79
    offset = 0
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    80
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    81
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    82
    limit = int(limit)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    83
  except ValueError:
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    84
    limit = getPreferredListPagination()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    85
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    86
  offset = max(0, offset)
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    87
  limit = max(1, limit)
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    88
  limit = min(MAX_PAGINATION, limit)
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    89
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    90
  return limit, offset
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    91
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
    92
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    93
def generateLinkFromGetArgs(request, offset_and_limits):
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    94
  """Constructs the get args for the url.
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    95
  """
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    96
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    97
  args = ["%s=%s" % (k, v) for k, v in offset_and_limits.iteritems()]
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    98
  link_suffix = '?' + '&'.join(args)
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
    99
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   100
  return request.path + link_suffix
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   101
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   102
651
ef6e22d463cb Remove the logic parameter from getListContent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 632
diff changeset
   103
def getListContent(request, params, filter=None, idx=0):
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   104
  """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
   105
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
  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
   107
    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
   108
    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
   109
    filter: a filter for this list
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   110
    idx: the index of 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
   111
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
  Returns:
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   113
    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
   114
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
   115
    {
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   116
      'data': list data to be displayed
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   117
      'main': url to list main template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   118
      'pagination': url to list pagination template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   119
      'row': url to list row template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   120
      '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
   121
      '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
   122
      '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
   123
      '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
   124
      '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
   125
      '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
   126
      '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
   127
    }
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
651
ef6e22d463cb Remove the logic parameter from getListContent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 632
diff changeset
   130
  logic = params['logic']
ef6e22d463cb Remove the logic parameter from getListContent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 632
diff changeset
   131
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   132
  offset_key = 'offset_%d' % idx
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   133
  limit_key = 'limit_%d' % idx
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   134
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   135
  limit, offset = getLimitAndOffset(request, offset_key, limit_key)
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   136
  pagination_form = makePaginationForm(request, limit, limit_key)
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   137
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   138
  # Fetch one more to see if there should be a 'next' link
1165
4db62684ce13 Unify 'getForFields' and 'getForLimitAndOffset'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 723
diff changeset
   139
  data = logic.getForFields(filter=filter, limit=limit+1, offset=offset)
723
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   140
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   141
  more = len(data) > limit
1165
4db62684ce13 Unify 'getForFields' and 'getForLimitAndOffset'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 723
diff changeset
   142
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
   143
  if more:
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   144
    del data[limit:]
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   145
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   146
  newest = next = prev = ''
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   147
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   148
  get_args = request.GET
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   149
  offset_and_limits = {}
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   150
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   151
  for key, value in get_args.iteritems():
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   152
    if key.startswith('offset_') or key.startswith('limit_'):
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   153
      offset_and_limits[key] = value
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   154
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
   155
  if more:
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   156
    offset_and_limits[offset_key] = offset+limit
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   157
    offset_and_limits[limit_key] = limit
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   158
    next = generateLinkFromGetArgs(request, offset_and_limits)
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   159
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
   160
  if offset > 0:
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   161
    offset_and_limits[offset_key] = max(0, offset-limit)
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   162
    offset_and_limits[limit_key] = limit
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   163
    prev = generateLinkFromGetArgs(request, offset_and_limits)
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   164
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
   165
  if offset > limit:
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   166
    del offset_and_limits[offset_key]
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   167
    offset_and_limits[limit_key] = limit
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   168
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   169
    newest = generateLinkFromGetArgs(request, offset_and_limits)
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   170
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   171
  content = {
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   172
      'data': data,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   173
      'first': offset+1,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   174
      '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
   175
      'logic': logic,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   176
      'limit': limit,
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   177
      'newest': newest, 
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   178
      'next': next,
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   179
      'pagination_form': pagination_form,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   180
      'prev': prev, 
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   181
      }
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   182
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   183
  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
   184
  content.update(updates)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   185
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   186
  return content
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   187
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   188
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   189
def makePaginationForm(
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   190
  request, limit, arg_name, choices=DEF_PAGINATION_CHOICES,
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   191
  field_name_fmt=soc.views.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
   192
  """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
   193
  
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   194
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   195
    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
   196
    limit: the initial value of the selection control
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   197
    arg_name: see soc.views.helper.forms.makeSelectQueryArgForm(); default is 'limit'
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   198
    choices: see soc.views.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
   199
      DEF_PAGINATION_CHOICES
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   200
    field_name_fmt: see soc.views.helper.forms.makeSelectQueryArgForm()
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   201
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   202
  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
   203
  
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   204
  return soc.views.helper.forms.makeSelectQueryArgForm(
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   205
      request, arg_name, limit, choices)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   206
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
def makeNewPaginationChoices(limit=DEF_PAGINATION,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   209
                             choices=DEF_PAGINATION_CHOICES):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   210
  """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
   211
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   212
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   213
    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
   214
      default is DEF_PAGINATION
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   215
    choices: see soc.views.helper.forms.makeSelectQueryArgForm();
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   216
      default is DEF_PAGINATION_CHOICES
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   217
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   218
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   219
    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
   220
    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
   221
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   222
  # 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
   223
  new_choices = []
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   224
  
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   225
  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
   226
    items = int(pagination)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   227
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   228
    if limit == items:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   229
      # 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
   230
      return choices
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   231
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   232
    if limit < items:
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   233
      # 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
   234
      # 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
   235
      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
   236
      new_choices.append(choice)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   237
      
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   238
      # 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
   239
      # (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
   240
      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
   241
      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
   242
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   243
    # append the existing choice
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   244
    new_choices.append((pagination, label))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   245
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   246
  # 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
   247
  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
   248
  new_choices.append(choice)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   249
      
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   250
  return new_choices