From 7f5ed18a2c8ca7cb7e766d50428c82d60002bc95 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 24 Feb 2014 18:26:11 -0500 Subject: [PATCH] Aggregate thanks over courses closes #342 . Cleaned up some DataTable code. --- karmaworld/apps/courses/models.py | 9 +++++ karmaworld/templates/courses/course_list.html | 38 +++++-------------- .../templates/courses/course_list_entry.html | 5 ++- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 6639180..573bfe4 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -274,6 +274,15 @@ class Course(models.Model): self.file_count = self.note_set.count() self.save() + def get_popularity(self): + """ Aggregate popularity of notes contained within. """ + # Run an efficient GROUP BY aggregation within the database. + # It returns {'fieldname': #}, where fieldname is set in the left hand + # side of the aggregate kwarg. Call the field x and retrieve the dict + # value using that key. + # The value might be None, return zero in that case with shortcut logic. + return self.note_set.aggregate(x=models.Sum('thanks'))['x'] or 0 + reversion.register(Course) class ProfessorTaughtManager(models.Manager): diff --git a/karmaworld/templates/courses/course_list.html b/karmaworld/templates/courses/course_list.html index 45a2ccc..c7d16ed 100644 --- a/karmaworld/templates/courses/course_list.html +++ b/karmaworld/templates/courses/course_list.html @@ -41,44 +41,23 @@ $(document).ready(function() { 'sDom': '<"top"f>rt<"bottom"p><"clear">', // Specify options for each column "aoColumnDefs": [ + { + // 4th element: thanks + "aTargets": [ 3 ], + "bSortable": true, + "bVisible": true + }, { // 3rd element: notes "aTargets": [ 2 ], "bSortable": true, - "bVisible": true, - "mData": function ( source, type, val ) { - //console.log(source); - if (type === 'set') { - source.count = val; - // Store the computed dislay and filter values for efficiency - source.count_display = val=="" ? "" : ""+val+" Notes"; - return; - } - else if (type === 'display') { - return source.count_display; - } - // 'sort', 'type', 'filter' and undefined all just use the integer - return source.count; - } + "bVisible": true }, { // 2nd element: date "aTargets": [ 1 ], "bSortable": true, - "bVisible": true, - "mData": function ( source, type, val ) { - //console.log(source); - if (type === 'set') { - source.date = val; - source.date_display = val=="" ? "" : "Updated "+val+""; - return; - } - else if (type === 'display') { - return source.date_display; - } - // for types 'sort', 'type', 'filter' and undefined use raw date - return source.date; - } + "bVisible": true }, { // 1st element: "sort by" label @@ -118,6 +97,7 @@ $(document).ready(function() { Sort by: Date #Notes + Popularity Upload Course Name Institution diff --git a/karmaworld/templates/courses/course_list_entry.html b/karmaworld/templates/courses/course_list_entry.html index 0f2ac62..18fd2fc 100644 --- a/karmaworld/templates/courses/course_list_entry.html +++ b/karmaworld/templates/courses/course_list_entry.html @@ -1,7 +1,8 @@ - {{ course.updated_at|date:"b d, o"|capfirst }} - {{ course.file_count }} + Updated {{ course.updated_at|date:"b d, o"|capfirst }} + {{ course.file_count }} Notes + {{ course.get_popularity }} Thanks {% if course.department %}{{course.department.name}}: {% endif %}{{ course.name }} -- 2.25.1