add polymorphism to RawDocument and super() save to register a celery task
authorSeth Woodworth <seth@sethish.com>
Wed, 28 Aug 2013 20:16:49 +0000 (16:16 -0400)
committerSeth Woodworth <seth@sethish.com>
Wed, 28 Aug 2013 20:16:49 +0000 (16:16 -0400)
karmaworld/apps/document_upload/models.py

index cb22d67671aa98958d761adae2aba8f0198eb3bb..766698ec67eedc4370fe9e20eda530d291eba6cb 100644 (file)
@@ -8,11 +8,13 @@ from django.db import models
 import django_filepicker
 
 from karmaworld.apps.notes.models import Document
+from karmaworld.apps.notes.models import Note
+from karmaworld.apps.document_upload import tasks
+
 
 class RawDocument(Document):
     is_processed = models.BooleanField(default=False)
 
-
     class Meta:
         """ Sort files most recent first """
         ordering = ['-uploaded_at']
@@ -23,4 +25,19 @@ class RawDocument(Document):
 
     def convert_to_note(self):
         """ polymorph this object into a note.models.Note object  """
-        pass
+        note = Note(
+                course=self.course,
+                name=self.name,
+                slug=self.slug,
+                ip=self.ip,
+                uploaded_at=self.uploaded_at,
+                fp_file=self.fp_file)
+        note.save()
+        for tag in self.tags.all():
+            note.tags.add(tag)
+        return note
+
+    def save(self, *args, **kwargs):
+        if not is_processed:
+            tasks.process_raw_document(self)
+        super(RawDocument, self).save(*args, **kwargs)