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
18 class School(models.Model):
19 """ A grouping that contains many courses """
20 name = models.CharField(max_length=255)
21 slug = models.SlugField(max_length=150, null=True)
22 location = models.CharField(max_length=255, blank=True, null=True)
23 url = models.URLField(max_length=511, blank=True)
24 # Facebook keeps a unique identifier for all schools
25 facebook_id = models.BigIntegerField(blank=True, null=True)
26 # United States Department of Education institution_id
27 usde_id = models.BigIntegerField(blank=True, null=True)
28 file_count = models.IntegerField(default=0)
29 priority = models.BooleanField(default=0)
30 alias = models.CharField(max_length=255, null=True, blank=True)
33 """ Sort School by file_count descending, name abc=> """
34 ordering = ['-file_count','-priority', 'name']
37 def __unicode__(self):
40 def save(self, *args, **kwargs):
41 """ Save school and generate a slug if one doesn't exist """
43 self.slug = defaultfilters.slugify(self.name)
44 super(School, self).save(*args, **kwargs)
47 def autocomplete_search_fields():
48 return ("name__icontains",)
50 def update_note_count(self):
51 """ Update the School.file_count by summing the
52 contained course.file_count
54 self.file_count = sum([course.file_count for course in self.course_set.all()])
58 class Course(models.Model):
59 """ First class object that contains many notes.Note objects """
61 name = models.CharField(max_length=255)
62 slug = models.SlugField(max_length=150, null=True)
63 school = models.ForeignKey(School) # Should this be optional ever?
64 file_count = models.IntegerField(default=0)
66 desc = models.TextField(max_length=511, blank=True, null=True)
67 url = models.URLField(max_length=511, blank=True, null=True)
68 academic_year = models.IntegerField(blank=True, null=True,
69 default=datetime.datetime.now().year)
71 instructor_name = models.CharField(max_length=255, blank=True, null=True)
72 instructor_email = models.EmailField(blank=True, null=True)
74 updated_at = models.DateTimeField(default=datetime.datetime.utcnow)
76 created_at = models.DateTimeField(auto_now_add=True)
80 ordering = ['-file_count', 'school', 'name']
81 unique_together = ('school', 'name', 'instructor_name')
82 verbose_name = 'course'
83 verbose_name_plural = 'courses'
85 def __unicode__(self):
86 return u"{0}: {1}".format(self.name, self.school)
88 def get_absolute_url(self):
89 """ return url based on school slug and self slug """
90 return u"/{0}/{1}".format(self.school.slug, self.slug)
92 def save(self, *args, **kwargs):
93 """ Save school and generate a slug if one doesn't exist """
94 super(Course, self).save(*args, **kwargs) # generate a self.id
96 self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
97 self.save() # Save the slug
99 def get_updated_at_string(self):
100 """ return the formatted style for datetime strings """
101 return self.updated_at.strftime("%I%p // %a %b %d %Y")
104 def autocomplete_search_fields():
105 return ("name__icontains",)
107 def update_note_count(self):
108 """ Update self.file_count by summing the note_set """
109 self.file_count = self.note_set.count()
113 # Enforce unique constraints even when we're using a database like
114 # SQLite that doesn't understand them
115 auto_add_check_unique_together(Course)