Update note counts when a note is create or deleted
authorCharles Connell <charles@connells.org>
Fri, 20 Dec 2013 03:57:51 +0000 (22:57 -0500)
committerCharles Connell <charles@connells.org>
Fri, 20 Dec 2013 03:57:51 +0000 (22:57 -0500)
karmaworld/apps/courses/management/commands/fix_note_counts.py [new file with mode: 0644]
karmaworld/apps/courses/models.py
karmaworld/apps/notes/models.py

diff --git a/karmaworld/apps/courses/management/commands/fix_note_counts.py b/karmaworld/apps/courses/management/commands/fix_note_counts.py
new file mode 100644 (file)
index 0000000..1b896ab
--- /dev/null
@@ -0,0 +1,18 @@
+from django.core.management.base import BaseCommand
+from karmaworld.apps.notes.models import *
+from karmaworld.apps.courses.models import *
+
+class Command(BaseCommand):
+    help = """Set the field file_count on every Course and School
+            to the correct value."""
+
+    def handle(self, *args, **kwargs):
+        for c in Course.objects.all():
+            c.update_note_count()
+            print "Updated course {c}".format(c=c)
+
+        for s in School.objects.all():
+            s.update_note_count()
+            print "Updated school {s}".format(s=s)
+
+
index 7c82802696f17a4302721d8ae3993e17f2333a92..a3d6bae1d58a640474c087f056f95a98ec77252b 100644 (file)
@@ -48,20 +48,12 @@ class School(models.Model):
         return ("name__icontains",)
 
     def update_note_count(self):
-        """ Update the School.file_count by summing the 
+        """ Update the School.file_count by summing the
             contained course.file_count
         """
         self.file_count = sum([course.file_count for course in self.course_set.all()])
         self.save()
 
-    def update_related_note_count(self):
-        """ Runs the update_note_count function on all related course
-            objects, then generates the self.file_count
-        """
-        for course in self.course_set.all():
-            course.update_note_count()
-        self.update_note_count()
-
 
 class Course(models.Model):
     """ First class object that contains many notes.Note objects """
@@ -117,6 +109,7 @@ class Course(models.Model):
         self.file_count = self.note_set.count()
         self.save()
 
+
 # Enforce unique constraints even when we're using a database like
 # SQLite that doesn't understand them
 auto_add_check_unique_together(Course)
index db5b3560c212598d53d29c2a60f9f53554b72a5d..4fc3f4e86964946a8fa325b834ab862ac01348aa 100644 (file)
@@ -7,6 +7,8 @@
     Contains only the minimum for handling files and their representation
 """
 import datetime
+from django.db.models.signals import post_save, post_delete
+from django.dispatch import receiver
 import os
 import urllib
 
@@ -218,6 +220,20 @@ class Note(Document):
         super(Note, self).save(*args, **kwargs)
 
 
+def update_note_counts(note_instance):
+    note_instance.course.update_note_count()
+    note_instance.course.school.update_note_count()
+
+@receiver(post_save, sender=Note, weak=False)
+def note_receiver(sender, **kwargs):
+    if kwargs['created']:
+        update_note_counts(kwargs['instance'])
+
+@receiver(post_delete, sender=Note, weak=False)
+def note_receiver(sender, **kwargs):
+    update_note_counts(kwargs['instance'])
+
+
 class DriveAuth(models.Model):
     """ stored google drive authentication and refresh token
         used for interacting with google drive """