Show More
@@ -1,65 +1,66 b'' | |||||
1 | from django.core.urlresolvers import reverse |
|
1 | from django.core.urlresolvers import reverse | |
2 | from django.core.files import File |
|
2 | from django.core.files import File | |
3 | from django.core.files.temp import NamedTemporaryFile |
|
3 | from django.core.files.temp import NamedTemporaryFile | |
4 | from django.core.paginator import EmptyPage |
|
4 | from django.core.paginator import EmptyPage | |
5 | from django.db import transaction |
|
5 | from django.db import transaction | |
6 | from django.http import Http404 |
|
6 | from django.http import Http404 | |
7 | from django.shortcuts import render, redirect |
|
7 | from django.shortcuts import render, redirect | |
8 | import requests |
|
8 | import requests | |
9 |
|
9 | |||
10 | from boards import utils, settings |
|
10 | from boards import utils, settings | |
11 | from boards.abstracts.paginator import get_paginator |
|
11 | from boards.abstracts.paginator import get_paginator | |
12 | from boards.models import Post, Thread, Ban, Tag, PostImage, Banner |
|
12 | from boards.models import Post, Thread, Ban, Tag, PostImage, Banner | |
13 | from boards.views.base import BaseBoardView |
|
13 | from boards.views.base import BaseBoardView | |
14 | from boards.views.posting_mixin import PostMixin |
|
14 | from boards.views.posting_mixin import PostMixin | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
17 | PARAMETER_CURRENT_PAGE = 'current_page' | |
18 | PARAMETER_PAGINATOR = 'paginator' |
|
18 | PARAMETER_PAGINATOR = 'paginator' | |
19 | PARAMETER_POSTS = 'posts' |
|
19 | PARAMETER_POSTS = 'posts' | |
20 |
|
20 | |||
21 | PARAMETER_PREV_LINK = 'prev_page_link' |
|
21 | PARAMETER_PREV_LINK = 'prev_page_link' | |
22 | PARAMETER_NEXT_LINK = 'next_page_link' |
|
22 | PARAMETER_NEXT_LINK = 'next_page_link' | |
23 |
|
23 | |||
24 | TEMPLATE = 'boards/feed.html' |
|
24 | TEMPLATE = 'boards/feed.html' | |
25 | DEFAULT_PAGE = 1 |
|
25 | DEFAULT_PAGE = 1 | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class FeedView(PostMixin, BaseBoardView): |
|
28 | class FeedView(PostMixin, BaseBoardView): | |
29 |
|
29 | |||
30 | def get(self, request, page=DEFAULT_PAGE): |
|
30 | def get(self, request, page=DEFAULT_PAGE): | |
31 | params = self.get_context_data(request=request) |
|
31 | params = self.get_context_data(request=request) | |
32 |
|
32 | |||
33 |
paginator = get_paginator(Post.objects.order_by('-pub_time') |
|
33 | paginator = get_paginator(Post.objects.order_by('-pub_time') | |
|
34 | .prefetch_related('images', 'thread', 'threads'), 10) | |||
34 | paginator.current_page = int(page) |
|
35 | paginator.current_page = int(page) | |
35 |
|
36 | |||
36 | params[PARAMETER_POSTS] = paginator.page(page).object_list |
|
37 | params[PARAMETER_POSTS] = paginator.page(page).object_list | |
37 |
|
38 | |||
38 | self.get_page_context(paginator, params, page) |
|
39 | self.get_page_context(paginator, params, page) | |
39 |
|
40 | |||
40 | return render(request, TEMPLATE, params) |
|
41 | return render(request, TEMPLATE, params) | |
41 |
|
42 | |||
42 | # TODO Dedup this into PagedMixin |
|
43 | # TODO Dedup this into PagedMixin | |
43 | def get_page_context(self, paginator, params, page): |
|
44 | def get_page_context(self, paginator, params, page): | |
44 | """ |
|
45 | """ | |
45 | Get pagination context variables |
|
46 | Get pagination context variables | |
46 | """ |
|
47 | """ | |
47 |
|
48 | |||
48 | params[PARAMETER_PAGINATOR] = paginator |
|
49 | params[PARAMETER_PAGINATOR] = paginator | |
49 | current_page = paginator.page(int(page)) |
|
50 | current_page = paginator.page(int(page)) | |
50 | params[PARAMETER_CURRENT_PAGE] = current_page |
|
51 | params[PARAMETER_CURRENT_PAGE] = current_page | |
51 | if current_page.has_previous(): |
|
52 | if current_page.has_previous(): | |
52 | params[PARAMETER_PREV_LINK] = self.get_previous_page_link( |
|
53 | params[PARAMETER_PREV_LINK] = self.get_previous_page_link( | |
53 | current_page) |
|
54 | current_page) | |
54 | if current_page.has_next(): |
|
55 | if current_page.has_next(): | |
55 | params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) |
|
56 | params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) | |
56 |
|
57 | |||
57 | def get_previous_page_link(self, current_page): |
|
58 | def get_previous_page_link(self, current_page): | |
58 | return reverse('feed', kwargs={ |
|
59 | return reverse('feed', kwargs={ | |
59 | 'page': current_page.previous_page_number(), |
|
60 | 'page': current_page.previous_page_number(), | |
60 | }) |
|
61 | }) | |
61 |
|
62 | |||
62 | def get_next_page_link(self, current_page): |
|
63 | def get_next_page_link(self, current_page): | |
63 | return reverse('feed', kwargs={ |
|
64 | return reverse('feed', kwargs={ | |
64 | 'page': current_page.next_page_number(), |
|
65 | 'page': current_page.next_page_number(), | |
65 | }) |
|
66 | }) |
General Comments 0
You need to be logged in to leave comments.
Login now