Contains only the minimum for handling files and their representation
"""
import datetime
+import os
+import urllib
from django.conf import settings
+from django.core.files import File
from django.core.files.storage import FileSystemStorage
from django.db import models
from django.template import defaultfilters
fs = FileSystemStorage(location=settings.MEDIA_ROOT)
+def _choose_upload_to(instance, filename):
+ # /school/course/year/month/day
+ return u"{school}/{course}/{year}/{month}/{day}".format(
+ school=instance.course.school.slug,
+ course=instance.course.slug,
+ year=instance.uploaded_at.year,
+ month=instance.uploaded_at.month,
+ day=instance.uploaded_at.day)
+
class Document(models.Model):
""" An Abstract Base Class representing a document
intended to be subclassed
is_hidden = models.BooleanField(default=False)
fp_file = django_filepicker.models.FPFileField(
- upload_to='queue/%Y/%m/%j/',
+ upload_to=_choose_upload_to,
+ storage=fs, \
null=True, blank=True,
help_text=u"An uploaded file reference from Filepicker.io")
+ mimetype = models.CharField(max_length=255, blank=True, null=True)
class Meta:
abstract = True
ordering = ['-uploaded_at']
-
def __unicode__(self):
return u"Document: {1} -- {2}".format(self.name, self.uploaded_at)
""" generate a unique slug based on name and uploaded_at """
_slug = defaultfilters.slugify(self.name)
klass = self.__class__
- collision = klass.objects.filter(slug=self.slug)
+ collision = klass.objects.filter(slug=_slug)
if collision:
_slug = u"{0}-{1}-{2}-{3}".format(
_slug, self.uploaded_at.month,
self.uploaded_at.day, self.uploaded_at.microsecond)
self.slug = _slug
+ def get_file(self):
+ """ Downloads the file from filepicker.io and returns a
+ Django File wrapper object """
+ # clean up any old downloads that are still hanging around
+ if hasattr(self, 'tempfile'):
+ self.tempfile.close()
+ delattr(self, 'tempfile')
+
+ if hasattr(self, 'filename'):
+ # the file might have been moved in the meantime so
+ # check first
+ if os.path.exists(self.filename):
+ os.remove(self.filename)
+ delattr(self, 'filename')
+
+ # The temporary file will be created in a directory set by the
+ # environment (TEMP_DIR, TEMP or TMP)
+ self.filename, header = urllib.urlretrieve(self.fp_file.name)
+ name = os.path.basename(self.filename)
+ disposition = header.get('Content-Disposition')
+ if disposition:
+ name = disposition.rpartition("filename=")[2].strip('" ')
+ filename = header.get('X-File-Name')
+ if filename:
+ name = filename
+
+ self.tempfile = open(self.filename, 'r')
+ return File(self.tempfile, name=name)
+
def save(self, *args, **kwargs):
if self.name and not self.slug:
self._generate_unique_slug()
('img', 'Scan or picture of notes'),
('pdf', 'PDF file'),
('ppt', 'Powerpoint'),
+ ('txt', 'Text'),
(UNKNOWN_FILE, 'Unknown file'),
)
blank=True, null=True)
# Upload files to MEDIA_ROOT/notes/YEAR/MONTH/DAY, 2012/10/30/filename
- # FIXME: because we are adding files by hand in tasks.py, upload_to is being ignored for media files
pdf_file = models.FileField( \
storage=fs, \
- upload_to="notes/%Y/%m/%j/",\
+ upload_to="notes/%Y/%m/%d/",\
blank=True, null=True)
# No longer keeping a local copy backed by django
note_file = models.FileField( \
storage=fs, \
- upload_to="notes/%Y/%m/%j/",\
+ upload_to="notes/%Y/%m/%d/",\
blank=True, null=True)
# Google Drive URLs