--- /dev/null
+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)
+
+
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 """
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)
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
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 """