attempted speed enhancement for #342
authorBryan <btbonval@gmail.com>
Tue, 25 Feb 2014 00:35:13 +0000 (19:35 -0500)
committerBryan <btbonval@gmail.com>
Tue, 25 Feb 2014 00:35:13 +0000 (19:35 -0500)
karmaworld/apps/courses/templatetags/__init__.py [new file with mode: 0644]
karmaworld/apps/courses/templatetags/dict_filters.py [new file with mode: 0644]
karmaworld/apps/courses/views.py
karmaworld/templates/courses/course_list_entry.html

diff --git a/karmaworld/apps/courses/templatetags/__init__.py b/karmaworld/apps/courses/templatetags/__init__.py
new file mode 100644 (file)
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 (file)
index 0000000..cad27ad
--- /dev/null
@@ -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]
index b88fe7b5a2be99601bac918a044ddb0a4c71114f..5b68ad051a45c3d9ef595d48b19df25849580f59 100644 (file)
@@ -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):
index 18fd2fca2588fc105c88eecb898f7ffd051d6b34..b1417f0982226110e287c86e36f3dd851696d1a8 100644 (file)
@@ -1,8 +1,9 @@
+{% load dict_filters %}
 <tr>
   <td class="no-display"><!-- this spot solely for the Sort By header --></td>
   <td class="small-8 columns large-4 slim"><span>Updated {{ course.updated_at|date:"b d, o"|capfirst }}</span></td>
   <td class="small-4 columns large-2 large-offset-2 slim"><span>{{ course.file_count }} Notes</span></td>
-  <td class="small-4 columns large-2 large-offset-1 slim"><span>{{ course.get_popularity }} Thanks</span></td>
+  <td class="small-4 columns large-2 large-offset-1 slim"><span>{{ course_thanks|get:course.id }} Thanks</span></td>
 
   <td class="small-12 large-5 columns">
     <a href="{{ course.get_absolute_url }}">{% if course.department %}{{course.department.name}}: {% endif %}{{ course.name }}</a>