Show More
@@ -1,72 +1,74 | |||||
1 | __author__ = 'neko259' |
|
1 | __author__ = 'neko259' | |
2 |
|
2 | |||
3 | from django.core.paginator import Paginator |
|
3 | from django.core.paginator import Paginator | |
4 |
|
4 | |||
5 | PAGINATOR_LOOKAROUND_SIZE = 2 |
|
5 | PAGINATOR_LOOKAROUND_SIZE = 2 | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | def get_paginator(*args, **kwargs): |
|
8 | def get_paginator(*args, **kwargs): | |
9 | return DividedPaginator(*args, **kwargs) |
|
9 | return DividedPaginator(*args, **kwargs) | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | class DividedPaginator(Paginator): |
|
12 | class DividedPaginator(Paginator): | |
13 |
|
13 | |||
14 | lookaround_size = PAGINATOR_LOOKAROUND_SIZE |
|
14 | lookaround_size = PAGINATOR_LOOKAROUND_SIZE | |
15 | current_page = 0 |
|
15 | current_page = 0 | |
16 |
|
16 | |||
17 | def _left_range(self): |
|
17 | def _left_range(self): | |
18 | return self.page_range[:self.lookaround_size] |
|
18 | return self.page_range[:self.lookaround_size] | |
19 |
|
19 | |||
20 | def _right_range(self): |
|
20 | def _right_range(self): | |
21 | pages = self.num_pages-self.lookaround_size |
|
21 | pages = self.num_pages-self.lookaround_size | |
22 | if pages <= 0: |
|
22 | if pages <= 0: | |
23 | return [] |
|
23 | return [] | |
24 | else: |
|
24 | else: | |
25 | return self.page_range[pages:] |
|
25 | return self.page_range[pages:] | |
26 |
|
26 | |||
27 | def _center_range(self): |
|
27 | def _center_range(self): | |
28 | index = self.page_range.index(self.current_page) |
|
28 | index = self.page_range.index(self.current_page) | |
29 |
|
29 | |||
30 | start = max(self.lookaround_size, index - self.lookaround_size) |
|
30 | start = max(self.lookaround_size, index - self.lookaround_size) | |
31 | end = min(self.num_pages - self.lookaround_size, index + self.lookaround_size + 1) |
|
31 | end = min(self.num_pages - self.lookaround_size, index + self.lookaround_size + 1) | |
32 | return self.page_range[start:end] |
|
32 | return self.page_range[start:end] | |
33 |
|
33 | |||
34 | def get_divided_range(self): |
|
34 | def get_divided_range(self): | |
35 | dr = list() |
|
35 | dr = list() | |
36 |
|
36 | |||
37 | dr += self._left_range() |
|
37 | dr += self._left_range() | |
38 | dr += self._center_range() |
|
38 | dr += self._center_range() | |
39 | dr += self._right_range() |
|
39 | dr += self._right_range() | |
40 |
|
40 | |||
41 | # Remove duplicates |
|
41 | # Remove duplicates | |
42 | dr = list(set(dr)) |
|
42 | dr = list(set(dr)) | |
43 | dr.sort() |
|
43 | dr.sort() | |
44 |
|
44 | |||
45 | return dr |
|
45 | return dr | |
46 |
|
46 | |||
47 | def get_dividers(self): |
|
47 | def get_dividers(self): | |
48 | dividers = [] |
|
48 | dividers = [] | |
49 |
|
49 | |||
50 | prev_page = 1 |
|
50 | prev_page = 1 | |
51 | for page in self.get_divided_range(): |
|
51 | for page in self.get_divided_range(): | |
52 | if page - prev_page > 1: |
|
52 | if page - prev_page > 1: | |
53 | dividers.append(page) |
|
53 | dividers.append(page) | |
54 |
|
54 | |||
55 | # There can be no more than 2 dividers, so don't bother going |
|
55 | # There can be no more than 2 dividers, so don't bother going | |
56 | # further |
|
56 | # further | |
57 | if len(dividers) > 2: |
|
57 | if len(dividers) > 2: | |
58 | break |
|
58 | break | |
59 | prev_page = page |
|
59 | prev_page = page | |
60 |
|
60 | |||
61 | return dividers |
|
61 | return dividers | |
62 |
|
62 | |||
63 | def set_url(self, link, params): |
|
63 | def set_url(self, link, params): | |
64 | self.link = link |
|
64 | self.link = link | |
65 | self.params = params |
|
65 | self.params = params | |
66 |
|
66 | |||
67 | def get_page_url(self, page): |
|
67 | def get_page_url(self, page): | |
68 | self.params['page'] = page |
|
68 | self.params['page'] = page | |
69 | url_params = '?' + '&'.join(['{}={}'.format(key, self.params[key]) |
|
69 | url_params = '?' + '&'.join(['{}={}'.format(key, self.params[key]) | |
70 | for key in self.params.keys()]) |
|
70 | for key in self.params.keys()]) | |
71 | return self.link + url_params |
|
71 | return self.link + url_params | |
72 |
|
72 | |||
|
73 | def supports_urls(self): | |||
|
74 | return self.link is not None and self.params is not None |
@@ -1,48 +1,49 | |||||
1 | import re |
|
1 | import re | |
2 | from django.shortcuts import get_object_or_404 |
|
2 | from django.shortcuts import get_object_or_404 | |
3 | from django import template |
|
3 | from django import template | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | IMG_ACTION_URL = '[<a href="{}">{}</a>]' |
|
6 | IMG_ACTION_URL = '[<a href="{}">{}</a>]' | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | register = template.Library() |
|
9 | register = template.Library() | |
10 |
|
10 | |||
11 | actions = [ |
|
11 | actions = [ | |
12 | { |
|
12 | { | |
13 | 'name': 'google', |
|
13 | 'name': 'google', | |
14 | 'link': 'http://google.com/searchbyimage?image_url=%s', |
|
14 | 'link': 'http://google.com/searchbyimage?image_url=%s', | |
15 | }, |
|
15 | }, | |
16 | { |
|
16 | { | |
17 | 'name': 'iqdb', |
|
17 | 'name': 'iqdb', | |
18 | 'link': 'http://iqdb.org/?url=%s', |
|
18 | 'link': 'http://iqdb.org/?url=%s', | |
19 | }, |
|
19 | }, | |
20 | ] |
|
20 | ] | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | @register.simple_tag(name='post_url') |
|
23 | @register.simple_tag(name='post_url') | |
24 | def post_url(*args, **kwargs): |
|
24 | def post_url(*args, **kwargs): | |
25 | post_id = args[0] |
|
25 | post_id = args[0] | |
26 |
|
26 | |||
27 | post = get_object_or_404('Post', id=post_id) |
|
27 | post = get_object_or_404('Post', id=post_id) | |
28 |
|
28 | |||
29 | return post.get_absolute_url() |
|
29 | return post.get_absolute_url() | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | @register.simple_tag(name='image_actions') |
|
32 | @register.simple_tag(name='image_actions') | |
33 | def image_actions(*args, **kwargs): |
|
33 | def image_actions(*args, **kwargs): | |
34 | image_link = args[0] |
|
34 | image_link = args[0] | |
35 | if len(args) > 1: |
|
35 | if len(args) > 1: | |
36 | image_link = 'http://' + args[1] + image_link # TODO https? |
|
36 | image_link = 'http://' + args[1] + image_link # TODO https? | |
37 |
|
37 | |||
38 | return ', '.join([IMG_ACTION_URL.format( |
|
38 | return ', '.join([IMG_ACTION_URL.format( | |
39 | action['link'] % image_link, action['name']) for action in actions]) |
|
39 | action['link'] % image_link, action['name']) for action in actions]) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | @register.simple_tag(name='post_view') |
|
42 | @register.simple_tag(name='post_view') | |
43 | def post_view(post, *args, **kwargs): |
|
43 | def post_view(post, *args, **kwargs): | |
44 | return post.get_view(*args, **kwargs) |
|
44 | return post.get_view(*args, **kwargs) | |
45 |
|
45 | |||
46 | @register.simple_tag(name='page_url') |
|
46 | @register.simple_tag(name='page_url') | |
47 | def page_url(paginator, page_number, *args, **kwargs): |
|
47 | def page_url(paginator, page_number, *args, **kwargs): | |
48 | return paginator.get_page_url(page_number) |
|
48 | if paginator.supports_urls(): | |
|
49 | return paginator.get_page_url(page_number) |
General Comments 0
You need to be logged in to leave comments.
Login now