Conditions | 21 |
Total Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like get_pagination_context() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import re |
||
100 | def get_pagination_context(page, pages_to_show=11, |
||
101 | url=None, size=None, extra=None, |
||
102 | parameter_name='page'): |
||
103 | """ |
||
104 | Generate Bootstrap pagination context from a page object |
||
105 | """ |
||
106 | pages_to_show = int(pages_to_show) |
||
107 | if pages_to_show < 1: |
||
108 | raise ValueError( |
||
109 | "Pagination pages_to_show should be a positive" |
||
110 | "integer, you specified {pages}".format( |
||
111 | pages=pages_to_show) |
||
112 | ) |
||
113 | num_pages = page.paginator.num_pages |
||
114 | current_page = page.number |
||
115 | half_page_num = int(floor(pages_to_show / 2)) |
||
116 | if half_page_num < 0: |
||
117 | half_page_num = 0 |
||
118 | first_page = current_page - half_page_num |
||
119 | if first_page <= 1: |
||
120 | first_page = 1 |
||
121 | if first_page > 1: |
||
122 | pages_back = first_page - half_page_num |
||
123 | if pages_back < 1: |
||
124 | pages_back = 1 |
||
125 | else: |
||
126 | pages_back = None |
||
127 | last_page = first_page + pages_to_show - 1 |
||
128 | if pages_back is None: |
||
129 | last_page += 1 |
||
130 | if last_page > num_pages: |
||
131 | last_page = num_pages |
||
132 | if last_page < num_pages: |
||
133 | pages_forward = last_page + half_page_num |
||
134 | if pages_forward > num_pages: |
||
135 | pages_forward = num_pages |
||
136 | else: |
||
137 | pages_forward = None |
||
138 | if first_page > 1: |
||
139 | first_page -= 1 |
||
140 | if pages_back is not None and pages_back > 1: |
||
141 | pages_back -= 1 |
||
142 | else: |
||
143 | pages_back = None |
||
144 | pages_shown = [] |
||
145 | for i in range(first_page, last_page + 1): |
||
146 | pages_shown.append(i) |
||
147 | # Append proper character to url |
||
148 | if url: |
||
149 | # Remove existing page GET parameters |
||
150 | url = force_text(url) |
||
151 | url = re.sub(r'\?{0}\=[^\&]+'.format(parameter_name), '?', url) |
||
152 | url = re.sub(r'\&{0}\=[^\&]+'.format(parameter_name), '', url) |
||
153 | # Append proper separator |
||
154 | if '?' in url: |
||
155 | url += '&' |
||
156 | else: |
||
157 | url += '?' |
||
158 | # Append extra string to url |
||
159 | if extra: |
||
160 | if not url: |
||
161 | url = '?' |
||
162 | url += force_text(extra) + '&' |
||
163 | if url: |
||
164 | url = url.replace('?&', '?') |
||
165 | # Set CSS classes, see http://getbootstrap.com/components/#pagination |
||
166 | pagination_css_classes = ['pagination'] |
||
167 | if size == 'small': |
||
168 | pagination_css_classes.append('pagination-sm') |
||
169 | elif size == 'large': |
||
170 | pagination_css_classes.append('pagination-lg') |
||
171 | # Build context object |
||
172 | return { |
||
173 | 'bootstrap_pagination_url': url, |
||
174 | 'num_pages': num_pages, |
||
175 | 'current_page': current_page, |
||
176 | 'first_page': first_page, |
||
177 | 'last_page': last_page, |
||
178 | 'pages_shown': pages_shown, |
||
179 | 'pages_back': pages_back, |
||
180 | 'pages_forward': pages_forward, |
||
181 | 'pagination_css_classes': ' '.join(pagination_css_classes), |
||
182 | 'parameter_name': parameter_name, |
||
183 | } |
||
184 |