From: Charles Connell Date: Wed, 22 Jan 2014 00:01:19 +0000 (-0500) Subject: Fix to note/user association X-Git-Tag: release-20150131~221 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=fffb367ece2d9dcd334da5958a0ada72c20a7f7f;p=oweals%2Fkarmaworld.git Fix to note/user association --- diff --git a/karmaworld/apps/document_upload/models.py b/karmaworld/apps/document_upload/models.py index 841b33e..ab2bd9a 100644 --- a/karmaworld/apps/document_upload/models.py +++ b/karmaworld/apps/document_upload/models.py @@ -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) diff --git a/karmaworld/apps/document_upload/tasks.py b/karmaworld/apps/document_upload/tasks.py index bb8f661..f4e31d0 100644 --- a/karmaworld/apps/document_upload/tasks.py +++ b/karmaworld/apps/document_upload/tasks.py @@ -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()) diff --git a/karmaworld/apps/document_upload/tests.py b/karmaworld/apps/document_upload/tests.py index cf36a5e..b5d3ca7 100644 --- a/karmaworld/apps/document_upload/tests.py +++ b/karmaworld/apps/document_upload/tests.py @@ -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() diff --git a/karmaworld/apps/document_upload/views.py b/karmaworld/apps/document_upload/views.py index f2a38c9..6032e0f 100644 --- a/karmaworld/apps/document_upload/views.py +++ b/karmaworld/apps/document_upload/views.py @@ -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() diff --git a/karmaworld/apps/notes/gdrive.py b/karmaworld/apps/notes/gdrive.py index 6ad9f38..128a588 100644 --- a/karmaworld/apps/notes/gdrive.py +++ b/karmaworld/apps/notes/gdrive.py @@ -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() diff --git a/karmaworld/settings/common.py b/karmaworld/settings/common.py index 09ab1c4..f8853e9 100644 --- a/karmaworld/settings/common.py +++ b/karmaworld/settings/common.py @@ -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