From 0dd9acd1ff72503889ab1d789bbda7964a3666d8 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 6 Jan 2014 17:42:50 -0500 Subject: [PATCH] for #68, fixing upstream link 'display and uploading to FP by URL works. --- karmaworld/apps/document_upload/models.py | 23 ++++--- .../management/commands/import_ocw_json.py | 61 ++++++++++--------- karmaworld/templates/notes/note_detail.html | 2 +- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/karmaworld/apps/document_upload/models.py b/karmaworld/apps/document_upload/models.py index a46984d..8a1d9b7 100644 --- a/karmaworld/apps/document_upload/models.py +++ b/karmaworld/apps/document_upload/models.py @@ -25,15 +25,20 @@ class RawDocument(Document): def convert_to_note(self): """ polymorph this object into a note.models.Note object """ - note = Note( - course=self.course, - name=self.name, - slug=self.slug, - ip=self.ip, - uploaded_at=self.uploaded_at, - fp_file=self.fp_file, - user=self.user, - mimetype=self.mimetype) + # TODO move this to Note. superclasses should not care about subclasses, + # but subclasses should care about parents. + + # Note inherits all fields of Document as does RawDocument. + # Dynamically refer to all fields of RawDocument found within Document + # and also Note. + initdict = {} + for field in Document._meta.get_all_field_names(): + if field in ('tags',): + # TaggableManager does not play well with init() + continue + initdict[field] = getattr(self,field) + # Create a new Note using all fields from the Document + note = Note(**initdict) note.save() for tag in self.tags.all(): note.tags.add(tag) diff --git a/karmaworld/apps/notes/management/commands/import_ocw_json.py b/karmaworld/apps/notes/management/commands/import_ocw_json.py index 3cf2e46..3113c72 100644 --- a/karmaworld/apps/notes/management/commands/import_ocw_json.py +++ b/karmaworld/apps/notes/management/commands/import_ocw_json.py @@ -81,39 +81,40 @@ class Command(BaseCommand): # process notes for each course for note in course['noteLinks']: - # Check to see if the Note is already there. - if len(RawDocument.objects.filter(upstream_link=note['link'])): + # Check to see if the Note is already uploaded. + if len(Note.objects.filter(upstream_link=note['link'])): print "Already there, moving on: {0}".format(note['link']) continue - # Download the note into memory. - print "Downloading {0}".format(note['link']) - dlresp = requests.get(note['link']) - # Check there weren't any problems - dlresp.raise_for_status() - - # Upload raw contents of note to Filepicker - # https://developers.inkfilepicker.com/docs/web/#inkblob-store - print "Uploading to FP." - ulresp = requests.post(fpurl, files={ - #'fileUpload': (note['fileName'], dlresp.raw) - 'fileUpload': dlresp.raw, - }) - ulresp.raise_for_status() - # Filepicker returns JSON, so use that - uljson = ulresp.json() - - print "Saving raw document to database." - # Extract the note info - dbnote = RawDocument() - dbnote.course = dbcourse - dbnote.name = note['fileName'] - dbnote.license = dblicense - dbnote.upstream_link = note['link'] - dbnote.fp_file = uljson['url'] - dbnote.mimetype = uljson['type'] - # Create the RawDocument object. - dbnote.save() + # Upload URL of note to Filepicker if it is not already + # in RawDocument. + rd_test = RawDocument.objects.filter(upstream_link=note['link']) + if not len(rd_test): + # https://developers.inkfilepicker.com/docs/web/#inkblob-store + print "Uploading link {0} to FP.".format(note['link']) + ulresp = requests.post(fpurl, data={ + 'url': note['link'], + }) + ulresp.raise_for_status() + # Filepicker returns JSON, so use that + uljson = ulresp.json() + + print "Saving raw document to database." + # Extract the note info + dbnote = RawDocument() + dbnote.course = dbcourse + dbnote.name = note['fileName'] + dbnote.license = dblicense + dbnote.upstream_link = note['link'] + dbnote.fp_file = uljson['url'] + dbnote.mimetype = uljson['type'] + dbnote.is_processed = True # hack to bypass celery + # Create the RawDocument object. + dbnote.save() + else: + # Find the right RawDocument + print "Already uploaded link {0} to FP.".format(note['link']) + dbnote = rd_test[0] # Do tags separately dbnote.tags.add('mit-ocw','karma') diff --git a/karmaworld/templates/notes/note_detail.html b/karmaworld/templates/notes/note_detail.html index 447d1c7..fc48a2d 100644 --- a/karmaworld/templates/notes/note_detail.html +++ b/karmaworld/templates/notes/note_detail.html @@ -69,7 +69,7 @@ {% if note.license %}
- {{ note.license.html|safe }} {% if note.upstream_link %}{{ note.upstream_link }}{% endif %} + {{ note.license.html|safe }} {% if note.upstream_link %}{{ note.upstream_link|slice:":80" }}{% endif %}
{% endif %} -- 2.25.1