##// END OF EJS Templates
Moved pagination related stuff to the mixin
Moved pagination related stuff to the mixin

File last commit:

r1854:b4c06973 default
r1854:b4c06973 default
Show More
feed.py
131 lines | 3.7 KiB | text/x-python | PythonLexer
from django.core.urlresolvers import reverse
from django.shortcuts import render
from boards import settings
from boards.abstracts.paginator import get_paginator
from boards.abstracts.settingsmanager import get_settings_manager
from boards.models import Post
from boards.views.base import BaseBoardView
from boards.views.posting_mixin import PostMixin
from boards.views.mixins import PaginatedMixin
POSTS_PER_PAGE = settings.get_int('View', 'PostsPerPage')
PARAMETER_POSTS = 'posts'
PARAMETER_QUERIES = 'queries'
TEMPLATE = 'boards/feed.html'
DEFAULT_PAGE = 1
class FeedFilter:
@staticmethod
def get_filtered_posts(request, posts):
return posts
@staticmethod
def get_query(request):
return None
class TripcodeFilter(FeedFilter):
@staticmethod
def get_filtered_posts(request, posts):
filtered_posts = posts
tripcode = request.GET.get('tripcode', None)
if tripcode:
filtered_posts = filtered_posts.filter(tripcode=tripcode)
return filtered_posts
@staticmethod
def get_query(request):
tripcode = request.GET.get('tripcode', None)
if tripcode:
return 'Tripcode: {}'.format(tripcode)
class FavoritesFilter(FeedFilter):
@staticmethod
def get_filtered_posts(request, posts):
filtered_posts = posts
favorites = 'favorites' in request.GET
if favorites:
settings_manager = get_settings_manager(request)
fav_thread_ops = Post.objects.filter(id__in=settings_manager.get_fav_threads().keys())
fav_threads = [op.get_thread() for op in fav_thread_ops]
filtered_posts = filtered_posts.filter(thread__in=fav_threads)
return filtered_posts
class IpFilter(FeedFilter):
@staticmethod
def get_filtered_posts(request, posts):
filtered_posts = posts
ip = request.GET.get('ip', None)
if ip and request.user.has_perm('post_delete'):
filtered_posts = filtered_posts.filter(poster_ip=ip)
return filtered_posts
@staticmethod
def get_query(request):
ip = request.GET.get('ip', None)
if ip:
return 'IP: {}'.format(ip)
class ImageFilter(FeedFilter):
@staticmethod
def get_filtered_posts(request, posts):
filtered_posts = posts
image = request.GET.get('image', None)
if image:
filtered_posts = filtered_posts.filter(attachments__file=image)
return filtered_posts
@staticmethod
def get_query(request):
image = request.GET.get('image', None)
if image:
return 'File: {}'.format(image)
class FeedView(PostMixin, PaginatedMixin, BaseBoardView):
filters = (
TripcodeFilter,
FavoritesFilter,
IpFilter,
ImageFilter,
)
def get(self, request):
page = request.GET.get('page', DEFAULT_PAGE)
params = self.get_context_data(request=request)
settings_manager = get_settings_manager(request)
posts = Post.objects.exclude(
thread__tags__in=settings_manager.get_hidden_tags()).order_by(
'-pub_time').prefetch_related('attachments', 'thread')
queries = []
for filter in self.filters:
posts = filter.get_filtered_posts(request, posts)
query = filter.get_query(request)
if query:
queries.append(query)
params[PARAMETER_QUERIES] = queries
paginator = get_paginator(posts, POSTS_PER_PAGE)
paginator.current_page = int(page)
params[PARAMETER_POSTS] = paginator.page(page).object_list
paginator.set_url(reverse('feed'), request.GET.dict())
params.update(self.get_page_context(paginator, page))
return render(request, TEMPLATE, params)