~sandy-walsh/nova/zones

« back to all changes in this revision

Viewing changes to nova/api/openstack/common.py

  • Committer: Sandy Walsh
  • Date: 2011-02-17 21:39:03 UTC
  • mfrom: (635.1.60 nova)
  • Revision ID: sandy.walsh@rackspace.com-20110217213903-swehe88wea8inxow
changed from 003-004 migration

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from nova import exception
19
19
 
20
20
 
21
 
def limited(items, req):
22
 
    """Return a slice of items according to requested offset and limit.
23
 
 
24
 
    items - a sliceable
25
 
    req - wobob.Request possibly containing offset and limit GET variables.
26
 
          offset is where to start in the list, and limit is the maximum number
27
 
          of items to return.
28
 
 
29
 
    If limit is not specified, 0, or > 1000, defaults to 1000.
30
 
    """
31
 
 
32
 
    offset = int(req.GET.get('offset', 0))
33
 
    limit = int(req.GET.get('limit', 0))
34
 
    if not limit:
35
 
        limit = 1000
36
 
    limit = min(1000, limit)
 
21
def limited(items, request, max_limit=1000):
 
22
    """
 
23
    Return a slice of items according to requested offset and limit.
 
24
 
 
25
    @param items: A sliceable entity
 
26
    @param request: `webob.Request` possibly containing 'offset' and 'limit'
 
27
                    GET variables. 'offset' is where to start in the list,
 
28
                    and 'limit' is the maximum number of items to return. If
 
29
                    'limit' is not specified, 0, or > max_limit, we default
 
30
                    to max_limit.
 
31
    @kwarg max_limit: The maximum number of items to return from 'items'
 
32
    """
 
33
    try:
 
34
        offset = int(request.GET.get('offset', 0))
 
35
    except ValueError:
 
36
        offset = 0
 
37
 
 
38
    try:
 
39
        limit = int(request.GET.get('limit', max_limit))
 
40
    except ValueError:
 
41
        limit = max_limit
 
42
 
 
43
    limit = min(max_limit, limit or max_limit)
37
44
    range_end = offset + limit
38
45
    return items[offset:range_end]
39
46