From 42add402cfd7c71914f44c3772647886698dbdf3 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 24 Feb 2014 19:35:13 -0500 Subject: [PATCH] attempted speed enhancement for #342 --- karmaworld/apps/courses/templatetags/__init__.py | 0 karmaworld/apps/courses/templatetags/dict_filters.py | 8 ++++++++ karmaworld/apps/courses/views.py | 7 +++++++ karmaworld/templates/courses/course_list_entry.html | 3 ++- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 karmaworld/apps/courses/templatetags/__init__.py create mode 100644 karmaworld/apps/courses/templatetags/dict_filters.py diff --git a/karmaworld/apps/courses/templatetags/__init__.py b/karmaworld/apps/courses/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/karmaworld/apps/courses/templatetags/dict_filters.py b/karmaworld/apps/courses/templatetags/dict_filters.py new file mode 100644 index 0000000..cad27ad --- /dev/null +++ b/karmaworld/apps/courses/templatetags/dict_filters.py @@ -0,0 +1,8 @@ +from django import template + +register = template.Library() + +@register.filter(name='get') +def get(obj, key): + """ Call getitem against the given key. """ + return obj[key] diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index b88fe7b..5b68ad0 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -5,6 +5,7 @@ import json +from django.db.models import Sum from django.core import serializers from django.core.exceptions import MultipleObjectsReturned from django.core.exceptions import ObjectDoesNotExist @@ -49,6 +50,12 @@ class CourseListView(ListView, ModelFormMixin, ProcessFormView): # Include "Add Course" button in header context['display_add_course'] = True + # Include courses to number of total note thanks mapping + # See Course.get_popularity for a more detailed explanation. + thanks = Course.objects.values('id').annotate(sum=Sum('note__thanks')) + # Create a generator to convert the list of dicts into a dict. + context['course_thanks'] = dict((x['id'],x['sum'] or 0) for x in thanks) + return context def get_success_url(self): diff --git a/karmaworld/templates/courses/course_list_entry.html b/karmaworld/templates/courses/course_list_entry.html index 18fd2fc..b1417f0 100644 --- a/karmaworld/templates/courses/course_list_entry.html +++ b/karmaworld/templates/courses/course_list_entry.html @@ -1,8 +1,9 @@ +{% load dict_filters %} Updated {{ course.updated_at|date:"b d, o"|capfirst }} {{ course.file_count }} Notes - {{ course.get_popularity }} Thanks + {{ course_thanks|get:course.id }} Thanks {% if course.department %}{{course.department.name}}: {% endif %}{{ course.name }} -- 2.25.1