Refactor the batch file field renderer somewhat

try to leverage the "common" file field renderer some more...
This commit is contained in:
Lance Edgar 2017-03-21 13:16:48 -05:00
parent 581a21bd9d
commit 43022c3205
2 changed files with 8 additions and 19 deletions

View file

@ -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):

View file

@ -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'):