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)
# 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')