resolving merge conflict by adding both requirements.
[oweals/karmaworld.git] / karmaworld / apps / courses / models.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4
5 """
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.
10 """
11 import datetime
12
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
16
17
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)
31
32     class Meta:
33         """ Sort School by file_count descending, name abc=> """
34         ordering = ['-file_count','-priority', 'name']
35
36
37     def __unicode__(self):
38         return self.name
39
40     def save(self, *args, **kwargs):
41         """ Save school and generate a slug if one doesn't exist """
42         if not self.slug:
43             self.slug = defaultfilters.slugify(self.name)
44         super(School, self).save(*args, **kwargs)
45
46     @staticmethod
47     def autocomplete_search_fields():
48         return ("name__icontains",)
49
50     def update_note_count(self):
51         """ Update the School.file_count by summing the
52             contained course.file_count
53         """
54         self.file_count = sum([course.file_count for course in self.course_set.all()])
55         self.save()
56
57
58 class Course(models.Model):
59     """ First class object that contains many notes.Note objects """
60     # Core metadata
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)
65
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)
70
71     instructor_name     = models.CharField(max_length=255, blank=True, null=True)
72     instructor_email    = models.EmailField(blank=True, null=True)
73
74     updated_at      = models.DateTimeField(default=datetime.datetime.utcnow)
75
76     created_at      = models.DateTimeField(auto_now_add=True)
77
78
79     class Meta:
80         ordering = ['-file_count', 'school', 'name']
81         unique_together = ('school', 'name', 'instructor_name')
82         verbose_name = 'course'
83         verbose_name_plural = 'courses'
84
85     def __unicode__(self):
86         return u"{0}: {1}".format(self.name, self.school)
87
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)
91
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
95         if not self.slug:
96             self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
97             self.save() # Save the slug
98
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")
102
103     @staticmethod
104     def autocomplete_search_fields():
105         return ("name__icontains",)
106
107     def update_note_count(self):
108         """ Update self.file_count by summing the note_set """
109         self.file_count = self.note_set.count()
110         self.save()
111
112
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)
116