feed.py
81 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
neko259
|
r1165 | from django.core.urlresolvers import reverse | ||
neko259
|
r1302 | from django.shortcuts import render | ||
neko259
|
r1165 | |||
from boards.abstracts.paginator import get_paginator | ||||
neko259
|
r1170 | from boards.abstracts.settingsmanager import get_settings_manager | ||
neko259
|
r1302 | from boards.models import Post | ||
neko259
|
r1165 | from boards.views.base import BaseBoardView | ||
from boards.views.posting_mixin import PostMixin | ||||
neko259
|
r1302 | POSTS_PER_PAGE = 10 | ||
neko259
|
r1165 | |||
PARAMETER_CURRENT_PAGE = 'current_page' | ||||
PARAMETER_PAGINATOR = 'paginator' | ||||
PARAMETER_POSTS = 'posts' | ||||
neko259
|
r1302 | PARAMETER_ADDITONAL_ATTRS = 'additional_attrs' | ||
neko259
|
r1165 | |||
PARAMETER_PREV_LINK = 'prev_page_link' | ||||
PARAMETER_NEXT_LINK = 'next_page_link' | ||||
TEMPLATE = 'boards/feed.html' | ||||
DEFAULT_PAGE = 1 | ||||
class FeedView(PostMixin, BaseBoardView): | ||||
neko259
|
r1204 | def get(self, request): | ||
page = request.GET.get('page', DEFAULT_PAGE) | ||||
neko259
|
r1302 | tripcode = request.GET.get('tripcode', None) | ||
neko259
|
r1204 | |||
neko259
|
r1165 | params = self.get_context_data(request=request) | ||
neko259
|
r1170 | settings_manager = get_settings_manager(request) | ||
neko259
|
r1302 | posts = Post.objects.exclude( | ||
threads__tags__in=settings_manager.get_hidden_tags()).order_by( | ||||
'-pub_time').prefetch_related('images', 'thread', 'threads') | ||||
if tripcode: | ||||
posts = posts.filter(tripcode=tripcode) | ||||
paginator = get_paginator(posts, POSTS_PER_PAGE) | ||||
neko259
|
r1165 | paginator.current_page = int(page) | ||
params[PARAMETER_POSTS] = paginator.page(page).object_list | ||||
neko259
|
r1302 | additional_params = dict() | ||
if tripcode: | ||||
additional_params['tripcode'] = tripcode | ||||
params[PARAMETER_ADDITONAL_ATTRS] = '&tripcode=' + tripcode | ||||
self.get_page_context(paginator, params, page, additional_params) | ||||
neko259
|
r1165 | |||
return render(request, TEMPLATE, params) | ||||
# TODO Dedup this into PagedMixin | ||||
neko259
|
r1302 | def get_page_context(self, paginator, params, page, additional_params): | ||
neko259
|
r1165 | """ | ||
Get pagination context variables | ||||
""" | ||||
params[PARAMETER_PAGINATOR] = paginator | ||||
current_page = paginator.page(int(page)) | ||||
params[PARAMETER_CURRENT_PAGE] = current_page | ||||
if current_page.has_previous(): | ||||
params[PARAMETER_PREV_LINK] = self.get_previous_page_link( | ||||
current_page) | ||||
neko259
|
r1302 | for param in additional_params.keys(): | ||
params[PARAMETER_PREV_LINK] += '&{}={}'.format( | ||||
param, additional_params[param]) | ||||
neko259
|
r1165 | if current_page.has_next(): | ||
params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) | ||||
neko259
|
r1302 | for param in additional_params.keys(): | ||
params[PARAMETER_NEXT_LINK] += '&{}={}'.format( | ||||
param, additional_params[param]) | ||||
neko259
|
r1165 | |||
def get_previous_page_link(self, current_page): | ||||
neko259
|
r1204 | return reverse('feed') + '?page={}'.format( | ||
current_page.previous_page_number()) | ||||
neko259
|
r1165 | |||
def get_next_page_link(self, current_page): | ||||
neko259
|
r1204 | return reverse('feed') + '?page={}'.format( | ||
current_page.next_page_number()) | ||||