##// END OF EJS Templates
Don't return posts with the same image in random view
neko259 -
r1249:93f2906a default
parent child Browse files
Show More
@@ -2,9 +2,12 b' import hashlib'
2 import os
2 import os
3 from random import random
3 from random import random
4 import time
4 import time
5
5 from django.db import models
6 from django.db import models
6 from django.template.defaultfilters import filesizeformat
7 from django.template.defaultfilters import filesizeformat
8
7 from boards import thumbs
9 from boards import thumbs
10 import boards
8 from boards.models.base import Viewable
11 from boards.models.base import Viewable
9
12
10 __author__ = 'neko259'
13 __author__ = 'neko259'
@@ -39,6 +42,9 b' class PostImageManager(models.Manager):'
39 md5.update(chunk)
42 md5.update(chunk)
40 return md5.hexdigest()
43 return md5.hexdigest()
41
44
45 def get_random_images(self, count):
46 return self.order_by('?')[:count]
47
42
48
43 class PostImage(models.Model, Viewable):
49 class PostImage(models.Model, Viewable):
44 objects = PostImageManager()
50 objects = PostImageManager()
@@ -108,3 +114,7 b' class PostImage(models.Model, Viewable):'
108 str(self.hash), str(self.pre_width),
114 str(self.hash), str(self.pre_width),
109 str(self.pre_height), str(self.width), str(self.height),
115 str(self.pre_height), str(self.width), str(self.height),
110 full=self.image.url, image_meta=metadata)
116 full=self.image.url, image_meta=metadata)
117
118 def get_random_associated_post(self):
119 return boards.models.Post.objects.filter(images__in=[self])\
120 .order_by('?').first()
@@ -8,17 +8,17 b''
8
8
9 {% block content %}
9 {% block content %}
10
10
11 {% if posts %}
11 {% if images %}
12 <div class="random-images-table">
12 <div class="random-images-table">
13 <div>
13 <div>
14 {% for post in posts %}
14 {% for image in images %}
15 <div class="gallery_image">
15 <div class="gallery_image">
16 {% with post.get_first_image as image %}
17 {% autoescape off %}
16 {% autoescape off %}
18 {{ image.get_view }}
17 {{ image.get_view }}
19 {% endautoescape %}
18 {% endautoescape %}
20 <a href="{{ post.get_absolute_url }}">>>{{ post.id }}</a>
19 {% with image.get_random_associated_post as post %}
21 {% endwith %}
20 <a href="{{ post.get_absolute_url }}">>>{{ post.id }}</a>
21 {% endwith %}
22 </div>
22 </div>
23 {% if forloop.counter|divisibleby:"3" %}
23 {% if forloop.counter|divisibleby:"3" %}
24 </div>
24 </div>
@@ -1,16 +1,13 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.template import RequestContext
3 from django.views.generic import View
2 from django.views.generic import View
4 from django.db.models import Count
5
3
6 from boards.models import Post
4 from boards.models import PostImage
7 from boards.mdx_neboard import Parser
8
5
9 __author__ = 'neko259'
6 __author__ = 'neko259'
10
7
11 TEMPLATE = 'boards/random.html'
8 TEMPLATE = 'boards/random.html'
12
9
13 CONTEXT_POSTS = 'posts'
10 CONTEXT_IMAGES = 'images'
14
11
15 RANDOM_POST_COUNT = 9
12 RANDOM_POST_COUNT = 9
16
13
@@ -19,10 +16,7 b' class RandomImageView(View):'
19 def get(self, request):
16 def get(self, request):
20 params = dict()
17 params = dict()
21
18
22 posts = Post.objects.annotate(images_count=Count(
19 params[CONTEXT_IMAGES] = PostImage.objects.get_random_images(
23 'images')).filter(images_count__gt=0).order_by('?')\
20 RANDOM_POST_COUNT)
24 [:RANDOM_POST_COUNT]
25
26 params[CONTEXT_POSTS] = posts
27
21
28 return render(request, TEMPLATE, params)
22 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now