app/soc/views/helper/lists.py
changeset 555 3cdfb42d941b
parent 553 c0cc20b4afc9
child 572 1b3e7280743a
equal deleted inserted replaced
554:68c7a1dd3c52 555:3cdfb42d941b
    21   '"Chen Lunpeng" <forever.clp@gmail.com>',
    21   '"Chen Lunpeng" <forever.clp@gmail.com>',
    22   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    22   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    23   ]
    23   ]
    24 
    24 
    25 
    25 
       
    26 from soc.logic import dicts
    26 from soc.views import helper
    27 from soc.views import helper
    27 
    28 
    28 import soc.views.helper.forms
    29 import soc.views.helper.forms
    29 
    30 
    30 
    31 
    76     limit = getPreferredListPagination()
    77     limit = getPreferredListPagination()
    77 
    78 
    78   return max(0, offset), max(1, min(limit, MAX_PAGINATION))
    79   return max(0, offset), max(1, min(limit, MAX_PAGINATION))
    79 
    80 
    80 
    81 
    81 def getList(request, data, offset, limit):
    82 def getListContent(request, params, logic, filter):
    82   """Returns a dict with fields used for rendering lists.
    83   """Returns a dict with fields used for rendering lists.
    83 
    84 
    84   Args:
    85   Args:
    85     request: the Django HTTP request object
    86     request: the Django HTTP request object
    86     data: array of data to be displayed in the list
    87     params: a dict with params for the View this list belongs to
    87     offset: offset in list which defines first item to return
    88     logic: the logic object for the View this list belongs to
    88     limit: max amount of items per page
    89     filter: a filter for this list
    89 
    90 
    90   Returns:
    91   Returns:
    91     A a dictionary with the following values set:
    92     A a dictionary with the following values set:
    92 
    93 
    93     {
    94     {
   103       'first': offset of the first item in the list
   104       'first': offset of the first item in the list
   104       'last': offest of the lst item in the list
   105       'last': offest of the lst item in the list
   105     }
   106     }
   106   """
   107   """
   107 
   108 
       
   109   offset, limit = helper.lists.cleanListParameters(
       
   110       offset=request.GET.get('offset'), limit=request.GET.get('limit'))
       
   111 
       
   112   # Fetch one more to see if there should be a 'next' link
       
   113   if not filter:
       
   114     data = logic.getForLimitAndOffset(limit+1, offset=offset)
       
   115   else:
       
   116     data = logic.getForFields(filter, limit=limit+1, offset=offset)
       
   117 
   108   if not data:
   118   if not data:
   109     data = []
   119     data = []
   110 
   120 
   111   more = bool(data[limit:])
   121   more = bool(data[limit:])
   112   if more:
   122   if more:
   113     del data[limit:]
   123     del data[limit:]
   114 
   124 
   115   newest = ''
   125   newest = next = prev = ''
   116   next = ''
       
   117   prev = ''
       
   118 
   126 
   119   if more:
   127   if more:
   120     next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
   128     next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
   121 
   129 
   122   if offset > 0:
   130   if offset > 0:
   127 
   135 
   128   content = {
   136   content = {
   129       'data': data,
   137       'data': data,
   130       'first': offset+1,
   138       'first': offset+1,
   131       'last': len(data) > 1 and offset+len(data) or None,
   139       'last': len(data) > 1 and offset+len(data) or None,
       
   140       'logic': logic,
   132       'limit': limit,
   141       'limit': limit,
   133       'newest': newest, 
   142       'newest': newest, 
   134       'next': next,
   143       'next': next,
   135       'prev': prev, 
   144       'prev': prev, 
   136       }
   145       }
       
   146 
       
   147   updates = dicts.rename(params, params['list_params'])
       
   148   content.update(updates)
   137 
   149 
   138   return content
   150   return content
   139 
   151 
   140 
   152 
   141 def makePaginationForm(
   153 def makePaginationForm(