Commit 16495a78 authored by Tang, Alexander's avatar Tang, Alexander
Browse files

Anzeige der Blogeinträge nun wie folgt: letzten 3 zuerst, alle anderen nach Zufallsprinzip

parent fcc3c100
......@@ -2,6 +2,10 @@ from django.http import HttpResponseNotAllowed, Http404
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from django.db.models import Q
from itertools import chain
from random import shuffle
from .forms import CommentForm
from .models import *
......@@ -15,7 +19,14 @@ 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}'
blogs = Blog.objects.extra(where=[query]).order_by('created')
noOfBlogs = Blog.objects.filter(~Q(status = 1)).count()
# 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)
return render(request, 'blog/all_blogs.html', {'blogs': blogs})
......
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