Removing School and Department from Course model to prevent circular dependencies...
[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 from karmaworld.apps.schools.models import School
18 from karmaworld.apps.schools.models import Department
19 from karmaworld.apps.professors.models import Professor
20
21
22 class Course(models.Model):
23     """ First class object that contains many notes.Note objects """
24     # Core metadata
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
30     # (vistigial)
31     school      = models.ForeignKey(School) 
32     file_count  = models.IntegerField(default=0)
33
34     desc        = models.TextField(max_length=511, blank=True, null=True)
35     url         = models.URLField(max_length=511, blank=True, null=True)
36
37     instructor_name     = models.CharField(max_length=255, blank=True, null=True)
38     instructor_email    = models.EmailField(blank=True, null=True)
39
40     updated_at      = models.DateTimeField(default=datetime.datetime.utcnow)
41
42     created_at      = models.DateTimeField(auto_now_add=True)
43
44     # Number of times this course has been flagged as abusive/spam.
45     flags           = models.IntegerField(default=0,null=False)
46
47
48     class Meta:
49         ordering = ['-file_count', 'school', 'name']
50         unique_together = ('school', 'name', 'instructor_name')
51         verbose_name = 'course'
52         verbose_name_plural = 'courses'
53
54     def __unicode__(self):
55         return u"{0}: {1}".format(self.name, self.school)
56
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)
60
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
64         if not self.slug:
65             self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
66             self.save() # Save the slug
67
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")
71
72     @staticmethod
73     def autocomplete_search_fields():
74         return ("name__icontains",)
75
76     def update_note_count(self):
77         """ Update self.file_count by summing the note_set """
78         self.file_count = self.note_set.count()
79         self.save()
80
81
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)
85