Re-disable the HTML sanitize for now
[oweals/karmaworld.git] / karmaworld / apps / notes / gdrive.py
index fd5e9b1ebefedf5f8bf62ccee2c55d966e2f224c..79600547c79fbbb4a4c4558cb70508628d504de5 100644 (file)
@@ -3,8 +3,10 @@
 # Copyright (C) 2012  FinalsClub Foundation
 
 import datetime
+import magic
 import mimetypes
 import os
+import re
 import time
 
 import httplib2
@@ -21,7 +23,7 @@ CLIENT_SECRET = os.path.join(settings.DJANGO_ROOT, \
                     'secret/client_secrets.json')
 #from credentials import GOOGLE_USER # FIXME
 try:
-    from secret.drive import GOOGLE_USER
+    from secrets.drive import GOOGLE_USER
 except:
     GOOGLE_USER = 'admin@karmanotes.org' # FIXME
 
@@ -29,6 +31,23 @@ EXT_TO_MIME = {'.docx': 'application/msword'}
 
 PPT_MIMETYPES = ['application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
 
+def extract_file_details(fileobj):
+    details = None
+    year = None
+
+    fileobj.open()
+    filebuf = fileobj.read()
+    with magic.Magic() as m:
+        details = m.id_buffer(filebuf)
+    fileobj.close()
+
+    result = re.search(r'Create Time/Date:[^,]+(?P<year>\d{4})', details)
+    if result:
+        if 'year' in result.groupdict():
+            year = result.groupdict()['year']
+
+    return {'year': year}
+
 def build_flow():
     """ Create an oauth2 autentication object with our preferred details """
     scopes = [
@@ -247,16 +266,18 @@ def convert_raw_document(raw_document):
         note.file_type = 'ppt'
         note.pdf_file.save(filename + '.pdf', ContentFile(content_dict['pdf']))
 
-    else:
-        # PPT files do not have this export ability
-        note.gdrive_url = file_dict[u'exportLinks']['application/vnd.oasis.opendocument.text']
+    elif 'html' in content_dict and content_dict['html']:
         note.html = content_dict['html']
+        # before we save new html, sanitize a tags in note.html
+        #note.sanitize_html(save=False)
+        #FIXME: ^^^ disabled
 
     note.text = content_dict['text']
 
-    # before we save new html, sanitize a tags in note.html
-    #note.sanitize_html(save=False)
-    #FIXME: ^^^ disabled until we can get html out of an Etree html element
+    note_details = extract_file_details(fp_file)
+    if 'year' in note_details and note_details['year']:
+        note.year = note_details['year']
+
 
     # Finally, save whatever data we got back from google
     note.save()