Skip to content

Commit 563a122

Browse files
authored
Merge pull request #58 from ankitamk14/homepage
query optimization for homepage scroller
2 parents 64e0997 + 35102bd commit 563a122

2 files changed

Lines changed: 70 additions & 28 deletions

File tree

static/website/templates/index.html

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,45 @@
2525

2626

2727
</script>
28-
<div id="carousel-container">
29-
<div class="carousel">
30-
{% for category in categories %}
31-
<div>
32-
<div class="thumbnail">
33-
{% with file=category|get_category_image %}
34-
{% if file %}
35-
<img src="{% static category|get_category_image %}">
36-
{% else %}
37-
<div class="category-title">{{category}}</div>
38-
{% endif %}
39-
{% endwith %}
40-
<div class="caption">
41-
<small class="category">
42-
{{ category }}
43-
</small>
44-
{% latest_question category %}
45-
<a class="btn btn-xs btn-block btn-success" href="{% url 'website:new_question' %}?category={{ category|urlencode }}">Ask new question</a>
46-
</div>
28+
<div id="carousel-container">
29+
<div class="carousel">
30+
{% for category, data in category_question_map.items %}
31+
<div>
32+
<div class="thumbnail">
33+
{% with file=category|get_category_image %}
34+
{% if file %}
35+
<img src="{% static category|get_category_image %}">
36+
{% else %}
37+
<div class="category-title">{{category}}</div>
38+
{% endif %}
39+
{% endwith %}
40+
41+
<div class="caption">
42+
43+
<small class="category">
44+
{{ category }}
45+
</small>
46+
{% if data %}
47+
<small class="latest">
48+
<a href="{% url 'website:get_question' data.id %}">{{ data.question }}</a>
49+
</small>
50+
<a class="btn btn-xs btn-block btn-primary" href="{% url 'website:filter' data.foss_url %}">View previous questions</a>
51+
{% else %}
52+
<small class="latest">
53+
Be the first to ask question.
54+
</small>
55+
<a class="btn btn-block btn-xs">
56+
No questions to display
57+
</a>
58+
{% endif %}
59+
<a class="btn btn-xs btn-block btn-success" href="{% url 'website:new_question' %}?category={{ category|urlencode }}">Ask new question</a>
4760
</div>
4861
</div>
49-
{% endfor %}
50-
</div> <!-- /.carousel -->
51-
</div> <!-- /#carousel-container -->
62+
</div>
63+
{% endfor %}
64+
</div> <!-- /.carousel -->
65+
</div> <!-- /#carousel-container -->
66+
5267

5368
<div id="filter-container">
5469
<div class="row">

website/views.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.shortcuts import render, get_object_or_404
55
from django.template.context_processors import csrf
66
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
88
from django.core.mail import EmailMultiAlternatives
99
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
1010
from django.contrib.auth import get_user_model
@@ -17,7 +17,6 @@
1717
from website.templatetags.permission_tags import can_edit, can_hide_delete
1818
from spoken_auth.models import FossCategory
1919
from .sortable import SortableHeader, get_sorted_list, get_field_index
20-
from django.db.models import Count
2120

2221

2322
User = get_user_model()
@@ -32,11 +31,39 @@
3231
def home(request):
3332
questions = Question.objects.filter(status=1).order_by('date_created').reverse()[:100]
3433
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()))
3562
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+
}
4067
return render(request, "website/templates/index.html", context)
4168

4269

0 commit comments

Comments
 (0)