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 # Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar # Copyright © 2010-2017 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -41,7 +41,7 @@ from rattail.threads import Thread
from rattail.csvutil import UnicodeDictWriter from rattail.csvutil import UnicodeDictWriter
from rattail.util import load_object from rattail.util import load_object
import formalchemy import formalchemy as fa
from pyramid import httpexceptions from pyramid import httpexceptions
from pyramid.renderers import render_to_response from pyramid.renderers import render_to_response
from pyramid.response import FileResponse from pyramid.response import FileResponse
@ -236,6 +236,17 @@ class BatchMasterView(MasterView):
if field in fs.render_fields: if field in fs.render_fields:
delattr(fs, field) 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): def save_create_form(self, form):
self.before_create(form) self.before_create(form)

View file

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8; -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
@ -31,7 +31,7 @@ from sqlalchemy import orm
from rattail.util import prettify from rattail.util import prettify
import formalchemy import formalchemy as fa
from pyramid import httpexceptions from pyramid import httpexceptions
from pyramid.renderers import get_renderer, render_to_response, render from pyramid.renderers import get_renderer, render_to_response, render
from webhelpers.html import HTML, tags from webhelpers.html import HTML, tags
@ -703,12 +703,14 @@ class MasterView(View):
""" """
return getattr(cls, 'index_title', cls.get_model_title_plural()) return getattr(cls, 'index_title', cls.get_model_title_plural())
def get_action_url(self, action, instance): def get_action_url(self, action, instance, **kwargs):
""" """
Generate a URL for the given action on the given instance. Generate a URL for the given action on the given instance
""" """
return self.request.route_url('{0}.{1}'.format(self.get_route_prefix(), action), kw = self.get_action_route_kwargs(instance)
**self.get_action_route_kwargs(instance)) kw.update(kwargs)
route_prefix = self.get_route_prefix()
return self.request.route_url('{}.{}'.format(route_prefix, action), **kw)
def render_to_response(self, template, data, mobile=False): def render_to_response(self, template, data, mobile=False):
""" """
@ -1100,7 +1102,7 @@ class MasterView(View):
""" """
kwargs.setdefault('session', self.Session()) kwargs.setdefault('session', self.Session())
kwargs.setdefault('request', self.request) kwargs.setdefault('request', self.request)
fieldset = formalchemy.FieldSet(instance, **kwargs) fieldset = fa.FieldSet(instance, **kwargs)
fieldset.prettify = prettify fieldset.prettify = prettify
return fieldset return fieldset