Skip to content

Commit 35102bd

Browse files
committed
modifications to include all categories
1 parent 0765203 commit 35102bd

2 files changed

Lines changed: 64 additions & 33 deletions

File tree

static/website/templates/index.html

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,45 @@
2525

2626

2727
</script>
28-
<div id="carousel-container">
29-
<div class="carousel">
30-
{% for item in slider_questions %}
31-
<div>
32-
<div class="thumbnail">
33-
{% with file=item.category|get_category_image %}
34-
{% if file %}
35-
<img src="{% static item.category|get_category_image %}">
36-
{% else %}
37-
<div class="category-title">{{item.category}}</div>
38-
{% endif %}
39-
{% endwith %}
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 %}
4040

41-
<div class="caption">
42-
<small class="category">
43-
{{ item.category }}
44-
</small>
45-
<small class="latest">
46-
<a href="{% url 'website:get_question' item.id %}">{{ item.title }}</a>
47-
</small>
48-
<a class="btn btn-xs btn-block btn-primary" href="{% url 'website:filter' item.category %}">View previous questions</a>
49-
50-
<a class="btn btn-xs btn-block btn-success" href="{% url 'website:new_question' %}?category={{ item.category|urlencode }}">Ask new question</a>
51-
</div>
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>
5260
</div>
5361
</div>
54-
{% endfor %}
55-
</div> <!-- /.carousel -->
56-
</div> <!-- /#carousel-container -->
62+
</div>
63+
{% endfor %}
64+
</div> <!-- /.carousel -->
65+
</div> <!-- /#carousel-container -->
66+
5767

5868
<div id="filter-container">
5969
<div class="row">

website/views.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,38 @@ def home(request):
3232
questions = Question.objects.filter(status=1).order_by('date_created').reverse()[:100]
3333
active_questions = Question.objects.filter(status=1, last_active__isnull=False).order_by('last_active').reverse()[:100]
3434

35-
subquery = Question.objects.filter(category=OuterRef('category')).values('category').annotate(max_date=Max('date_created')).values('max_date')
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')
3637
slider_questions = Question.objects.filter(
37-
date_created=Subquery(subquery)
38+
date_created=Subquery(subquery), status=1
3839
).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}
3953

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()))
4062
context = {
41-
'categories': categories,
42-
'questions': questions,
43-
'active_questions':active_questions,
44-
'slider_questions': slider_questions
45-
}
63+
'questions': questions,
64+
'active_questions':active_questions,
65+
'category_question_map': category_question_map
66+
}
4667
return render(request, "website/templates/index.html", context)
4768

4869

0 commit comments

Comments
 (0)