for #68, fixing upstream link 'display and uploading to FP by URL works.
authorBryan <btbonval@gmail.com>
Mon, 6 Jan 2014 22:42:50 +0000 (17:42 -0500)
committerBryan <btbonval@gmail.com>
Mon, 6 Jan 2014 22:42:50 +0000 (17:42 -0500)
karmaworld/apps/document_upload/models.py
karmaworld/apps/notes/management/commands/import_ocw_json.py
karmaworld/templates/notes/note_detail.html

index a46984d7668a95c743c45eae0ad113cf409bcf3f..8a1d9b7b723a8dcef9fd396fd69636a1e06bf216 100644 (file)
@@ -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)
index 3cf2e46817461216163a05563e3d57cc74732fdc..3113c727585e0071af00f8ceda4bc3320c57d139 100644 (file)
@@ -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')
index 447d1c72d579785e68e2081ebdaa13610453933f..fc48a2d3c3afd60a4d15ce36fad1400db7a30b4f 100644 (file)
@@ -69,7 +69,7 @@
       {% if note.license %}
       <div class="row">
         <div id="note_pedigree" class="twelve columns activity_details_status">
-          {{ note.license.html|safe }} {% if note.upstream_link %}<a href="{{note.upstream_link}}" target="_blank">{{ note.upstream_link }}</a>{% endif %}
+          {{ note.license.html|safe }} {% if note.upstream_link %}<a href="{{note.upstream_link}}" target="_blank">{{ note.upstream_link|slice:":80" }}</a>{% endif %}
         </div><!-- /note_pedigree -->
       </div>
       {% endif %}