|
4 | 4 | from django.shortcuts import render, get_object_or_404 |
5 | 5 | from django.template.context_processors import csrf |
6 | 6 | from django.contrib.auth.decorators import login_required |
7 | | -from django.db.models import Q |
| 7 | +from django.db.models import Q, OuterRef, Subquery, Max, Count |
8 | 8 | from django.core.mail import EmailMultiAlternatives |
9 | 9 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
10 | 10 | from django.contrib.auth import get_user_model |
|
17 | 17 | from website.templatetags.permission_tags import can_edit, can_hide_delete |
18 | 18 | from spoken_auth.models import FossCategory |
19 | 19 | from .sortable import SortableHeader, get_sorted_list, get_field_index |
20 | | -from django.db.models import Count |
21 | 20 |
|
22 | 21 |
|
23 | 22 | User = get_user_model() |
|
32 | 31 | def home(request): |
33 | 32 | questions = Question.objects.filter(status=1).order_by('date_created').reverse()[:100] |
34 | 33 | active_questions = Question.objects.filter(status=1, last_active__isnull=False).order_by('last_active').reverse()[:100] |
| 34 | + |
| 35 | + subquery = Question.objects.filter(category=OuterRef('category')).values('category').annotate(max_date=Max('date_created')).values('max_date') |
| 36 | + slider_questions = Question.objects.filter( |
| 37 | + date_created=Subquery(subquery) |
| 38 | + ).order_by('category') |
| 39 | + |
35 | 40 | context = { |
36 | 41 | 'categories': categories, |
37 | 42 | 'questions': questions, |
38 | | - 'active_questions':active_questions |
| 43 | + 'active_questions':active_questions, |
| 44 | + 'slider_questions': slider_questions |
39 | 45 | } |
40 | 46 | return render(request, "website/templates/index.html", context) |
41 | 47 |
|
|
0 commit comments