From: Seth Woodworth Date: Fri, 25 Jan 2013 00:54:57 +0000 (-0500) Subject: adding file_count to School model and functions to update it X-Git-Tag: release-20150131~546^2~6 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=3a3395e34113171f14242713b9e96189b101e883;p=oweals%2Fkarmaworld.git adding file_count to School model and functions to update it --- diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index a83af3b..8ac6b1b 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -21,18 +21,33 @@ class School(models.Model): url = models.URLField(max_length=511, blank=True) # Facebook keeps a unique identifier for all schools facebook_id = models.BigIntegerField(blank=True, null=True) + file_count = models.IntegerField(default=0) def __unicode__(self): return self.name - def save(self, *args, **kwargs): """ Save school and generate a slug if one doesn't exist """ if not self.slug: self.slug = defaultfilters.slugify(self.name) super(School, self).save(*args, **kwargs) + def update_note_count(self): + """ 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 """ @@ -58,23 +73,19 @@ class Course(models.Model): class Meta: ordering = ['-file_count', 'school', 'name'] - def __unicode__(self): return u"{0}: {1}".format(self.name, self.school) - def get_absolute_url(self): """ Not implemented yet """ return u"/{0}/{1}".format(self.school.slug, self.slug) - def save(self, *args, **kwargs): """ Save school and generate a slug if one doesn't exist """ if not self.slug: self.slug = defaultfilters.slugify(self.name) super(Course, self).save(*args, **kwargs) - def update_note_count(self): self.file_count = self.note_set.count() self.save()