3 # Copyright (C) 2012 FinalsClub Foundation
6 Models for the notes django app.
7 Contains only the minimum for handling files and their representation
11 from django.conf import settings
12 from django.core.files.storage import FileSystemStorage
13 from django.db import models
14 from django.template import defaultfilters
15 from taggit.managers import TaggableManager
16 from oauth2client.client import Credentials
18 from karmaworld.apps.courses.models import Course
21 from secrets.drive import GOOGLE_USER
23 GOOGLE_USER = u'admin@karmanotes.org'
25 fs = FileSystemStorage(location=settings.MEDIA_ROOT)
27 class Note(models.Model):
28 """ A django model representing an uploaded file and associated metadata.
32 ('doc', 'MS Word compatible file (.doc, .docx, .rtf, .odf)'),
33 ('img', 'Scan or picture of notes'),
35 (UNKNOWN_FILE, 'Unknown file'),
38 course = models.ForeignKey(Course)
40 tags = TaggableManager(blank=True)
42 name = models.CharField(max_length=255, blank=True, null=True)
43 slug = models.SlugField(max_length=255, null=True)
44 desc = models.TextField(max_length=511, blank=True, null=True)
45 uploaded_at = models.DateTimeField(null=True, default=datetime.datetime.utcnow)
47 file_type = models.CharField(max_length=15,
48 choices=FILE_TYPE_CHOICES,
50 blank=True, null=True)
52 # Upload files to MEDIA_ROOT/notes/YEAR/MONTH/DAY, 2012/10/30/filename
53 note_file = models.FileField(
55 upload_to="notes/%Y/%m/%j/",
56 blank=True, null=True)
58 ## post gdrive conversion data
59 embed_url = models.URLField(max_length=1024, blank=True, null=True)
60 download_url = models.URLField(max_length=1024, blank=True, null=True)
61 # for word processor documents
62 html = models.TextField(blank=True, null=True)
63 text = models.TextField(blank=True, null=True)
67 """ Sort files by most recent first """
68 ordering = ['-uploaded_at']
71 def __unicode__(self):
72 return u"{0}: {1} -- {2}".format(self.file_type, self.name, self.uploaded_at)
74 def save(self, *args, **kwargs):
75 """ override built-in save to ensure contextual self.name """
76 # TODO: If self.name isn't set, generate one based on uploaded_name
77 # if we fail to set the Note.name earlier than this, use the saved filename
79 if not self.slug and self.name:
80 # only generate a slug if the name has been set, and slug hasn't
81 self.slug = defaultfilters.slugify(self.name)
82 super(Note, self).save(*args, **kwargs)
84 def get_absolute_url(self):
85 """ Resolve note url, use 'note' route and slug if slug
88 if self.slug is not None:
89 # return a url ending in slug
90 return u"/{0}/{1}/{2}".format(self.course.school.slug, self.course.slug, self.slug)
92 # return a url ending in id
93 return u"/{0}/{1}/{2}".format(self.course.school.slug, self.course.slug, self.id)
96 class DriveAuth(models.Model):
97 """ stored google drive authentication and refresh token
98 used for interacting with google drive """
100 email = models.EmailField(default=GOOGLE_USER)
101 credentials = models.TextField() # JSON of Oauth2Credential object
102 stored_at = models.DateTimeField(auto_now=True)
106 def get(email=GOOGLE_USER):
107 """ Staticmethod for getting the singleton DriveAuth object """
108 # FIXME: this is untested
109 return DriveAuth.objects.filter(email=email).reverse()[0]
112 def store(self, creds):
113 """ Transform an existing credentials object to a db serialized """
114 self.email = creds.id_token['email']
115 self.credentials = creds.to_json()
119 def transform_to_cred(self):
120 """ take stored credentials and produce a Credentials object """
121 return Credentials.new_from_json(self.credentials)
124 def __unicode__(self):
125 return u'Gdrive auth for %s created/updated at %s' % \
126 (self.email, self.stored_at)