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.db import models
12 from taggit.managers import TaggableManager
13 from oauth2client.client import Credentials
15 from karmaworld.apps.courses.models import Course
17 class Note(models.Model):
18 """ A django model representing an uploaded file and associated metadata.
22 ('doc', 'MS Word compatible file (.doc, .docx, .rtf, .odf)'),
23 ('img', 'Scan or picture of notes'),
25 (UNKNOWN_FILE, 'Unknown file'),
28 course = models.ForeignKey(Course)
30 tags = TaggableManager(blank=True)
32 name = models.CharField(max_length=255, blank=True, null=True)
33 slug = models.SlugField(null=True)
34 desc = models.TextField(max_length=511, blank=True, null=True)
35 uploaded_at = models.DateTimeField(null=True, default=datetime.datetime.utcnow)
37 file_type = models.CharField(max_length=15, blank=True, null=True, choices=FILE_TYPE_CHOICES, default=UNKNOWN_FILE)
38 # Upload files to MEDIA_ROOT/notes/YEAR/MONTH/DAY, 2012/10/30/filename
39 note_file = models.FileField(upload_to="notes/%Y/%m/%j/", blank=True, null=True)
41 ## post gdrive conversion data
42 embed_url = models.URLField(max_length=1024, blank=True, null=True)
43 download_url = models.URLField(max_length=1024, blank=True, null=True)
44 # for word processor documents
45 html = models.TextField(blank=True, null=True)
46 text = models.TextField(blank=True, null=True)
48 # FIXME: Not Implemented
49 #uploader = models.ForeignKey(User, blank=True, null=True, related_name='notes')
50 #course = models.ForeignKey(Course, blank=True, null=True, related_name="files")
51 #school = models.ForeignKey(School, blank=True, null=True)
54 def __unicode__(self):
55 return u"{0}: {1} -- {2}".format(self.file_type, self.name, self.uploaded_at)
58 def save(self, *args, **kwargs):
59 """ override built-in save to ensure contextual self.name """
60 # TODO: If self.name isn't set, generate one based on uploaded_name
61 # if we fail to set the Note.name earlier than this, use the saved filename
64 self.slug = defaultfilters.slugify(self.name)
65 super(Note, self).save(*args, **kwargs)
68 def get_absolute_url(self):
69 """ Resolve note url, use 'note' route and slug if slug
70 otherwise use note and id
72 return u"/{0}/{1}/{2}".format(self.course.school.slug, self.course.slug, self.slug)
75 # FIXME: replace the following GOOGLE_USER in a settings.py
76 GOOGLE_USER = 'seth.woodworth@gmail.com'
78 class DriveAuth(models.Model):
79 """ stored google drive authentication and refresh token
80 used for interacting with google drive """
82 email = models.EmailField(default=GOOGLE_USER)
83 credentials = models.TextField() # JSON of Oauth2Credential object
84 stored_at = models.DateTimeField(auto_now=True)
88 def get(email=GOOGLE_USER):
89 """ Staticmethod for getting the singleton DriveAuth object """
90 # FIXME: this is untested
91 return DriveAuth.objects.filter(email=email).reverse()[0]
94 def store(self, creds):
95 """ Transform an existing credentials object to a db serialized """
96 self.email = creds.id_token['email']
97 self.credentials = creds.to_json()
101 def transform_to_cred(self):
102 """ take stored credentials and produce a Credentials object """
103 return Credentials.new_from_json(self.credentials)
106 def __unicode__(self):
107 return u'Gdrive auth for %s created/updated at %s' % \
108 (self.email, self.stored_at)