From c3bc08a38663ebb246b87f95fad871180f6dc298 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Thu, 19 Dec 2013 22:57:51 -0500 Subject: [PATCH] Update note counts when a note is create or deleted --- .../management/commands/fix_note_counts.py | 18 ++++++++++++++++++ karmaworld/apps/courses/models.py | 11 ++--------- karmaworld/apps/notes/models.py | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 karmaworld/apps/courses/management/commands/fix_note_counts.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 index 0000000..1b896ab --- /dev/null +++ b/karmaworld/apps/courses/management/commands/fix_note_counts.py @@ -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) + + diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 7c82802..a3d6bae 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -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) diff --git a/karmaworld/apps/notes/models.py b/karmaworld/apps/notes/models.py index db5b356..4fc3f4e 100644 --- a/karmaworld/apps/notes/models.py +++ b/karmaworld/apps/notes/models.py @@ -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 """ -- 2.25.1