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):
'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
<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>
<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>