diff --git a/boards/config/default_settings.ini b/boards/config/default_settings.ini --- a/boards/config/default_settings.ini +++ b/boards/config/default_settings.ini @@ -43,3 +43,6 @@ ArchiveThreads = true [RSS] MaxItems = 20 + +[External] +ImageSearchHost=http://127.0.0.1/ diff --git a/boards/templates/boards/thread_gallery.html b/boards/templates/boards/thread_gallery.html --- a/boards/templates/boards/thread_gallery.html +++ b/boards/templates/boards/thread_gallery.html @@ -24,7 +24,8 @@ {{ image.get_view }}
diff --git a/boards/templatetags/board.py b/boards/templatetags/board.py --- a/boards/templatetags/board.py +++ b/boards/templatetags/board.py @@ -3,8 +3,10 @@ import re from django.shortcuts import get_object_or_404 from django import template from django.utils.text import re_tag +from django.core.urlresolvers import reverse from boards.mdx_neboard import LINE_BREAK_HTML +from boards import settings IMG_ACTION_URL = '[{}]' @@ -20,11 +22,11 @@ register = template.Library() actions = [ { 'name': 'google', - 'link': 'http://google.com/searchbyimage?image_url=%s', + 'link': 'https://www.google.com/searchbyimage?image_url={}', }, { 'name': 'iqdb', - 'link': 'http://iqdb.org/?url=%s', + 'link': 'http://iqdb.org/?url={}', }, ] @@ -41,11 +43,13 @@ def post_url(*args, **kwargs): @register.simple_tag(name='image_actions') def image_actions(*args, **kwargs): image_link = args[0] - if len(args) > 1: - image_link = 'http://' + args[1] + image_link # TODO https? + host = settings.get('External', 'ImageSearchHost') + if host.endswith('/'): + host = host[:-1] + image_link = settings.get('External', 'ImageSearchHost') + image_link return ', '.join([IMG_ACTION_URL.format( - action['link'] % image_link, action['name']) for action in actions]) + action['link'].format(image_link), action['name']) for action in actions]) @register.inclusion_tag('boards/post.html', name='post_view', takes_context=True) diff --git a/boards/views/feed.py b/boards/views/feed.py --- a/boards/views/feed.py +++ b/boards/views/feed.py @@ -21,13 +21,68 @@ 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) - tripcode = request.GET.get('tripcode', None) - favorites = 'favorites' in request.GET - ip = request.GET.get('ip', None) params = self.get_context_data(request=request) @@ -36,14 +91,8 @@ class FeedView(PostMixin, BaseBoardView) posts = Post.objects.exclude( thread__tags__in=settings_manager.get_hidden_tags()).order_by( '-pub_time').prefetch_related('attachments', 'thread') - if tripcode: - posts = posts.filter(tripcode=tripcode) - if favorites: - 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] - posts = posts.filter(thread__in=fav_threads) - if ip and request.user.has_perm('post_delete'): - posts = posts.filter(poster_ip=ip) + for filter in self.filters: + posts = filter.get_filtered_posts(request, posts) paginator = get_paginator(posts, POSTS_PER_PAGE) paginator.current_page = int(page)