add csv download option to device, port, and vlan search pages

This commit is contained in:
Eric A. Miller
2013-09-18 23:32:57 -04:00
parent d2b39a3f0e
commit 99ada1132c
10 changed files with 173 additions and 18 deletions

View File

@@ -11,21 +11,37 @@ use App::Netdisco::Web::Plugin;
register_search_tab({ tag => 'device', label => 'Device' });
my $headers = ['Device','Contact','Location','System Name','Model','OS Version','Management IP','Serial'];
# device with various properties or a default match-all
ajax '/ajax/content/search/device' => require_login sub {
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
sub get_rs_device {
my $q = shift;
my $has_opt = shift;
my $set;
if ($has_opt) {
$set = schema('netdisco')->resultset('Device')->search_by_field(scalar params);
}
else {
my $q = param('q');
send_error('Missing query', 400) unless $q;
$set = schema('netdisco')->resultset('Device')->search_fuzzy($q);
}
return $set;
}
ajax '/ajax/content/search/device' => require_login sub {
my $q = param('q');
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
unless ($has_opt || $q) {
send_error('Missing query', 400)
}
my $set = get_rs_device($q, $has_opt);
return unless $set->count;
content_type('text/html');
@@ -34,4 +50,33 @@ ajax '/ajax/content/search/device' => require_login sub {
}, { layout => undef };
};
get '/search/device' => require_login sub {
my $q = param('q');
my $format = param('format');
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
unless ($has_opt || $q) {
send_error('Missing query', 400)
}
my $set = get_rs_device($q, $has_opt);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-devicesearch.csv\"" );
template 'ajax/search/device_csv.tt', {
results => $set,
headers => $headers,
}, { layout => undef };
}
else {
return;
}
};
true;

View File

@@ -9,10 +9,9 @@ use App::Netdisco::Web::Plugin;
register_search_tab({ tag => 'port', label => 'Port' });
# device ports with a description (er, name) matching
ajax '/ajax/content/search/port' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
sub get_rs_port {
my $q = shift;
my $set;
if ($q =~ m/^\d+$/) {
@@ -28,6 +27,16 @@ ajax '/ajax/content/search/port' => require_login sub {
$set = schema('netdisco')->resultset('DevicePort')
->search({name => $query});
}
return $set;
}
# device ports with a description (er, name) matching
ajax '/ajax/content/search/port' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
my $set = get_rs_port($q);
return unless $set->count;
content_type('text/html');
@@ -36,4 +45,27 @@ ajax '/ajax/content/search/port' => require_login sub {
}, { layout => undef };
};
get '/search/port' => require_login sub {
my $q = param('q');
my $format = param('format');
send_error('Missing query', 400) unless $q;
my $set = get_rs_port($q);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-portsearch.csv\"" );
template 'ajax/search/port_csv.tt', {
results => $set,
}, { layout => undef };
}
else {
return;
}
};
true;

View File

@@ -7,26 +7,57 @@ use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_search_tab({ tag => 'vlan', label => 'VLAN' });
register_search_tab( { tag => 'vlan', label => 'VLAN' } );
# devices carrying vlan xxx
ajax '/ajax/content/search/vlan' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
sub get_rs_vlan {
my $q = shift;
my $set;
if ($q =~ m/^\d+$/) {
$set = schema('netdisco')->resultset('Device')->carrying_vlan({vlan => $q});
if ( $q =~ m/^\d+$/ ) {
$set = schema('netdisco')->resultset('Device')
->carrying_vlan( { vlan => $q } );
}
else {
$set = schema('netdisco')->resultset('Device')->carrying_vlan_name({name => $q});
$set = schema('netdisco')->resultset('Device')
->carrying_vlan_name( { name => $q } );
}
return $set;
}
ajax '/ajax/content/search/vlan' => require_login sub {
my $q = param('q');
send_error( 'Missing query', 400 ) unless $q;
my $set = get_rs_vlan($q);
return unless $set->count;
content_type('text/html');
template 'ajax/search/vlan.tt', {
results => $set,
}, { layout => undef };
template 'ajax/search/vlan.tt', { results => $set, }, { layout => undef };
};
get '/search/vlan' => require_login sub {
my $q = param('q');
my $format = param('format');
send_error( 'Missing query', 400 ) unless $q;
my $set = get_rs_vlan($q);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-vlansearch.csv\"" );
template 'ajax/search/vlan_csv.tt', { results => $set, },
{ layout => undef };
}
else {
return;
}
};
true;