app/soc/views/helpers/list_helpers.py
changeset 265 3c2994f3b85f
parent 228 2204287da374
equal deleted inserted replaced
264:97b60788cb9a 265:3c2994f3b85f
    16 
    16 
    17 """Helpers used to render lists.
    17 """Helpers used to render lists.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
       
    21   '"Chen Lunpeng" <forever.clp@gmail.com>',
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    22   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    22   ]
    23   ]
    23 
    24 
    24 
    25 
    25 DEF_LIMIT = 10
    26 from soc.views.helpers import forms_helpers
       
    27 
       
    28 
       
    29 DEF_PAGINATION = 10
       
    30 MAX_PAGINATION = 100
       
    31 
       
    32 DEF_PAGINATION_CHOICES = (
       
    33   ('10', '10 items per page'),
       
    34   ('25', '25 items per page'),
       
    35   ('50', '50 items per page'),
       
    36   ('100', '100 items per page'),
       
    37 )
    26 
    38 
    27 
    39 
    28 def getPreferredListPagination(user=None):
    40 def getPreferredListPagination(user=None):
    29     """Returns User's preferred list pagination limit.
    41     """Returns User's preferred list pagination limit.
    30     
    42     
    32       user: User entity containing the list pagination preference;
    44       user: User entity containing the list pagination preference;
    33         default is None, to use the current logged-in User
    45         default is None, to use the current logged-in User
    34     """
    46     """
    35     # TODO: eventually this limit should be a User profile preference
    47     # TODO: eventually this limit should be a User profile preference
    36     #   (stored in the site-wide User Model) preference 
    48     #   (stored in the site-wide User Model) preference 
    37     return DEF_LIMIT
    49     return DEF_PAGINATION
    38 
    50 
    39 
    51 
    40 def getListParemeters(offset=None, limit=None):
    52 def cleanListParameters(offset=None, limit=None):
    41   """Updates and validates offset and limit values of the list.
    53   """Converts and validates offset and limit values of the list.
    42 
    54 
    43   Args:
    55   Args:
    44     offset: offset in list which defines first item to return
    56     offset: offset in list which defines first item to return
    45     limit: max amount of items per page
    57     limit: max amount of items per page
    46 
    58 
    47   Returns:
    59   Returns:
    48     updated offset and limit values
    60     updated offset and limit values
    49   """
    61   """
    50   # update offset value 
    62   # update offset value
    51   if offset:
    63   try:
    52     try:
    64     offset = int(offset)
    53       offset = int(offset)
    65   except:
    54     except:
    66     # also catches offset=None case where offset not supplied
    55       offset = 0
       
    56     else:
       
    57       offset = max(0, offset)
       
    58   else:
       
    59     offset = 0
    67     offset = 0
    60   
    68 
    61   # update limit value
    69   # update limit value
    62   if limit:
    70   try:
    63     try:
    71     limit = int(limit)
    64       limit = int(limit)
    72   except:
    65     except:
    73     # also catches limit=None case where limit not supplied
    66       limit = DEF_LIMIT
    74     limit = getPreferredListPagination()
    67     else:
    75 
    68       limit = max(1, min(limit, 100))
    76   return max(0, offset), max(1, min(limit, MAX_PAGINATION))
    69   else:
    77 
    70     limit = DEF_LIMIT
       
    71   
       
    72   return offset, limit
       
    73 
    78 
    74 DEF_LIST_TEMPLATES = {'list_main': 'soc/list/list_main.html',
    79 DEF_LIST_TEMPLATES = {'list_main': 'soc/list/list_main.html',
    75                       'list_pagination': 'soc/list/list_pagination.html',
    80                       'list_pagination': 'soc/list/list_pagination.html',
    76                       'list_row': 'soc/list/list_row.html',
    81                       'list_row': 'soc/list/list_row.html',
    77                       'list_heading': 'soc/list/list_heading.html'}
    82                       'list_heading': 'soc/list/list_heading.html'}
   142      'next': next,
   147      'next': next,
   143      'first': offset+1,
   148      'first': offset+1,
   144      'last': len(list_data) > 1 and offset+len(list_data) or None})
   149      'last': len(list_data) > 1 and offset+len(list_data) or None})
   145   
   150   
   146   return context
   151   return context
       
   152 
       
   153 
       
   154 def makePaginationForm(
       
   155   request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES,
       
   156   field_name_fmt=forms_helpers.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
       
   157   """Returns a customized pagination limit selection form.
       
   158   
       
   159   Args:
       
   160     request: the standard Django HTTP request object
       
   161     limit: the initial value of the selection control
       
   162     arg_name: see forms_helpers.makeSelectQueryArgForm(); default is 'limit'
       
   163     choices: see forms_helpers.makeSelectQueryArgForm(); default is
       
   164       DEF_PAGINATION_CHOICES
       
   165     field_name_fmt: see forms_helpers.makeSelectQueryArgForm()
       
   166   """
       
   167   choices = makeNewPaginationChoices(limit=limit, choices=choices)
       
   168   
       
   169   return forms_helpers.makeSelectQueryArgForm(
       
   170       request, arg_name, limit, choices)
       
   171 
       
   172 
       
   173 def makeNewPaginationChoices(limit=DEF_PAGINATION,
       
   174                              choices=DEF_PAGINATION_CHOICES):
       
   175   """Updates the pagination limit selection form.
       
   176 
       
   177   Args:
       
   178     limit: the initial value of the selection control;
       
   179       default is DEF_PAGINATION
       
   180     choices: see forms_helpers.makeSelectQueryArgForm();
       
   181       default is DEF_PAGINATION_CHOICES
       
   182 
       
   183   Returns:
       
   184     a new pagination choices list if limit is not in
       
   185     DEF_PAGINATION_CHOICES, or DEF_PAGINATION_CHOICES otherwise
       
   186   """
       
   187   # determine where to insert the new limit into choices
       
   188   new_choices = []
       
   189   inserted = False
       
   190   
       
   191   for pagination, label in choices:
       
   192     items = int(pagination)
       
   193 
       
   194     if limit == items:
       
   195       # limit is already present, so just return existing choices
       
   196       return choices
       
   197 
       
   198     if (not inserted) and (limit < items):
       
   199       # limit needs to be inserted before the current pagination,
       
   200       # so assemble a new choice tuple and append it 
       
   201       choice = (str(limit), '%s items per page' % limit)
       
   202       new_choices.append(choice)
       
   203       inserted = True
       
   204       
       
   205     # append the existing choice
       
   206     new_choices.append((pagination, label))
       
   207 
       
   208   if not inserted:
       
   209     # new choice must go last, past all other existing choices
       
   210     choice = (str(limit), '%s items per page' % limit)
       
   211     new_choices.append(choice)
       
   212       
       
   213   return new_choices