3 # Copyright (C) 2012 FinalsClub Foundation
6 Models for the courses django app.
7 Handles courses, and their related models
8 Courses are the first class object, they contain notes.
9 Courses have a manytoone relation to schools.
13 from django.db import models
14 from django.template import defaultfilters
15 from karmaworld.settings.manual_unique_together import auto_add_check_unique_together
17 from karmaworld.apps.schools.models import School
18 from karmaworld.apps.schools.models import Department
19 from karmaworld.apps.professors.models import Professor
22 class Course(models.Model):
23 """ First class object that contains many notes.Note objects """
25 name = models.CharField(max_length=255)
26 slug = models.SlugField(max_length=150, null=True)
27 # department should remove nullable when school gets yoinked
28 department = models.ForeignKey(Department, blank=True, null=True)
29 # school is an appendix: the kind that gets swollen and should be removed
31 school = models.ForeignKey(School)
32 file_count = models.IntegerField(default=0)
34 desc = models.TextField(max_length=511, blank=True, null=True)
35 url = models.URLField(max_length=511, blank=True, null=True)
37 instructor_name = models.CharField(max_length=255, blank=True, null=True)
38 instructor_email = models.EmailField(blank=True, null=True)
40 updated_at = models.DateTimeField(default=datetime.datetime.utcnow)
42 created_at = models.DateTimeField(auto_now_add=True)
44 # Number of times this course has been flagged as abusive/spam.
45 flags = models.IntegerField(default=0,null=False)
49 ordering = ['-file_count', 'school', 'name']
50 unique_together = ('school', 'name', 'instructor_name')
51 verbose_name = 'course'
52 verbose_name_plural = 'courses'
54 def __unicode__(self):
55 return u"{0}: {1}".format(self.name, self.school)
57 def get_absolute_url(self):
58 """ return url based on school slug and self slug """
59 return u"/{0}/{1}".format(self.school.slug, self.slug)
61 def save(self, *args, **kwargs):
62 """ Save school and generate a slug if one doesn't exist """
63 super(Course, self).save(*args, **kwargs) # generate a self.id
65 self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
66 self.save() # Save the slug
68 def get_updated_at_string(self):
69 """ return the formatted style for datetime strings """
70 return self.updated_at.strftime("%I%p // %a %b %d %Y")
73 def autocomplete_search_fields():
74 return ("name__icontains",)
76 def update_note_count(self):
77 """ Update self.file_count by summing the note_set """
78 self.file_count = self.note_set.count()
82 # Enforce unique constraints even when we're using a database like
83 # SQLite that doesn't understand them
84 auto_add_check_unique_together(Course)