##// END OF EJS Templates
Speed up getting tag's random image
neko259 -
r1264:50888044 default
parent child Browse files
Show More
@@ -118,8 +118,6 b' class PostImage(models.Model, Viewable):'
118 str(self.pre_height), str(self.width), str(self.height),
118 str(self.pre_height), str(self.width), str(self.height),
119 full=self.image.url, image_meta=metadata)
119 full=self.image.url, image_meta=metadata)
120
120
121 def get_random_associated_post(self, tags=None):
121 def get_random_associated_post(self):
122 posts = boards.models.Post.objects.filter(images__in=[self])
122 posts = boards.models.Post.objects.filter(images__in=[self])
123 if tags is not None:
124 posts = posts.filter(threads__tags__in=tags)
125 return posts.order_by('?').first()
123 return posts.order_by('?').first()
@@ -5,7 +5,7 b' from django.core.urlresolvers import rev'
5
5
6 from boards.models.base import Viewable
6 from boards.models.base import Viewable
7 from boards.utils import cached_result
7 from boards.utils import cached_result
8
8 import boards
9
9
10 __author__ = 'neko259'
10 __author__ = 'neko259'
11
11
@@ -90,3 +90,9 b' class Tag(models.Model, Viewable):'
90
90
91 def get_description(self):
91 def get_description(self):
92 return self.description
92 return self.description
93
94 def get_random_image_post(self):
95 return boards.models.Post.objects.annotate(images_count=Count(
96 'images')).filter(images_count__gt=0, threads__tags__in=[self])\
97 .order_by('?').first()
98
@@ -39,10 +39,12 b''
39 {% if tag %}
39 {% if tag %}
40 <div class="tag_info">
40 <div class="tag_info">
41 <div class="tag-image">
41 <div class="tag-image">
42 {% with image=random_image_post.images.first %}
42 <a href="{{ random_image_post.get_absolute_url }}"><img
43 <a href="{{ random_image_post.get_absolute_url }}"><img
43 src="{{ random_image.image.url_200x150 }}"
44 src="{{ image.image.url_200x150 }}"
44 width="{{ random_image.pre_width }}"
45 width="{{ image.pre_width }}"
45 height="{{ random_image.pre_height }}"/></a>
46 height="{{ image.pre_height }}"/></a>
47 {% endwith %}
46 </div>
48 </div>
47 <div class="tag-text-data">
49 <div class="tag-text-data">
48 <h2>
50 <h2>
@@ -12,7 +12,6 b" PARAM_HIDDEN_TAGS = 'hidden_tags'"
12 PARAM_TAG = 'tag'
12 PARAM_TAG = 'tag'
13 PARAM_IS_FAVORITE = 'is_favorite'
13 PARAM_IS_FAVORITE = 'is_favorite'
14 PARAM_IS_HIDDEN = 'is_hidden'
14 PARAM_IS_HIDDEN = 'is_hidden'
15 PARAM_RANDOM_IMAGE = 'random_image'
16 PARAM_RANDOM_IMAGE_POST = 'random_image_post'
15 PARAM_RANDOM_IMAGE_POST = 'random_image_post'
17
16
18 __author__ = 'neko259'
17 __author__ = 'neko259'
@@ -49,9 +48,7 b' class TagView(AllThreadsView, Dispatcher'
49 params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names
48 params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names
50 params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names
49 params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names
51
50
52 image = PostImage.objects.get_random_images(1, tags=[tag])[0]
51 params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post()
53 params[PARAM_RANDOM_IMAGE] = image
54 params[PARAM_RANDOM_IMAGE_POST] = image.get_random_associated_post(tags=[tag])
55
52
56 return params
53 return params
57
54
General Comments 0
You need to be logged in to leave comments. Login now