From 1a573de68c90699dabbd9c9f8a84d29ec38192bd Mon Sep 17 00:00:00 2001 From: Bryan Date: Fri, 7 Feb 2014 02:10:55 -0500 Subject: [PATCH] Make use of FilepickerFile to retrieve Download URL, closes #316 --- karmaworld/apps/notes/models.py | 31 ++++++++++++++++++--- karmaworld/apps/notes/views.py | 2 +- karmaworld/templates/notes/note_detail.html | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/karmaworld/apps/notes/models.py b/karmaworld/apps/notes/models.py index cb6c039..83f00c3 100644 --- a/karmaworld/apps/notes/models.py +++ b/karmaworld/apps/notes/models.py @@ -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: diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index 8736288..e970655 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -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)) diff --git a/karmaworld/templates/notes/note_detail.html b/karmaworld/templates/notes/note_detail.html index 4f53334..f2ea776 100644 --- a/karmaworld/templates/notes/note_detail.html +++ b/karmaworld/templates/notes/note_detail.html @@ -92,7 +92,7 @@
- +