summaryrefslogtreecommitdiff
path: root/trove/common/pagination.py
blob: e9ed16b221196327127fa4a4237ae7c3259c6d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import bisect
import collections
import urllib.parse as urllib_parse


def url_quote(s):
    if s is None:
        return s
    return urllib_parse.quote(str(s))


def paginate_list(li, limit=None, marker=None, include_marker=False,
                  key=lambda x: x):
    """Sort the given list and return a sublist containing a page of items.

    :param list li:             The list to be paginated.
    :param int limit:           Maximum number of items to be returned.
    :param marker:              Key of the first item to appear on the sublist.
    :param bool include_marker: Include the marker value itself in the sublist.
    :param lambda key:          Sorting expression.
    :return:
    """
    sli = sorted(li, key=key)
    index = [key(item) for item in sli]
    if marker is None:
        marker = ''
    if include_marker:
        pos = bisect.bisect_left(index, marker)
    else:
        pos = bisect.bisect(index, marker)

    if limit and pos + limit < len(sli):
        page = sli[pos:pos + limit]
        return page, key(page[-1])
    else:
        return sli[pos:], None


def paginate_object_list(li, attr_name, limit=None, marker=None,
                         include_marker=False):
    """Wrapper for paginate_list to handle lists of generic objects paginated
    based on an attribute.
    """
    return paginate_list(li, limit=limit, marker=marker,
                         include_marker=include_marker,
                         key=lambda x: getattr(x, attr_name))


def paginate_dict_list(li, key, limit=None, marker=None, include_marker=False):
    """Wrapper for paginate_list to handle lists of dicts paginated
    based on a key.
    """
    return paginate_list(li, limit=limit, marker=marker,
                         include_marker=include_marker,
                         key=lambda x: x[key])


class PaginatedDataView(object):

    def __init__(self, collection_type, collection, current_page_url,
                 next_page_marker=None):
        self.collection_type = collection_type
        self.collection = collection
        self.current_page_url = current_page_url
        self.next_page_marker = url_quote(next_page_marker)

    def data(self):
        return {self.collection_type: self.collection,
                'links': self._links,
                }

    def _links(self):
        if not self.next_page_marker:
            return []
        app_url = AppUrl(self.current_page_url)
        next_url = app_url.change_query_params(marker=self.next_page_marker)
        next_link = {
            'rel': 'next',
            'href': str(next_url),
        }
        return [next_link]


class SimplePaginatedDataView(object):
    # In some cases, we can't create a PaginatedDataView because
    # we don't have a collection query object to create a view on.
    # In that case, we have to supply the URL and collection manually.

    def __init__(self, url, name, view, marker):
        self.url = url
        self.name = name
        self.view = view
        self.marker = url_quote(marker)

    def data(self):
        if not self.marker:
            return self.view.data()

        app_url = AppUrl(self.url)
        next_url = str(app_url.change_query_params(marker=self.marker))
        next_link = {'rel': 'next',
                     'href': next_url}
        view_data = {self.name: self.view.data()[self.name],
                     'links': [next_link]}
        return view_data


class AppUrl(object):

    def __init__(self, url):
        self.url = url

    def __str__(self):
        return self.url

    def change_query_params(self, **kwargs):
        # Seeks out the query params in a URL and changes/appends to them
        # from the kwargs given. So change_query_params(foo='bar')
        # would remove from the URL any old instance of foo=something and
        # then add &foo=bar to the URL.
        parsed_url = urllib_parse.urlparse(self.url)
        # Build a dictionary out of the query parameters in the URL
        # with an OrderedDict to preserve the order of the URL.
        query_params = collections.OrderedDict(
            urllib_parse.parse_qsl(parsed_url.query))
        # Use kwargs to change or update any values in the query dict.
        query_params.update(kwargs)

        # Build a new query based on the updated query dict.
        new_query_params = urllib_parse.urlencode(query_params)
        return self.__class__(
            # Force HTTPS.
            urllib_parse.ParseResult('https',
                                     parsed_url.netloc, parsed_url.path,
                                     parsed_url.params, new_query_params,
                                     parsed_url.fragment).geturl())