From: Bryan Date: Tue, 25 Feb 2014 00:35:13 +0000 (-0500) Subject: attempted speed enhancement for #342 X-Git-Tag: release-20150131~163^2~6 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=42add402cfd7c71914f44c3772647886698dbdf3;p=oweals%2Fkarmaworld.git attempted speed enhancement for #342 --- 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 }}