app/soc/views/helper/lists.py
changeset 3075 1e78db95e38a
parent 2855 ec2ed1571e3a
equal deleted inserted replaced
3074:ebda36efbd61 3075:1e78db95e38a
    16 
    16 
    17 """Helpers used to render lists.
    17 """Helpers used to render lists.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
       
    21   '"Daniel Hans" <daniel.m.hans@gmail.com>',
    21   '"Chen Lunpeng" <forever.clp@gmail.com>',
    22   '"Chen Lunpeng" <forever.clp@gmail.com>',
    22   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    23   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    23   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    24   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    24   ]
    25   ]
    25 
    26 
   129   Params:
   130   Params:
   130     request: the request for the page
   131     request: the request for the page
   131     base_params: the base parameters
   132     base_params: the base parameters
   132     updated_params: the parameters to update
   133     updated_params: the parameters to update
   133   """
   134   """
       
   135 
   134   params = base_params.copy()
   136   params = base_params.copy()
   135   params.update(updated_params)
   137   params.update(updated_params)
   136   return generateLinkFromGetArgs(request, params)
   138   return generateLinkFromGetArgs(request, params)
   137 
   139 
   138 
   140 
   169       'next': url to next page
   171       'next': url to next page
   170       'first': offset of the first item in the list
   172       'first': offset of the first item in the list
   171       'last': offset of the last item in the list
   173       'last': offset of the last item in the list
   172     }
   174     }
   173   """
   175   """
       
   176 
       
   177   list_params = getListParameters(request, idx)
       
   178   limit, offset = list_params['limit'], list_params['offset']
       
   179 
       
   180   # Fetch one more to see if there should be a 'next' link
       
   181   logic = params['logic']
       
   182   data = logic.getForFields(filter=filter, limit=limit+1, offset=offset,
       
   183                             order=order, prefetch=prefetch)
       
   184 
       
   185   return getListContentForData(request, params, data=data, idx=idx,
       
   186        limit=limit, offset=offset, need_content=need_content)
       
   187 
       
   188 
       
   189 def getListContentForData(request, params, data=None, idx=0,
       
   190                           limit=DEF_DEFAULT_PAGINATION, offset=0,
       
   191                           need_content=False):
       
   192   """Returns a dict with fields used for rendering lists.
       
   193 
       
   194   TODO(dbentley): we need better terminology. List, in this context, can have
       
   195     one of two meanings.
       
   196     Meaning 1:  the underlying list, which may be very large.
       
   197     Meaning 2:  the returned list, which is at most 'limit' items.
       
   198 
       
   199   Args:
       
   200     request: the Django HTTP request object
       
   201     params: a dict with params for the View this list belongs to
       
   202     data: list of entities to fill the list with
       
   203     idx: the index of this list
       
   204     limit: number of entities on a single list page
       
   205     offset: length of offset of the entities
       
   206     need_content: iff True will return None if there is no data
       
   207 
       
   208   Returns:
       
   209     See getListContent() for details.
       
   210   """
       
   211 
       
   212   if need_content and not data:
       
   213     return None
       
   214 
   174   # TODO(dbentley): this appears to be unnecessary indirection,
   215   # TODO(dbentley): this appears to be unnecessary indirection,
   175   # as we only use this logic for getForFields, which is never overridden
   216   # as we only use this logic for getForFields, which is never overridden
   176   logic = params['logic']
   217   logic = params['logic']
   177 
   218 
   178   limit_key, offset_key = makeLimitKey(idx), makeOffsetKey(idx)
   219   limit_key, offset_key = makeLimitKey(idx), makeOffsetKey(idx)
   179 
   220 
   180   list_params = getListParameters(request, idx)
   221   pagination_form = makePaginationForm(request, limit, limit_key)
   181   limit, offset = list_params['limit'], list_params['offset']
       
   182   pagination_form = makePaginationForm(request, list_params['limit'],
       
   183                                        limit_key)
       
   184 
       
   185   # Fetch one more to see if there should be a 'next' link
       
   186   data = logic.getForFields(filter=filter, limit=limit+1, offset=offset,
       
   187                             order=order, prefetch=prefetch)
       
   188 
       
   189   if need_content and not data:
       
   190     return None
       
   191 
   222 
   192   more = len(data) > limit
   223   more = len(data) > limit
   193 
   224 
   194   if more:
   225   if more:
   195     del data[limit:]
   226     del data[limit:]
   202   if params.get('list_key_order'):
   233   if params.get('list_key_order'):
   203     export_link = generateLinkForRequest(request, base_params, {'export': idx})
   234     export_link = generateLinkForRequest(request, base_params, {'export': idx})
   204 
   235 
   205   if more:
   236   if more:
   206     # TODO(dbentley): here we need to implement a new field "last_key"
   237     # TODO(dbentley): here we need to implement a new field "last_key"
   207     next = generateLinkForRequest(request, base_params, 
   238     next = generateLinkForRequest(request, base_params,
   208                                   {offset_key: offset + limit,
   239                                   {offset_key: offset + limit,
   209                                    limit_key: limit})
   240                                    limit_key: limit})
   210 
   241 
   211   if offset > 0:
   242   if offset > 0:
   212     # TODO(dbentley): here we need to implement previous in the good way.
   243     # TODO(dbentley): here we need to implement previous in the good way.