Clean up PPT converted PDF to save properly
authorJosh Williams <jwilliams@endpoint.com>
Thu, 26 Sep 2013 22:17:27 +0000 (18:17 -0400)
committerJosh Williams <jwilliams@endpoint.com>
Thu, 26 Sep 2013 22:17:27 +0000 (18:17 -0400)
karmaworld/apps/notes/gdrive.py
karmaworld/apps/notes/models.py

index 3b35c397c770a46ac1c780df7bb3d5a0110212c7..fd5e9b1ebefedf5f8bf62ccee2c55d966e2f224c 100644 (file)
@@ -3,7 +3,6 @@
 # Copyright (C) 2012  FinalsClub Foundation
 
 import datetime
-from io import FileIO, BufferedWriter
 import mimetypes
 import os
 import time
@@ -13,7 +12,7 @@ from apiclient.discovery import build
 from apiclient.http import MediaFileUpload
 from apiclient.http import MediaInMemoryUpload
 from django.conf import settings
-from django.core.files import File
+from django.core.files.base import ContentFile
 from oauth2client.client import flow_from_clientsecrets
 
 from karmaworld.apps.notes.models import DriveAuth, Note
@@ -188,22 +187,7 @@ def convert_with_google_drive(note):
 
     elif extension.lower() in ['.ppt', '.pptx']:
         new_note.file_type = 'ppt'
-        now = datetime.datetime.utcnow()
-        # create a folder path to store the ppt > pdf file with year and month folders
-        nonce_path = '/ppt_pdf/%s/%s/' % (now.year, now.month)
-
-        _path = filename + '.pdf'
-        try:
-            # If those folders don't exist, create them
-            os.makedirs(os.path.realpath(os.path.dirname(_path)))
-        except:
-            print "we failed to create those directories"
-
-        _writer = BufferedWriter(FileIO(_path, "w"))
-        _writer.write(content_dict['pdf'])
-        _writer.close()
-
-        new_note.pdf_file = _path
+        new_note.pdf_file.save(filename + '.pdf', ContentFile(content_dict['pdf']))
 
     else:
         # PPT files do not have this export ability
@@ -261,22 +245,7 @@ def convert_raw_document(raw_document):
 
     elif raw_document.mimetype in PPT_MIMETYPES:
         note.file_type = 'ppt'
-        now = datetime.datetime.utcnow()
-        # create a folder path to store the ppt > pdf file with year and month folders
-        nonce_path = '/ppt_pdf/%s/%s/' % (now.year, now.month)
-
-        _path = filename + '.pdf'
-        try:
-            # If those folders don't exist, create them
-            os.makedirs(os.path.realpath(os.path.dirname(_path)))
-        except:
-            print "we failed to create those directories"
-
-        _writer = BufferedWriter(FileIO(_path, "w"))
-        _writer.write(content_dict['pdf'])
-        _writer.close()
-
-        note.pdf_file = _path
+        note.pdf_file.save(filename + '.pdf', ContentFile(content_dict['pdf']))
 
     else:
         # PPT files do not have this export ability
index 63e3a9cbfc72823bc496db8f3d0bfd1bb716d065..75296e926031768e942160fc6e0b29258f69bc71 100644 (file)
@@ -136,15 +136,14 @@ class Note(Document):
                             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