Make use of FilepickerFile to retrieve Download URL, closes #316
authorBryan <btbonval@gmail.com>
Fri, 7 Feb 2014 07:10:55 +0000 (02:10 -0500)
committerBryan <btbonval@gmail.com>
Fri, 7 Feb 2014 07:10:55 +0000 (02:10 -0500)
karmaworld/apps/notes/models.py
karmaworld/apps/notes/views.py
karmaworld/templates/notes/note_detail.html

index cb6c03922332a5e45bffed681466dc9157e246df..83f00c3d46635ece523f345a6acc957ddd18a71c 100644 (file)
@@ -142,11 +142,34 @@ class Document(models.Model):
                     self.uploaded_at.day, self.uploaded_at.microsecond)
         self.slug = _slug
 
+    def _get_fpf(self):
+        """
+        Memoized FilepickerFile getter. Returns FilepickerFile.
+        """
+        if not hasattr(self, 'cached_fpf'):
+            # Fetch additional_params containing signature, etc
+            aps = self.fp_file.field.additional_params
+            self.cached_fpf = django_filepicker.utils.FilepickerFile(self.fp_file.name, aps)
+        return self.cached_fpf
+
+    def get_fp_url(self):
+        """
+        Returns the Filepicker URL for reading the upstream document.
+        """
+        # Fetch FilepickerFile
+        fpf = self._get_fpf()
+        # Return proper URL for reading
+        return fpf.get_url()
+
     def get_file(self):
-        """ Downloads the file from filepicker.io and returns a
-        Django File wrapper object """
-        fpf = django_filepicker.utils.FilepickerFile(self.fp_file.name)
-        return fpf.get_file(self.fp_file.field.additional_params)
+        """
+        Downloads the file from filepicker.io and returns a Django File wrapper
+        object.
+        """
+        # Fetch FilepickerFile
+        fpf = self._get_fpf()
+        # Return Django File
+        return fpf.get_file()
 
     def save(self, *args, **kwargs):
         if self.name and not self.slug:
index 873628895b38a5f67212c84e79c3224f5349b320..e970655ed7140688e1eefc1c627b04876345d0e3 100644 (file)
@@ -165,7 +165,7 @@ class PDFView(DetailView):
             kwargs['pdf_path'] = "{0}{1}".format(settings.MEDIA_URL,
                 os.path.basename(self.object.pdf_file.name))
         elif is_pdf(self):
-            kwargs['pdf_path'] = self.object.fp_file
+            kwargs['pdf_path'] = self.object.get_fp_url()
             #kwargs['pdf_path'] = "{0}{1}".format(settings.MEDIA_URL,
             #    os.path.basename(self.object.note_file.name))
 
index 4f533340dd43fca170f3fdb082739591ab4ed39f..f2ea776f7362de64cdc40c45fc829f9f14f4a0c7 100644 (file)
@@ -92,7 +92,7 @@
                 </a>
               </div>
               <div class="small-4 column">
-                <a id="note-download-button" href="{{ note.fp_file }}">
+                <a id="note-download-button" href="{{ note.get_fp_url }}">
                   <img
                     src="{{ STATIC_URL }}img/note_download.png"
                     alt="note_download"