|
|
from django.urls import reverse
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
from boards import settings
|
|
|
from boards.abstracts.constants import PARAM_PAGE
|
|
|
from boards.abstracts.paginator import get_paginator
|
|
|
from boards.abstracts.settingsmanager import get_settings_manager
|
|
|
from boards.models import Post
|
|
|
from boards.settings import SECTION_VIEW
|
|
|
from boards.views.base import BaseBoardView
|
|
|
from boards.views.mixins import PaginatedMixin
|
|
|
|
|
|
POSTS_PER_PAGE = settings.get_int(SECTION_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)
|
|
|
last_posts = settings_manager.get_last_posts()
|
|
|
threads = [post.get_thread() for post in last_posts]
|
|
|
filtered_posts = filtered_posts.filter(thread__in=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(PaginatedMixin, BaseBoardView):
|
|
|
filters = (
|
|
|
TripcodeFilter,
|
|
|
FavoritesFilter,
|
|
|
IpFilter,
|
|
|
ImageFilter,
|
|
|
)
|
|
|
|
|
|
def get(self, request):
|
|
|
page = request.GET.get(PARAM_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,
|
|
|
link=reverse('feed'),
|
|
|
params=request.GET.dict())
|
|
|
paginator.current_page = int(page)
|
|
|
|
|
|
params[PARAMETER_POSTS] = paginator.page(page).object_list
|
|
|
|
|
|
params.update(self.get_page_context(paginator, page))
|
|
|
|
|
|
return render(request, TEMPLATE, params)
|
|
|
|
|
|
|