Based on jeneric's CSV download templates;

- try to reduce code duplication by using same route handler for ajax and csv,
  using request->is_ajax to switch the template, and set content-type

- use new HTML5 "download" attribute on links so content-disposition header is
  no longer necessary

- download CSV icon is placed on all tables (per report/device/serach section)

- update download CSV link using javascript just before table content is
  fetched - this is necessary to make sure updated sidebar query params are
  included

The idea here is to allow us to support CSV download in the pages which
display tables by only doing the following:

- (existing routes:) replace "ajax" with "get" route handler

- add logic to switch template in handler, based on request->is_ajax

- write _csv.tt version of the template, to spit out CSV file content

This makes it much easier for new devs to write reports supporting CSV, I
think?
This commit is contained in:
Oliver Gorwits
2013-09-20 09:23:57 +01:00
parent d3553d2623
commit ce57cdba69
4 changed files with 27 additions and 24 deletions

View File

@@ -1,7 +1,6 @@
package App::Netdisco::Web::Plugin::Report::HalfDuplex;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
@@ -13,19 +12,6 @@ register_report({
label => 'Ports in Half Duplex Mode',
});
ajax '/ajax/content/report/halfduplex' => require_login sub {
my $set = schema('netdisco')->resultset('DevicePort')->search(
{ up => 'up', duplex => { '-ilike' => 'half' } },
{ order_by => [qw/device.dns port/], prefetch => 'device' },
);
return unless $set->count;
content_type('text/html');
template 'ajax/report/halfduplex.tt', {
results => $set,
}, { layout => undef };
};
get '/ajax/content/report/halfduplex' => require_login sub {
my $format = param('format');
my $set = schema('netdisco')->resultset('DevicePort')->search(
@@ -34,16 +20,14 @@ get '/ajax/content/report/halfduplex' => require_login sub {
);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"halfduplex.csv\"" );
template 'ajax/report/halfduplex_csv.tt', { results => $set, },
if (request->is_ajax) {
template 'ajax/report/halfduplex.tt', { results => $set, },
{ layout => undef };
}
else {
return;
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/report/halfduplex_csv.tt', { results => $set, },
{ layout => undef };
}
};