|
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 | + # Retrieve latest questions per category for the slider |
| 36 | + subquery = Question.objects.filter(category=OuterRef('category'), status=1).values('category').annotate(max_date=Max('date_created')).values('max_date') |
| 37 | + slider_questions = Question.objects.filter( |
| 38 | + date_created=Subquery(subquery), status=1 |
| 39 | + ).order_by('category') |
| 40 | + |
| 41 | + # Mapping of foss name as in spk db & its corresponding category name in forums db |
| 42 | + category_fosses = {val.replace(" ", "-") : val for val in categories} |
| 43 | + |
| 44 | + # All eligible categories to be shown in homepage slider |
| 45 | + all_eligible_categories = list(category_fosses.keys()) |
| 46 | + |
| 47 | + # Create a dictionary to map categories to questions for the slider |
| 48 | + category_question_map = {} |
| 49 | + for question in slider_questions: |
| 50 | + if question.category in all_eligible_categories: |
| 51 | + foss = category_fosses.get(question.category) |
| 52 | + category_question_map[foss] = {"id" : question.id, "question": question.title, "foss_url": question.category} |
| 53 | + |
| 54 | + # Fill in missing categories without questions |
| 55 | + for category in category_fosses.keys(): |
| 56 | + foss = category_fosses.get(category) |
| 57 | + if foss not in category_question_map: |
| 58 | + category_question_map[foss] = None |
| 59 | + |
| 60 | + # Sort category_question_map by category name |
| 61 | + category_question_map = dict(sorted(category_question_map.items(), key= lambda item: item[0].lower())) |
35 | 62 | context = { |
36 | | - 'categories': categories, |
37 | | - 'questions': questions, |
38 | | - 'active_questions':active_questions |
39 | | - } |
| 63 | + 'questions': questions, |
| 64 | + 'active_questions':active_questions, |
| 65 | + 'category_question_map': category_question_map |
| 66 | +} |
40 | 67 | return render(request, "website/templates/index.html", context) |
41 | 68 |
|
42 | 69 |
|
|
0 commit comments