Add BatchMasterView.add_file_field() convenience method

This commit is contained in:
Lance Edgar 2017-03-21 13:19:38 -05:00
parent 0d830d595c
commit abb82c91f3
2 changed files with 23 additions and 10 deletions

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
# Copyright © 2010-2017 Lance Edgar
#
# This file is part of Rattail.
#
@ -41,7 +41,7 @@ from rattail.threads import Thread
from rattail.csvutil import UnicodeDictWriter
from rattail.util import load_object
import formalchemy
import formalchemy as fa
from pyramid import httpexceptions
from pyramid.renderers import render_to_response
from pyramid.response import FileResponse
@ -236,6 +236,17 @@ class BatchMasterView(MasterView):
if field in fs.render_fields:
delattr(fs, field)
def add_file_field(self, fs, name, **kwargs):
kwargs.setdefault('value', lambda b: getattr(b, 'filename_{}'.format(name)))
if 'renderer' not in kwargs:
batch = fs.model
storage_path = self.rattail_config.batch_filedir(self.handler.batch_key)
download_url = self.get_action_url('download', batch, _query={'file': name})
kwargs['renderer'] = FileFieldRenderer.new(self,
storage_path=storage_path,
download_url=download_url)
fs.append(fa.Field(name, **kwargs))
def save_create_form(self, form):
self.before_create(form)