Aggregate thanks over courses closes #342 . Cleaned up some DataTable code.
authorBryan <btbonval@gmail.com>
Mon, 24 Feb 2014 23:26:11 +0000 (18:26 -0500)
committerBryan <btbonval@gmail.com>
Mon, 24 Feb 2014 23:26:11 +0000 (18:26 -0500)
karmaworld/apps/courses/models.py
karmaworld/templates/courses/course_list.html
karmaworld/templates/courses/course_list_entry.html

index 66391804b9b15e51baf5fbebcc07c4a00eae27bd..573bfe4db8bf5f6620c646c18785507dcad8b162 100644 (file)
@@ -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):
index 45a2ccc46d2093ba3eb3d7605519864b1dd9e825..c7d16ed7c5ce4085a93bf25ef33f82c982e9ee22 100644 (file)
@@ -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=="" ? "" : "<span>"+val+" Notes</span>";
-            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=="" ? "" : "<span>Updated "+val+"</span>";
-            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() {
             <th id="data-table-sort-by"> Sort by: </th>
             <th id="data-table-date"> Date </th>
             <th id="data-table-note-count"> #Notes </th>
+            <th id="data-table-thanks"> Popularity </th>
             <th class="no-display" id="data-table-upload"> Upload </th>
             <th class="no-display" id="data-table-course-name"> Course Name </th>
             <th class="no-display" id="data-table-school-name"> Institution </th>
index 0f2ac627d550611886636dbf3d3b4219030df303..18fd2fca2588fc105c88eecb898f7ffd051d6b34 100644 (file)
@@ -1,7 +1,8 @@
 <tr>
   <td class="no-display"><!-- this spot solely for the Sort By header --></td>
-  <td class="small-8 columns large-4 slim">{{ course.updated_at|date:"b d, o"|capfirst }}</td>
-  <td class="small-4 columns large-2 large-offset-2 slim">{{ course.file_count }}</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-12 large-5 columns">
     <a href="{{ course.get_absolute_url }}">{% if course.department %}{{course.department.name}}: {% endif %}{{ course.name }}</a>