app/soc/views/helper/lists.py
changeset 572 1b3e7280743a
parent 555 3cdfb42d941b
child 632 1f20cfb91e11
equal deleted inserted replaced
571:2aad108bc617 572:1b3e7280743a
    50   # TODO: eventually this limit should be a User profile preference
    50   # TODO: eventually this limit should be a User profile preference
    51   #   (stored in the site-wide User Model) preference 
    51   #   (stored in the site-wide User Model) preference 
    52   return DEF_PAGINATION
    52   return DEF_PAGINATION
    53 
    53 
    54 
    54 
    55 def cleanListParameters(offset=None, limit=None):
    55 def getLimitAndOffset(request, idx):
    56   """Converts and validates offset and limit values of the list.
    56   """Retrieves, converts and validates offset and limit values
    57 
    57 
    58   Args:
    58   Args:
    59     offset: offset in list which defines first item to return
    59     offset: offset in list which defines first item to return
    60     limit: max amount of items per page
    60     limit: max amount of items per page
    61 
    61 
    62   Returns:
    62   Returns:
    63     updated offset and limit values
    63     updated offset and limit values
    64   """
    64   """
    65   # update offset value
    65 
       
    66   offset = request.GET.get('offset_%d' % idx)
       
    67   limit = request.GET.get('limit_%d' % idx)
       
    68 
       
    69   if offset is None:
       
    70     offset = ''
       
    71 
       
    72   if limit is None:
       
    73     limit = ''
       
    74 
    66   try:
    75   try:
    67     offset = int(offset)
    76     offset = int(offset)
    68   except:
    77   except ValueError:
    69     # also catches offset=None case where offset not supplied
       
    70     offset = 0
    78     offset = 0
    71 
    79 
    72   # update limit value
       
    73   try:
    80   try:
    74     limit = int(limit)
    81     limit = int(limit)
    75   except:
    82   except ValueError:
    76     # also catches limit=None case where limit not supplied
       
    77     limit = getPreferredListPagination()
    83     limit = getPreferredListPagination()
    78 
    84 
    79   return max(0, offset), max(1, min(limit, MAX_PAGINATION))
    85   offset = max(0, offset)
    80 
    86   limit = max(1, limit)
    81 
    87   limit = min(MAX_PAGINATION, limit)
    82 def getListContent(request, params, logic, filter):
    88 
       
    89   return limit, offset
       
    90 
       
    91 
       
    92 def getListContent(request, params, logic, filter=None, idx=0):
    83   """Returns a dict with fields used for rendering lists.
    93   """Returns a dict with fields used for rendering lists.
    84 
    94 
    85   Args:
    95   Args:
    86     request: the Django HTTP request object
    96     request: the Django HTTP request object
    87     params: a dict with params for the View this list belongs to
    97     params: a dict with params for the View this list belongs to
    88     logic: the logic object for the View this list belongs to
    98     logic: the logic object for the View this list belongs to
    89     filter: a filter for this list
    99     filter: a filter for this list
       
   100     idx: the index of this list
    90 
   101 
    91   Returns:
   102   Returns:
    92     A a dictionary with the following values set:
   103     A a dictionary with the following values set:
    93 
   104 
    94     {
   105     {
   104       'first': offset of the first item in the list
   115       'first': offset of the first item in the list
   105       'last': offest of the lst item in the list
   116       'last': offest of the lst item in the list
   106     }
   117     }
   107   """
   118   """
   108 
   119 
   109   offset, limit = helper.lists.cleanListParameters(
   120   limit, offset = getLimitAndOffset(request, idx)
   110       offset=request.GET.get('offset'), limit=request.GET.get('limit'))
   121   arg_name = 'limit_%d' % idx
       
   122   pagination_form = makePaginationForm(request, limit, arg_name)
   111 
   123 
   112   # Fetch one more to see if there should be a 'next' link
   124   # Fetch one more to see if there should be a 'next' link
   113   if not filter:
   125   if not filter:
   114     data = logic.getForLimitAndOffset(limit+1, offset=offset)
   126     data = logic.getForLimitAndOffset(limit+1, offset=offset)
   115   else:
   127   else:
   123     del data[limit:]
   135     del data[limit:]
   124 
   136 
   125   newest = next = prev = ''
   137   newest = next = prev = ''
   126 
   138 
   127   if more:
   139   if more:
   128     next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
   140     next = request.path + '?offset_%d=%d&limit_%d=%d' % (
       
   141         idx, offset+limit, idx, limit)
   129 
   142 
   130   if offset > 0:
   143   if offset > 0:
   131     prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit)
   144     prev = request.path + '?offset_%d=%d&limit_%d=%d' % (
       
   145         idx, max(0, offset-limit), idx, limit)
   132 
   146 
   133   if offset > limit:
   147   if offset > limit:
   134     newest = request.path + '?limit=%d' % limit
   148     newest = request.path + '?limit_%d=%d' % (idx, limit)
   135 
   149 
   136   content = {
   150   content = {
   137       'data': data,
   151       'data': data,
   138       'first': offset+1,
   152       'first': offset+1,
   139       'last': len(data) > 1 and offset+len(data) or None,
   153       'last': len(data) > 1 and offset+len(data) or None,
   140       'logic': logic,
   154       'logic': logic,
   141       'limit': limit,
   155       'limit': limit,
   142       'newest': newest, 
   156       'newest': newest, 
   143       'next': next,
   157       'next': next,
       
   158       'pagination_form': pagination_form,
   144       'prev': prev, 
   159       'prev': prev, 
   145       }
   160       }
   146 
   161 
   147   updates = dicts.rename(params, params['list_params'])
   162   updates = dicts.rename(params, params['list_params'])
   148   content.update(updates)
   163   content.update(updates)
   149 
   164 
   150   return content
   165   return content
   151 
   166 
   152 
   167 
   153 def makePaginationForm(
   168 def makePaginationForm(
   154   request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES,
   169   request, limit, arg_name, choices=DEF_PAGINATION_CHOICES,
   155   field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
   170   field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
   156   """Returns a customized pagination limit selection form.
   171   """Returns a customized pagination limit selection form.
   157   
   172   
   158   Args:
   173   Args:
   159     request: the standard Django HTTP request object
   174     request: the standard Django HTTP request object