Fix to note/user association
authorCharles Connell <charles@connells.org>
Wed, 22 Jan 2014 00:01:19 +0000 (19:01 -0500)
committerCharles Connell <charles@connells.org>
Thu, 23 Jan 2014 00:17:52 +0000 (19:17 -0500)
karmaworld/apps/document_upload/models.py
karmaworld/apps/document_upload/tasks.py
karmaworld/apps/document_upload/tests.py
karmaworld/apps/document_upload/views.py
karmaworld/apps/notes/gdrive.py
karmaworld/settings/common.py

index 841b33eb955fe59568aad959537f86f2da261c45..ab2bd9ae5d4d07cabe702d56e100e48f8d1017dc 100644 (file)
@@ -65,9 +65,9 @@ class RawDocument(Document):
             note.tags.add(tag)
         return note
 
-    def save(self, user=None, session_key=None, *args, **kwargs):
+    def save(self, user=None, session=None, *args, **kwargs):
         super(RawDocument, self).save(*args, **kwargs)
         if not self.is_processed:
-            tasks.process_raw_document.delay(self, user, session_key)
+            tasks.process_raw_document.delay(self, user, session)
 
 auto_add_check_unique_together(RawDocument)
index bb8f6612936ce1457ffb2afba5e9f6286c53dfff..f4e31d042cfca1004399abe5d9665ba461ab6e16 100644 (file)
@@ -10,10 +10,10 @@ from karmaworld.apps.notes.gdrive import convert_raw_document
 logger = get_task_logger(__name__)
 
 @task()
-def process_raw_document(raw_document, user, session_key):
+def process_raw_document(raw_document, user, session):
     """ Process a RawDocument instance in to a Note instance """
     try:
-        convert_raw_document(raw_document, user=user, session_key=session_key)
+        convert_raw_document(raw_document, user=user, session=session)
     except:
         logger.error(traceback.format_exc())
 
index cf36a5eeb50634bbe65c8f70f54c151898e95953..b5d3ca77d7df3b4108a442d621f4309910ba6a30 100644 (file)
@@ -27,13 +27,13 @@ class ConversionTest(TestCase):
         self.course.save()
         self.client = Client()
 
-    def doConversionForPost(self, post, user=None, session_key=None):
+    def doConversionForPost(self, post, user=None, session=None):
         self.assertEqual(Note.objects.count(), 0)
         r_d_f = RawDocumentForm(post)
         self.assertTrue(r_d_f.is_valid())
         raw_document = r_d_f.save(commit=False)
         raw_document.fp_file = post['fp_file']
-        convert_raw_document(raw_document, user=user, session_key=session_key)
+        convert_raw_document(raw_document, user=user, session_key=session)
         self.assertEqual(Note.objects.count(), 1)
 
     def testPlaintextConversion(self):
@@ -97,7 +97,7 @@ class ConversionTest(TestCase):
                                  'name': 'graph3.txt',
                                  'tags': '',
                                  'mimetype': 'text/plain'},
-                                 session_key=s.session_key)
+                                 session=s)
         note = Note.objects.all()[0]
         self.assertEqual(note.user, user)
 
@@ -114,7 +114,7 @@ class ConversionTest(TestCase):
                                  'name': 'graph3.txt',
                                  'tags': '',
                                  'mimetype': 'text/plain'},
-                                 session_key=s.session_key)
+                                 session=s)
         user = User(username=TEST_USERNAME)
         user.save()
 
index f2a38c9173335b68d0a1dd7b4e2156b5e634e752..6032e0fcff33453a5ad7a10b224b18ba4a8c4b67 100644 (file)
@@ -24,7 +24,11 @@ def save_fp_upload(request):
         if request.user.is_authenticated():
             raw_document.save(user=request.user)
         else:
-            raw_document.save(session_key=request.session.session_key)
+            # Generate session key if it doesn't exist
+            if not request.session.get('has_session'):
+                request.session['has_session'] = True
+                request.session.save()
+            raw_document.save(session=request.session)
         # save the tags to the database, too. don't forget those guys.
         r_d_f.save_m2m()
 
index 6ad9f380a2741aa61f85a714538f1add8b4d8418..128a5885933db6604f950b8379b836949a24fc9c 100644 (file)
@@ -14,6 +14,7 @@ import magic
 import re
 import json
 import time
+from django.contrib.auth import SESSION_KEY
 
 import httplib2
 from apiclient.discovery import build
@@ -28,6 +29,7 @@ PPT_MIMETYPES = ['application/vnd.ms-powerpoint', 'application/vnd.openxmlformat
 
 UPLOADED_NOTES_SESSION_KEY = 'uploaded_notes'
 
+
 def extract_file_details(fileobj):
     details = None
     year = None
@@ -176,7 +178,7 @@ def upload_to_gdrive(service, media, filename, extension=None, mimetype=None):
     return file_dict
 
 
-def convert_raw_document(raw_document, user=None, session_key=None):
+def convert_raw_document(raw_document, user=None, session=None):
     """ Upload a raw document to google drive and get a Note back"""
     fp_file = raw_document.get_file()
 
@@ -245,13 +247,12 @@ def convert_raw_document(raw_document, user=None, session_key=None):
     # Finally, save whatever data we got back from google
     note.save()
 
-    if session_key and not user:
-        s = SessionStore(session_key=session_key)
+    if session and not user:
         # If the person who uploaded this made an
         # account or signed in while convert_raw_document
         # was running, associate their account with this note
         try:
-            uid = s['_auth_user_id']
+            uid = session[SESSION_KEY]
             user = User.objects.get(pk=uid)
             note.user = user
             note.save()
@@ -261,9 +262,10 @@ def convert_raw_document(raw_document, user=None, session_key=None):
         # so if the uploader later creates an account,
         # we can find notes they uploaded
         except (KeyError, ObjectDoesNotExist):
-            uploaded_notes = s.get(UPLOADED_NOTES_SESSION_KEY, [])
+            uploaded_notes = session.get(UPLOADED_NOTES_SESSION_KEY, [])
             uploaded_notes.append(note.id)
-            s[UPLOADED_NOTES_SESSION_KEY] = uploaded_notes
-            s.save()
+            session[UPLOADED_NOTES_SESSION_KEY] = uploaded_notes
+            session.modified = True
+            session.save()
 
 
index 09ab1c477f58ab5093086f3de04c76a9874fcb26..f8853e9eb57862fa2803ea3ca12e4d87f841bf41 100644 (file)
@@ -346,6 +346,7 @@ COMPRESS_JS_FILTERS = [
 
 ########## SESSION CONFIGURATION
 
+SESSION_ENGINE = 'django.contrib.sessions.backends.db'
 SESSION_COOKIE_AGE = 63072000    # 2 years in seconds
 
 ########## END SESSION CONFIGURATION