app/soc/views/helper/lists.py
changeset 2049 d9adbaf1c30f
parent 1821 6a548cbb0f7e
child 2077 fd2e83a297c7
equal deleted inserted replaced
2048:236f37777764 2049:d9adbaf1c30f
    48     ]
    48     ]
    49 
    49 
    50 
    50 
    51 def getPreferredListPagination(user=None):
    51 def getPreferredListPagination(user=None):
    52   """Returns User's preferred list pagination limit.
    52   """Returns User's preferred list pagination limit.
    53   
    53 
    54   Args:
    54   Args:
    55     user: User entity containing the list pagination preference;
    55     user: User entity containing the list pagination preference;
    56       default is None, to use the current logged-in User
    56       default is None, to use the current logged-in User
    57   """
    57   """
    58   # TODO: eventually this limit should be a User profile preference
    58   # TODO: eventually this limit should be a User profile preference
    59   #   (stored in the site-wide User Model) preference 
    59   #   (stored in the site-wide User Model) preference
    60   return DEF_DEFAULT_PAGINATION
    60   return DEF_DEFAULT_PAGINATION
    61 
    61 
    62 
    62 
    63 def getLimitAndOffset(request, offset_key, limit_key):
    63 def getLimitAndOffset(request, offset_key, limit_key):
    64   """Retrieves, converts and validates offset and limit values
    64   """Retrieves, converts and validates offset and limit values
   112 
   112 
   113 
   113 
   114 def getListContent(request, params, filter=None, order=None,
   114 def getListContent(request, params, filter=None, order=None,
   115                    idx=0, need_content=False):
   115                    idx=0, need_content=False):
   116   """Returns a dict with fields used for rendering lists.
   116   """Returns a dict with fields used for rendering lists.
       
   117 
       
   118   TODO(dbentley): we need better terminology. List in this context can have
       
   119     one of two meanings.
       
   120     Meaning 1:  the underlying list, which may be very large.
       
   121     Meaning 2:  the returned list, which is at most 'limit' items.
   117 
   122 
   118   Args:
   123   Args:
   119     request: the Django HTTP request object
   124     request: the Django HTTP request object
   120     params: a dict with params for the View this list belongs to
   125     params: a dict with params for the View this list belongs to
   121     filter: a filter for this list
   126     filter: a filter for this list
   122     order: the order which should be used for the list (in getForFields format)
   127     order: the order which should be used for the list (in getForFields format)
   123     idx: the index of this list
   128     idx: the index of this list
   124     need_content: iff True will return None if there is no data
   129     need_content: iff True will return None if there is no data
   125 
   130 
   126   Returns:
   131   Returns:
   127     A a dictionary with the following values set:
   132     A dictionary with the following values set:
   128 
   133 
   129     {
   134     {
   130       'data': list data to be displayed
   135       'data': list data to be displayed
   131       'main': url to list main template
   136       'main': url to list main template
   132       'pagination': url to list pagination template
   137       'pagination': url to list pagination template
   133       'row': url to list row template
   138       'row': url to list row template
   134       'heading': url to list heading template
   139       'heading': url to list heading template
   135       'limit': max amount of items per page,
   140       'limit': max amount of items per page,
   136       'newest': url to first page of the list 
   141       'newest': url to first page of the list
   137       'prev': url to previous page 
   142       'prev': url to previous page
   138       'next': url to next page
   143       'next': url to next page
   139       'first': offset of the first item in the list
   144       'first': offset of the first item in the list
   140       'last': offest of the lst item in the list
   145       'last': offest of the last item in the list
   141     }
   146     }
   142   """
   147   """
   143 
   148 
   144   logic = params['logic']
   149   logic = params['logic']
   145 
   150 
   161   if more:
   166   if more:
   162     del data[limit:]
   167     del data[limit:]
   163 
   168 
   164   newest = next = prev = export_link =''
   169   newest = next = prev = export_link =''
   165 
   170 
   166   get_args = request.GET
   171   base_params = dict(i for i in request.GET.iteritems() if
   167   offset_and_limits = {}
   172                      i[0].startswith('offset_') or i[0].startswith('limit_'))
   168 
       
   169   for key, value in get_args.iteritems():
       
   170     if key.startswith('offset_') or key.startswith('limit_'):
       
   171       offset_and_limits[key] = value
       
   172 
   173 
   173   if params.get('list_key_order'):
   174   if params.get('list_key_order'):
   174     offset_and_limits['export'] = idx
   175     export_link_params = dict(base_params)
   175     export_link = generateLinkFromGetArgs(request, offset_and_limits)
   176     export_link_params['export'] = idx
   176     del offset_and_limits['export']
   177     export_link = generateLinkFromGetArgs(request, export_link_params)
   177 
   178 
   178   if more:
   179   if more:
   179     offset_and_limits[offset_key] = offset+limit
   180     # TODO(dbentley): here we need to implement a new field "last_key"
   180     offset_and_limits[limit_key] = limit
   181     next_params = dict(base_params)
   181     next = generateLinkFromGetArgs(request, offset_and_limits)
   182     next_params[offset_key] = offset+limit
       
   183     next_params[limit_key] = limit
       
   184     next = generateLinkFromGetArgs(request, next_params)
   182 
   185 
   183   if offset > 0:
   186   if offset > 0:
   184     offset_and_limits[offset_key] = max(0, offset-limit)
   187     # TODO(dbentley): here we need to implement previous in the good way.
   185     offset_and_limits[limit_key] = limit
   188     prev_params = dict(base_params)
   186     prev = generateLinkFromGetArgs(request, offset_and_limits)
   189     prev_params[offset_key] = max(0, offset-limit)
       
   190     prev_params[limit_key] = limit
       
   191     prev = generateLinkFromGetArgs(request, prev_params)
   187 
   192 
   188   if offset > limit:
   193   if offset > limit:
   189     del offset_and_limits[offset_key]
   194     newest_params = dict(base_params)
   190     offset_and_limits[limit_key] = limit
   195     del newest_params[offset_key]
   191 
   196     newest_params[limit_key] = limit
   192     newest = generateLinkFromGetArgs(request, offset_and_limits)
   197     newest = generateLinkFromGetArgs(request, newest_params)
   193 
   198 
   194   content = {
   199   content = {
   195       'data': data,
   200       'data': data,
   196       'export': export_link,
   201       'export': export_link,
   197       'first': offset+1,
   202       'first': offset+1,
   198       'last': len(data) > 1 and offset+len(data) or None,
   203       'last': len(data) > 1 and offset+len(data) or None,
   199       'logic': logic,
   204       'logic': logic,
   200       'limit': limit,
   205       'limit': limit,
   201       'newest': newest, 
   206       'newest': newest,
   202       'next': next,
   207       'next': next,
   203       'pagination_form': pagination_form,
   208       'pagination_form': pagination_form,
   204       'prev': prev, 
   209       'prev': prev,
   205       }
   210       }
   206 
   211 
   207   updates = dicts.rename(params, params['list_params'])
   212   updates = dicts.rename(params, params['list_params'])
   208   content.update(updates)
   213   content.update(updates)
   209 
   214 
   212 
   217 
   213 def makePaginationForm(
   218 def makePaginationForm(
   214   request, limit, arg_name, choices=DEF_PAGINATION_CHOICES,
   219   request, limit, arg_name, choices=DEF_PAGINATION_CHOICES,
   215   field_name_fmt=soc.views.helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
   220   field_name_fmt=soc.views.helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
   216   """Returns a customized pagination limit selection form.
   221   """Returns a customized pagination limit selection form.
   217   
   222 
   218   Args:
   223   Args:
   219     request: the standard Django HTTP request object
   224     request: the standard Django HTTP request object
   220     limit: the initial value of the selection control
   225     limit: the initial value of the selection control
   221     arg_name: see soc.views.helper.forms.makeSelectQueryArgForm(); default is 'limit'
   226     arg_name: see soc.views.helper.forms.makeSelectQueryArgForm(); default is 'limit'
   222     choices: see soc.views.helper.forms.makeSelectQueryArgForm(); default is
   227     choices: see soc.views.helper.forms.makeSelectQueryArgForm(); default is
   223       DEF_PAGINATION_CHOICES
   228       DEF_PAGINATION_CHOICES
   224     field_name_fmt: see soc.views.helper.forms.makeSelectQueryArgForm()
   229     field_name_fmt: see soc.views.helper.forms.makeSelectQueryArgForm()
   225   """
   230   """
   226   choices = makeNewPaginationChoices(limit=limit, choices=choices)
   231   choices = makeNewPaginationChoices(limit=limit, choices=choices)
   227   
   232 
   228   return soc.views.helper.forms.makeSelectQueryArgForm(
   233   return soc.views.helper.forms.makeSelectQueryArgForm(
   229       request, arg_name, limit, choices)
   234       request, arg_name, limit, choices)
   230 
   235 
   231 
   236 
   232 def makeNewPaginationChoices(limit=DEF_DEFAULT_PAGINATION,
   237 def makeNewPaginationChoices(limit=DEF_DEFAULT_PAGINATION,