Commit dd6b2997 authored by Tang, Alexander's avatar Tang, Alexander
Browse files

Blog-Shuffling-Mechanismus angepasst

parent 16495a78
......@@ -19,16 +19,17 @@ def all_blogs(request):
"""
# Exclude those with status hidden from the overall list
query = f'status & {BLOG_STATE_HIDE_FROM_LIST} <> {BLOG_STATE_HIDE_FROM_LIST}'
noOfBlogs = Blog.objects.filter(~Q(status = 1)).count()
allblogs = Blog.objects.extra(where=[query]).order_by('-created')
# 2 queries turned into lists for shuffling, then being chained
lastThreeBlogs = list(Blog.objects.extra(where=[query]).order_by('-date')[:3])
allBlogsExceptLastThree = Blog.objects.filter(~Q(status = 1)).order_by('date')[:(noOfBlogs-3)]
allBlogsExceptLastThreeInRandomOrder = list(allBlogsExceptLastThree)
shuffle(allBlogsExceptLastThreeInRandomOrder)
blogs = chain(lastThreeBlogs, allBlogsExceptLastThreeInRandomOrder)
# split into subqueries, turn into lists for shuffling, then being chained
if allblogs.count() > 3:
last_three_blogs = allblogs[:3]
all_blogs_except_last_three_in_random_order = list(allblogs[3:])
shuffle(all_blogs_except_last_three_in_random_order)
allblogs = chain(last_three_blogs, all_blogs_except_last_three_in_random_order)
return render(request, 'blog/all_blogs.html', {'blogs': blogs})
return render(request, 'blog/all_blogs.html', {'blogs': allblogs})
def detail(request, slug):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment