Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: appengine/monorail/framework/paginate.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is govered by a BSD-style
3 # license that can be found in the LICENSE file or at
4 # https://developers.google.com/open-source/licenses/bsd
5
6 """Classes that help display pagination widgets for result sets."""
7
8 import logging
9
10 from third_party import ezt
11
12 import settings
13 from framework import framework_helpers
14
15
16 class VirtualPagination(object):
17 """Class to calc Prev and Next pagination links based on result counts."""
18
19 def __init__(self, mr, total_count, items_per_page,
20 list_page_url=None, count_up=True,
21 start_param='start', num_param='num', max_num=None):
22 """Given 'num' and 'start' params, determine Prev and Next links.
23
24 Args:
25 mr: commonly used info parsed from the request.
26 total_count: total number of artifacts that satisfy the query.
27 items_per_page: number of items to display on each page, e.g., 25.
28 list_page_url: URL of the web application page that is displaying
29 the list of artifacts. Used to build the Prev and Next URLs.
30 If None, no URLs will be built.
31 count_up: if False, count down from total_count.
32 start_param: query string parameter name to use for the start
33 of the pagination page.
34 num_param: query string parameter name to use for the number of items
35 to show on a pagination page.
36 max_num: optional limit on the value of the num param. If not given,
37 settings.max_artifact_search_results_per_page is used.
38 """
39 self.total_count = total_count
40 self.prev_url = ''
41 self.reload_url = ''
42 self.next_url = ''
43
44 if max_num is None:
45 max_num = settings.max_artifact_search_results_per_page
46
47 self.num = mr.GetPositiveIntParam(num_param, items_per_page)
48 self.num = min(self.num, max_num)
49
50 if count_up:
51 self.start = mr.GetPositiveIntParam(start_param, 0)
52 self.last = min(self.total_count, self.start + self.num)
53 prev_start = max(0, self.start - self.num)
54 next_start = self.start + self.num
55 else:
56 self.start = mr.GetPositiveIntParam(start_param, self.total_count)
57 self.last = max(0, self.start - self.num)
58 prev_start = min(self.total_count, self.start + self.num)
59 next_start = self.start - self.num
60
61 if list_page_url:
62 if mr.project_name:
63 list_servlet_rel_url = '/p/%s%s' % (
64 mr.project_name, list_page_url)
65 else:
66 list_servlet_rel_url = list_page_url
67
68 self.reload_url = framework_helpers.FormatURL(
69 mr, list_servlet_rel_url,
70 **{start_param: self.start, num_param: self.num})
71
72 if prev_start != self.start:
73 self.prev_url = framework_helpers.FormatURL(
74 mr, list_servlet_rel_url,
75 **{start_param: prev_start, num_param: self.num})
76 if ((count_up and next_start < self.total_count) or
77 (not count_up and next_start >= 1)):
78 self.next_url = framework_helpers.FormatURL(
79 mr, list_servlet_rel_url,
80 **{start_param: next_start, num_param: self.num})
81
82 self.visible = ezt.boolean(self.last != self.start)
83
84 # Adjust indices to one-based values for display to users.
85 if count_up:
86 self.start += 1
87 else:
88 self.last += 1
89
90 def DebugString(self):
91 """Return a string that is useful in on-page debugging."""
92 return '%s - %s of %s; prev_url:%s; next_url:%s' % (
93 self.start, self.last, self.total_count, self.prev_url, self.next_url)
94
95
96 class ArtifactPagination(VirtualPagination):
97 """Class to calc Prev and Next pagination links based on a results list."""
98
99 def __init__(
100 self, mr, results, items_per_page, list_page_url, total_count=None,
101 limit_reached=False, skipped=0):
102 """Given 'num' and 'start' params, determine Prev and Next links.
103
104 Args:
105 mr: commonly used info parsed from the request.
106 results: a list of artifact ids that satisfy the query.
107 items_per_page: number of items to display on each page, e.g., 25.
108 list_page_url: URL of the web application page that is displaying
109 the list of artifacts. Used to build the Prev and Next URLs.
110 total_count: specify total result count rather than the length of results
111 limit_reached: optional boolean that indicates that more results could
112 not be fetched because a limit was reached.
113 skipped: optional int number of items that were skipped and left off the
114 front of results.
115 """
116 if total_count is None:
117 total_count = skipped + len(results)
118 super(ArtifactPagination, self).__init__(
119 mr, total_count, items_per_page, list_page_url=list_page_url)
120
121 self.limit_reached = ezt.boolean(limit_reached)
122 # Determine which of those results should be visible on the current page.
123 range_start = self.start - 1 - skipped
124 range_end = range_start + self.num
125 assert 0 <= range_start <= range_end
126 self.visible_results = results[range_start:range_end]
OLDNEW
« no previous file with comments | « appengine/monorail/framework/monorailrequest.py ('k') | appengine/monorail/framework/pbproxy_test_pb2.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698