feed.py
130 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
|
r1986 | from django.urls import reverse | ||
|
r1302 | from django.shortcuts import render | ||
|
r1165 | |||
|
r1681 | from boards import settings | ||
|
r1165 | from boards.abstracts.paginator import get_paginator | ||
|
r1170 | from boards.abstracts.settingsmanager import get_settings_manager | ||
|
r1302 | from boards.models import Post | ||
|
r1165 | from boards.views.base import BaseBoardView | ||
|
r1854 | from boards.views.mixins import PaginatedMixin | ||
|
r1165 | |||
|
r1681 | POSTS_PER_PAGE = settings.get_int('View', 'PostsPerPage') | ||
|
r1165 | |||
PARAMETER_POSTS = 'posts' | ||||
|
r1823 | PARAMETER_QUERIES = 'queries' | ||
|
r1165 | |||
TEMPLATE = 'boards/feed.html' | ||||
DEFAULT_PAGE = 1 | ||||
|
r1802 | class FeedFilter: | ||
@staticmethod | ||||
def get_filtered_posts(request, posts): | ||||
return posts | ||||
|
r1823 | @staticmethod | ||
def get_query(request): | ||||
return None | ||||
|
r1802 | |||
class TripcodeFilter(FeedFilter): | ||||
@staticmethod | ||||
def get_filtered_posts(request, posts): | ||||
filtered_posts = posts | ||||
tripcode = request.GET.get('tripcode', None) | ||||
if tripcode: | ||||
|
r1804 | filtered_posts = filtered_posts.filter(tripcode=tripcode) | ||
|
r1802 | return filtered_posts | ||
|
r1823 | @staticmethod | ||
def get_query(request): | ||||
tripcode = request.GET.get('tripcode', None) | ||||
if tripcode: | ||||
return 'Tripcode: {}'.format(tripcode) | ||||
|
r1802 | |||
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 | ||||
|
r1823 | @staticmethod | ||
def get_query(request): | ||||
ip = request.GET.get('ip', None) | ||||
if ip: | ||||
return 'IP: {}'.format(ip) | ||||
|
r1802 | |||
|
r1827 | class ImageFilter(FeedFilter): | ||
|
r1802 | @staticmethod | ||
def get_filtered_posts(request, posts): | ||||
filtered_posts = posts | ||||
|
r1827 | image = request.GET.get('image', None) | ||
if image: | ||||
filtered_posts = filtered_posts.filter(attachments__file=image) | ||||
|
r1802 | return filtered_posts | ||
|
r1823 | @staticmethod | ||
def get_query(request): | ||||
|
r1827 | image = request.GET.get('image', None) | ||
if image: | ||||
return 'File: {}'.format(image) | ||||
|
r1823 | |||
|
r1802 | |||
|
r1997 | class FeedView(PaginatedMixin, BaseBoardView): | ||
|
r1802 | filters = ( | ||
TripcodeFilter, | ||||
FavoritesFilter, | ||||
IpFilter, | ||||
|
r1827 | ImageFilter, | ||
|
r1802 | ) | ||
|
r1165 | |||
|
r1204 | def get(self, request): | ||
page = request.GET.get('page', DEFAULT_PAGE) | ||||
|
r1165 | params = self.get_context_data(request=request) | ||
|
r1170 | settings_manager = get_settings_manager(request) | ||
|
r1302 | posts = Post.objects.exclude( | ||
|
r1704 | thread__tags__in=settings_manager.get_hidden_tags()).order_by( | ||
'-pub_time').prefetch_related('attachments', 'thread') | ||||
|
r1823 | queries = [] | ||
|
r1802 | for filter in self.filters: | ||
posts = filter.get_filtered_posts(request, posts) | ||||
|
r1823 | query = filter.get_query(request) | ||
if query: | ||||
queries.append(query) | ||||
params[PARAMETER_QUERIES] = queries | ||||
|
r1302 | |||
paginator = get_paginator(posts, POSTS_PER_PAGE) | ||||
|
r1165 | paginator.current_page = int(page) | ||
params[PARAMETER_POSTS] = paginator.page(page).object_list | ||||
|
r1378 | paginator.set_url(reverse('feed'), request.GET.dict()) | ||
|
r1854 | params.update(self.get_page_context(paginator, page)) | ||
|
r1165 | |||
return render(request, TEMPLATE, params) | ||||