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