app/soc/views/helper/lists.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Tue, 03 Nov 2009 00:50:23 +0100
changeset 3075 1e78db95e38a
parent 2855 ec2ed1571e3a
permissions -rw-r--r--
getListContentForData function added to lists helper. It allows to manually set a list of entities which is to be displayed on the view. Previously, the only function was getListContent, but it retrived data on its own by a single query. The new function can be useful whenever it is impossible or very awkward to obtain entities in such a way (for example more sophisticated SQL statements). Additionally, the getListContent function is reconstructed so that it collects the data first and then calls getListContentForData.
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__ = [
3075
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
    21
  '"Daniel Hans" <daniel.m.hans@gmail.com>',
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    22
  '"Chen Lunpeng" <forever.clp@gmail.com>',
2855
ec2ed1571e3a Hook up prefetching in the view (helper)
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2363
diff changeset
    23
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
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
    24
  '"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
    25
  ]
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
    26
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
    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
1802
407000879987 Allow developers to specify a higher limit for a list view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1692
diff changeset
    29
from soc.logic.models.user import logic as user_logic
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 277
diff changeset
    30
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    31
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
    32
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    33
1810
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
    34
DEF_DEFAULT_PAGINATION = 50
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
    35
DEF_MAX_PAGINATION = 100
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
    36
DEF_MAX_DEV_PAGINATION = 1000
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    37
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    38
DEF_PAGINATION_CHOICES = [
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    39
    ('10', '10 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    40
    ('25', '25 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    41
    ('50', '50 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    42
    ('100', '100 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    43
]
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    44
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    45
DEF_DEVELOPER_CHOICES = [
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    46
    ('500', '500 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    47
    ('1000', '1000 items per page'),
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
    48
    ]
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
    49
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    50
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    51
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
    52
  """Returns User's preferred list pagination limit.
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
    53
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    54
  Args:
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    55
    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
    56
      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
    57
  """
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    58
  # TODO: eventually this limit should be a User profile preference
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
    59
  #   (stored in the site-wide User Model) preference
1810
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
    60
  return DEF_DEFAULT_PAGINATION
228
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    61
2204287da374 Add placeholder for future (to-be-implemented) getPreferredListPagination()
Todd Larsen <tlarsen@google.com>
parents: 185
diff changeset
    62
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    63
OFFSET_KEY = 'offset_%d'
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    64
LIMIT_KEY = 'limit_%d'
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    65
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    66
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    67
def makeOffsetKey(limit_idx):
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    68
  return OFFSET_KEY % limit_idx
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    69
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    70
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    71
def makeLimitKey(limit_idx):
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    72
  return LIMIT_KEY % limit_idx
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    73
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    74
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    75
def getListParameters(request, list_index):
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    76
  """Retrieves, converts and validates values for one 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
    77
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
    78
  Args:
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    79
    list_index, int: which list to get the values for.
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    80
      (there may be multiple lists on one page, which are multiplexed
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    81
       by an integer.)
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
    82
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
  Returns:
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    84
    a dictionary of str -> str.  field name -> field value.
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
    85
  """
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    86
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    87
  offset = request.GET.get(makeOffsetKey(list_index))
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
    88
  limit = request.GET.get(makeLimitKey(list_index))
572
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
  if offset is None:
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    91
    offset = ''
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    92
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    93
  if limit is None:
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    94
    limit = ''
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    95
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    96
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
    97
    offset = int(offset)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
    98
  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
    99
    offset = 0
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   100
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   101
  try:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   102
    limit = int(limit)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   103
  except ValueError:
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   104
    limit = getPreferredListPagination()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   105
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   106
  offset = max(0, offset)
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   107
  limit = max(1, limit)
1802
407000879987 Allow developers to specify a higher limit for a list view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1692
diff changeset
   108
1810
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   109
  if user_logic.isDeveloper():
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   110
    limit = min(DEF_MAX_DEV_PAGINATION, limit)
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   111
  else:
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   112
    limit = min(DEF_MAX_PAGINATION, limit)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   113
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   114
  return dict(limit=limit, offset=offset)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   115
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
   116
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   117
def generateLinkFromGetArgs(request, offset_and_limits):
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   118
  """Constructs the get args for the url.
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   119
  """
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   120
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   121
  args = ["%s=%s" % (k, v) for k, v in offset_and_limits.iteritems()]
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   122
  link_suffix = '?' + '&'.join(args)
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   123
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   124
  return request.path + link_suffix
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   125
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   126
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   127
def generateLinkForRequest(request, base_params, updated_params):
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   128
  """Create a link to the same page as request but with different params
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   129
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   130
  Params:
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   131
    request: the request for the page
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   132
    base_params: the base parameters
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   133
    updated_params: the parameters to update
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   134
  """
3075
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   135
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   136
  params = base_params.copy()
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   137
  params.update(updated_params)
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   138
  return generateLinkFromGetArgs(request, params)
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
   139
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
   140
1667
95bc81b4cd5c Updated list to use the new order in getForFields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1604
diff changeset
   141
def getListContent(request, params, filter=None, order=None,
2855
ec2ed1571e3a Hook up prefetching in the view (helper)
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2363
diff changeset
   142
                   idx=0, need_content=False, prefetch=None):
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   143
  """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
   144
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
   145
  TODO(dbentley): we need better terminology. List, in this context, can have
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   146
    one of two meanings.
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   147
    Meaning 1:  the underlying list, which may be very large.
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   148
    Meaning 2:  the returned list, which is at most 'limit' items.
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   149
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
   150
  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
   151
    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
   152
    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
   153
    filter: a filter for this list
1667
95bc81b4cd5c Updated list to use the new order in getForFields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1604
diff changeset
   154
    order: the order which should be used for the list (in getForFields format)
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   155
    idx: the index of this list
1604
297444daac68 Two docstring fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1459
diff changeset
   156
    need_content: iff True will return None if there is no data
2855
ec2ed1571e3a Hook up prefetching in the view (helper)
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2363
diff changeset
   157
    prefetch: the fields of the data that should be pre-fetched
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
   158
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
   159
  Returns:
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   160
    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
   161
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
   162
    {
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   163
      'data': list data to be displayed
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   164
      'main': url to list main template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   165
      'pagination': url to list pagination template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   166
      'row': url to list row template
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   167
      '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
   168
      'limit': max amount of items per page,
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   169
      'newest': url to first page of the list
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   170
      'prev': url to previous page
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
   171
      '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
   172
      'first': offset of the first item in the list
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
   173
      'last': offset of the last item in 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
   174
    }
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   175
  """
3075
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   176
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   177
  list_params = getListParameters(request, idx)
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   178
  limit, offset = list_params['limit'], list_params['offset']
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   179
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   180
  # Fetch one more to see if there should be a 'next' link
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   181
  logic = params['logic']
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   182
  data = logic.getForFields(filter=filter, limit=limit+1, offset=offset,
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   183
                            order=order, prefetch=prefetch)
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   184
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   185
  return getListContentForData(request, params, data=data, idx=idx,
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   186
       limit=limit, offset=offset, need_content=need_content)
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   187
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   188
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   189
def getListContentForData(request, params, data=None, idx=0,
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   190
                          limit=DEF_DEFAULT_PAGINATION, offset=0,
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   191
                          need_content=False):
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   192
  """Returns a dict with fields used for rendering lists.
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   193
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   194
  TODO(dbentley): we need better terminology. List, in this context, can have
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   195
    one of two meanings.
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   196
    Meaning 1:  the underlying list, which may be very large.
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   197
    Meaning 2:  the returned list, which is at most 'limit' items.
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   198
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   199
  Args:
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   200
    request: the Django HTTP request object
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   201
    params: a dict with params for the View this list belongs to
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   202
    data: list of entities to fill the list with
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   203
    idx: the index of this list
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   204
    limit: number of entities on a single list page
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   205
    offset: length of offset of the entities
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   206
    need_content: iff True will return None if there is no data
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   207
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   208
  Returns:
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   209
    See getListContent() for details.
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   210
  """
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   211
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   212
  if need_content and not data:
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   213
    return None
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   214
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   215
  # TODO(dbentley): this appears to be unnecessary indirection,
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   216
  # as we only use this logic for getForFields, which is never overridden
651
ef6e22d463cb Remove the logic parameter from getListContent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 632
diff changeset
   217
  logic = params['logic']
ef6e22d463cb Remove the logic parameter from getListContent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 632
diff changeset
   218
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   219
  limit_key, offset_key = makeLimitKey(idx), makeOffsetKey(idx)
1166
558bd62ee9d4 Fix get args construction when there are multiple lists on the page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   220
3075
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   221
  pagination_form = makePaginationForm(request, limit, limit_key)
1459
2584724403ff Make it possible not to generate empty lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   222
723
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   223
  more = len(data) > limit
1807
1f8cde169f32 Add an export link to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1802
diff changeset
   224
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
   225
  if more:
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   226
    del data[limit:]
2316
3156760b4d26 Use key_name instead of link_id
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2313
diff changeset
   227
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   228
  newest = next = prev = export_link = ''
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   229
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   230
  base_params = dict(i for i in request.GET.iteritems() if
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   231
                     i[0].startswith('offset_') or i[0].startswith('limit_'))
2166
c9c7c6111988 Last step of refactoring before adding support for > 1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2108
diff changeset
   232
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   233
  if params.get('list_key_order'):
2363
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   234
    export_link = generateLinkForRequest(request, base_params, {'export': idx})
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   235
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   236
  if more:
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   237
    # TODO(dbentley): here we need to implement a new field "last_key"
3075
1e78db95e38a getListContentForData function added to lists helper.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2855
diff changeset
   238
    next = generateLinkForRequest(request, base_params,
2363
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   239
                                  {offset_key: offset + limit,
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   240
                                   limit_key: limit})
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   241
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   242
  if offset > 0:
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   243
    # TODO(dbentley): here we need to implement previous in the good way.
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   244
    prev = generateLinkForRequest(request, base_params,
2363
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   245
                                  {offset_key: max(0, offset-limit),
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   246
                                   limit_key: limit})
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   247
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   248
  if offset > limit:
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   249
    # Having a link to the first doesn't make sense on the first page (we're on
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   250
    # it).  It also doesn't make sense on the second page (because the first
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   251
    # page is the previous page).
2313
c39a81bce1bd Use offset_linkid instead of offset to scan >1000 entities.
Daniel Bentley <dbentley@google.com>
parents: 2312
diff changeset
   252
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   253
    # NOTE(dbentley): I personally disagree that it's simpler to do that way,
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   254
    # because sometimes you want to go to the first page without having to
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   255
    # consider what page you're on now.
2363
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   256
    newest = generateLinkForRequest(request, base_params, {offset_key: 0,
9046c80a9137 Style fixes and wrong function call fix in soc.views.helper.lists module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2358
diff changeset
   257
                                                           limit_key: limit})
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   258
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   259
  content = {
2108
286aa6528e84 Properly handle exports when there are multiple lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
   260
      'idx': idx,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   261
      'data': data,
1807
1f8cde169f32 Add an export link to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1802
diff changeset
   262
      'export': export_link,
2354
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   263
      'first': offset+1,
4cc66ab098e8 remove accidental head
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2316
diff changeset
   264
      '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
   265
      'logic': logic,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   266
      'limit': limit,
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   267
      'newest': newest,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   268
      'next': next,
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   269
      'pagination_form': pagination_form,
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   270
      'prev': prev,
553
c0cc20b4afc9 Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 539
diff changeset
   271
      }
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   272
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   273
  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
   274
  content.update(updates)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
   275
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   276
  return content
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   277
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   278
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   279
def makePaginationForm(
572
1b3e7280743a Made list pagination part of Lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 555
diff changeset
   280
  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
   281
  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
   282
  """Returns a customized pagination limit selection form.
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   283
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   284
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   285
    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
   286
    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
   287
    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
   288
    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
   289
      DEF_PAGINATION_CHOICES
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   290
    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
   291
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   292
  choices = makeNewPaginationChoices(limit=limit, choices=choices)
2049
d9adbaf1c30f Some fixes to lists helper to reduce mutation.
Daniel Bentley <dbentley@google.com>
parents: 1821
diff changeset
   293
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   294
  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
   295
      request, arg_name, limit, choices)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   296
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   297
1810
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   298
def makeNewPaginationChoices(limit=DEF_DEFAULT_PAGINATION,
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   299
                             choices=DEF_PAGINATION_CHOICES):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   300
  """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
   301
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   302
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   303
    limit: the initial value of the selection control;
1810
ec0bae3632bb Adhere to the DEF_... pattern for constants in lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1807
diff changeset
   304
      default is DEF_DEFAULT_PAGINATION
632
1f20cfb91e11 Remove unused imports from some modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 572
diff changeset
   305
    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
   306
      default is DEF_PAGINATION_CHOICES
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   307
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   308
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   309
    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
   310
    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
   311
  """
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   312
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   313
  new_choices = []
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   314
  new_choice = (str(limit), '%s items per page' % limit)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   315
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   316
  new_choices.append(new_choice)
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   317
  new_choices.extend(choices)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   318
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   319
  if user_logic.isDeveloper():
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   320
    new_choices.extend(DEF_DEVELOPER_CHOICES)
277
85f7d537e4d7 Speed up average case by appending remainder of original choices list unchanged
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   321
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   322
  new_choices = set(new_choices)
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 228
diff changeset
   323
1811
75d3c1384736 Greatly simplify makeNewPaginationChoices
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1810
diff changeset
   324
  return sorted(new_choices, key=lambda (x, y): int(x))