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 POSTS_PER_PAGE = settings.get_int('View', 'PostsPerPage') PARAMETER_CURRENT_PAGE = 'current_page' PARAMETER_PAGINATOR = 'paginator' PARAMETER_POSTS = 'posts' PARAMETER_PREV_LINK = 'prev_page_link' PARAMETER_NEXT_LINK = 'next_page_link' TEMPLATE = 'boards/feed.html' DEFAULT_PAGE = 1 class FeedFilter: @staticmethod def get_filtered_posts(request, posts): return posts 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) return filtered_posts 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 class HashFilter(FeedFilter): @staticmethod def get_filtered_posts(request, posts): filtered_posts = posts image_hash = request.GET.get('image_hash', None) if image_hash: filtered_posts = filtered_posts.filter(attachments__hash=image_hash) return filtered_posts class FeedView(PostMixin, BaseBoardView): filters = ( TripcodeFilter, FavoritesFilter, IpFilter, HashFilter, ) 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') for filter in self.filters: posts = filter.get_filtered_posts(request, posts) 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()) self.get_page_context(paginator, params, page) return render(request, TEMPLATE, params) # TODO Dedup this into PagedMixin def get_page_context(self, paginator, params, page): """ 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] = paginator.get_page_url( current_page.previous_page_number()) if current_page.has_next(): params[PARAMETER_NEXT_LINK] = paginator.get_page_url( current_page.next_page_number())