organizing course model and adding update_note_count func
authorSeth Woodworth <seth@sethish.com>
Mon, 7 Jan 2013 20:44:39 +0000 (15:44 -0500)
committerSeth Woodworth <seth@sethish.com>
Mon, 7 Jan 2013 20:44:39 +0000 (15:44 -0500)
karmaworld/apps/courses/models.py

index 8123f9b7473a488e5f10e6b806190346d879033a..a266a8ef8efdf99bc42622f3d6e5a3baab6a4a2c 100644 (file)
@@ -41,18 +41,24 @@ class Course(models.Model):
     """ First class object that contains many notes.Note objects """
     # Core metadata
     name        = models.CharField(max_length=255)
-    desc        = models.TextField(max_length=511, blank=True, null=True)
     slug        = models.SlugField(null=True)
-    url         = models.URLField(max_length=511, blank=True, null=True)
-
     school      = models.ForeignKey(School) # Should this be optional ever?
+    file_count  = models.IntegerField(default=0)
 
+    desc        = models.TextField(max_length=511, blank=True, null=True)
+    url         = models.URLField(max_length=511, blank=True, null=True)
     academic_year   = models.IntegerField(blank=True, null=True, 
                         default=datetime.datetime.now().year)
-    instructor_name = models.CharField(max_length=255, blank=True, null=True)
-    instructor_email = models.EmailField(blank=True, null=True)
-    updated_at    = models.DateTimeField(default=datetime.datetime.utcnow)
-    created_at    = models.DateTimeField(auto_now_add=True)
+
+    instructor_name     = models.CharField(max_length=255, blank=True, null=True)
+    instructor_email    = models.EmailField(blank=True, null=True)
+
+    updated_at      = models.DateTimeField(default=datetime.datetime.utcnow)
+
+    created_at      = models.DateTimeField(auto_now_add=True)
+
+    class Meta:
+        ordering = ['-file_count', 'school', 'name']
 
     def __unicode__(self):
         return u"{0}: {1}".format(self.name, self.school)
@@ -67,3 +73,7 @@ class Course(models.Model):
         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()