diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -118,8 +118,6 @@ class PostImage(models.Model, Viewable): str(self.pre_height), str(self.width), str(self.height), full=self.image.url, image_meta=metadata) - def get_random_associated_post(self, tags=None): + def get_random_associated_post(self): posts = boards.models.Post.objects.filter(images__in=[self]) - if tags is not None: - posts = posts.filter(threads__tags__in=tags) return posts.order_by('?').first() diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -5,7 +5,7 @@ from django.core.urlresolvers import rev from boards.models.base import Viewable from boards.utils import cached_result - +import boards __author__ = 'neko259' @@ -90,3 +90,9 @@ class Tag(models.Model, Viewable): def get_description(self): return self.description + + def get_random_image_post(self): + return boards.models.Post.objects.annotate(images_count=Count( + 'images')).filter(images_count__gt=0, threads__tags__in=[self])\ + .order_by('?').first() + diff --git a/boards/templates/boards/all_threads.html b/boards/templates/boards/all_threads.html --- a/boards/templates/boards/all_threads.html +++ b/boards/templates/boards/all_threads.html @@ -39,10 +39,12 @@ {% if tag %}