From 43022c3205b33e57c7750a477a7e40dea01c5b18 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 21 Mar 2017 13:16:48 -0500 Subject: [PATCH] Refactor the batch file field renderer somewhat try to leverage the "common" file field renderer some more... --- tailbone/forms/renderers/batch.py | 23 ++++------------------- tailbone/forms/renderers/files.py | 4 ++++ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/tailbone/forms/renderers/batch.py b/tailbone/forms/renderers/batch.py index cb8662f4..be63d9e6 100644 --- a/tailbone/forms/renderers/batch.py +++ b/tailbone/forms/renderers/batch.py @@ -31,10 +31,10 @@ import stat import random import formalchemy as fa -from formalchemy.ext import fsblob -from formalchemy.fields import FileFieldRenderer as Base from webhelpers.html import tags +from tailbone.forms.renderers import FileFieldRenderer as BaseFileFieldRenderer + class BatchIDFieldRenderer(fa.FieldRenderer): """ @@ -53,23 +53,13 @@ class BatchIDFieldRenderer(fa.FieldRenderer): return '' -# TODO: make this inherit from `tailbone.forms.renderers.files.FileFieldRenderer` -class FileFieldRenderer(fsblob.FileFieldRenderer): +class FileFieldRenderer(BaseFileFieldRenderer): """ Custom file field renderer for batches based on a single source data file. In edit mode, shows a file upload field. In readonly mode, shows the filename and its size. """ - @classmethod - def new(cls, view): - name = 'Configured%s_%s' % (cls.__name__, str(random.random())[2:]) - return type(str(name), (cls,), dict(view=view)) - - @property - def storage_path(self): - return self.view.upload_dir - def get_size(self): size = super(FileFieldRenderer, self).get_size() if size: @@ -80,13 +70,8 @@ class FileFieldRenderer(fsblob.FileFieldRenderer): return os.stat(path)[stat.ST_SIZE] return 0 - def get_url(self, filename): - batch = self.field.parent.model - return self.view.request.route_url('{}.download'.format(self.view.get_route_prefix()), - uuid=batch.uuid) - def render(self, **kwargs): - return Base.render(self, **kwargs) + return BaseFileFieldRenderer.render(self, **kwargs) class HandheldBatchFieldRenderer(fa.FieldRenderer): diff --git a/tailbone/forms/renderers/files.py b/tailbone/forms/renderers/files.py index 940275af..dd65f105 100644 --- a/tailbone/forms/renderers/files.py +++ b/tailbone/forms/renderers/files.py @@ -71,11 +71,15 @@ class FileFieldRenderer(fsblob.FileFieldRenderer): return 0 def get_url(self, filename): + """ + Must return a URL suitable for downloading the file + """ url = self.get_download_url() if url: if callable(url): return url(filename) return url + return self.view.get_action_url('download', self.field.parent.model) def get_download_url(self): if hasattr(self, 'download_url'):