From 09ba419ee3a938286ddc6c9b3464987d056fda97 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 6 Dec 2017 13:40:00 -0600 Subject: [PATCH] Refactor "exports" views to use master3 --- tailbone/views/exports.py | 76 +++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/tailbone/views/exports.py b/tailbone/views/exports.py index 1e044b97..af1ab20e 100644 --- a/tailbone/views/exports.py +++ b/tailbone/views/exports.py @@ -28,13 +28,15 @@ from __future__ import unicode_literals, absolute_import import os +import six + from rattail.db import model -import formalchemy as fa from pyramid.response import FileResponse +from webhelpers2.html import HTML, tags -from tailbone import forms -from tailbone.views import MasterView2 as MasterView +from tailbone import forms2 as forms +from tailbone.views import MasterView3 as MasterView class ExportMasterView(MasterView): @@ -52,10 +54,16 @@ class ExportMasterView(MasterView): 'record_count', ] + form_fields = [ + 'id', + 'created', + 'created_by', + 'record_count', + ] + def get_export_key(self): if hasattr(self, 'export_key'): return self.export_key - return def get_file_path(self, export, makedirs=False): return self.rattail_config.export_filepath(self.export_key, @@ -78,29 +86,51 @@ class ExportMasterView(MasterView): g.set_link('id') - def render_id(self, export, column): + def render_id(self, export, field): return export.id_str - def _preconfigure_fieldset(self, fs): - fs.id.set(label="ID", renderer=forms.renderers.BatchIDFieldRenderer) - fs.created_by.set(label="Created by", renderer=forms.renderers.UserFieldRenderer, - attrs={'hyperlink': True}) - if self.export_has_file and self.viewing: - download = forms.renderers.FileFieldRenderer.new( - self, storage_path=self.rattail_config.export_filedir(self.export_key), - file_path=self.get_file_path(fs.model), download_url=self.get_download_url) - fs.append(fa.Field('download', renderer=download)) + def configure_form(self, f): + super(ExportMasterView, self).configure_form(f) - def configure_fieldset(self, fs): - fields = [ - fs.id, - fs.created, - fs.created_by, - fs.record_count, - ] + # id + f.set_readonly('id') + f.set_renderer('id', self.render_id) + f.set_label('id', "ID") + + # created + f.set_readonly('created') + + # created_by + f.set_readonly('created_by') + f.set_renderer('created_by', self.render_created_by) + f.set_label('created_by', "Created by") + + # record_count + f.set_readonly('record_count') + + # download if self.export_has_file and self.viewing: - fields.append(fs.download) - fs.configure(include=fields) + # download = forms.renderers.FileFieldRenderer.new( + # self, storage_path=self.rattail_config.export_filedir(self.export_key), + # file_path=self.get_file_path(fs.model), download_url=self.get_download_url) + f.append('download') + f.set_renderer('download', self.render_download) + + def render_download(self, export, field): + path = self.get_file_path(export) + text = "{} ({})".format(export.filename, self.readable_size(path)) + url = self.request.route_url('{}.download'.format(self.get_route_prefix()), uuid=export.uuid) + return tags.link_to(text, url) + + def render_created_by(self, export, field): + user = export.created_by + if not user: + return "" + text = six.text_type(user) + if self.request.has_perm('users.view'): + url = self.request.route_url('users.view', uuid=user.uuid) + return tags.link_to(text, url) + return text def get_download_url(self, filename): uuid = self.request.matchdict['uuid']