add csv download option to device, port, and vlan search pages
This commit is contained in:
		| @@ -11,21 +11,37 @@ use App::Netdisco::Web::Plugin; | |||||||
|  |  | ||||||
| register_search_tab({ tag => 'device', label => 'Device' }); | 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 | # device with various properties or a default match-all | ||||||
| ajax '/ajax/content/search/device' => require_login sub { | sub get_rs_device { | ||||||
|     my $has_opt = List::MoreUtils::any {param($_)} |     my $q = shift; | ||||||
|       qw/name location dns ip description model os_ver vendor/; |     my $has_opt = shift; | ||||||
|  |  | ||||||
|     my $set; |     my $set; | ||||||
|  |  | ||||||
|     if ($has_opt) { |     if ($has_opt) { | ||||||
|         $set = schema('netdisco')->resultset('Device')->search_by_field(scalar params); |         $set = schema('netdisco')->resultset('Device')->search_by_field(scalar params); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         my $q = param('q'); |  | ||||||
|         send_error('Missing query', 400) unless $q; |         send_error('Missing query', 400) unless $q; | ||||||
|  |  | ||||||
|         $set = schema('netdisco')->resultset('Device')->search_fuzzy($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; |     return unless $set->count; | ||||||
|  |  | ||||||
|     content_type('text/html'); |     content_type('text/html'); | ||||||
| @@ -34,4 +50,33 @@ ajax '/ajax/content/search/device' => require_login sub { | |||||||
|     }, { layout => undef }; |     }, { 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; | true; | ||||||
|   | |||||||
| @@ -9,10 +9,9 @@ use App::Netdisco::Web::Plugin; | |||||||
|  |  | ||||||
| register_search_tab({ tag => 'port', label => 'Port' }); | register_search_tab({ tag => 'port', label => 'Port' }); | ||||||
|  |  | ||||||
| # device ports with a description (er, name) matching | sub get_rs_port { | ||||||
| ajax '/ajax/content/search/port' => require_login sub { |     my $q = shift; | ||||||
|     my $q = param('q'); |  | ||||||
|     send_error('Missing query', 400) unless $q; |  | ||||||
|     my $set; |     my $set; | ||||||
|  |  | ||||||
|     if ($q =~ m/^\d+$/) { |     if ($q =~ m/^\d+$/) { | ||||||
| @@ -28,6 +27,16 @@ ajax '/ajax/content/search/port' => require_login sub { | |||||||
|         $set = schema('netdisco')->resultset('DevicePort') |         $set = schema('netdisco')->resultset('DevicePort') | ||||||
|           ->search({name => $query}); |           ->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; |     return unless $set->count; | ||||||
|  |  | ||||||
|     content_type('text/html'); |     content_type('text/html'); | ||||||
| @@ -36,4 +45,27 @@ ajax '/ajax/content/search/port' => require_login sub { | |||||||
|     }, { layout => undef }; |     }, { 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; | true; | ||||||
|   | |||||||
| @@ -10,23 +10,54 @@ use App::Netdisco::Web::Plugin; | |||||||
| register_search_tab( { tag => 'vlan', label => 'VLAN' } ); | register_search_tab( { tag => 'vlan', label => 'VLAN' } ); | ||||||
|  |  | ||||||
| # devices carrying vlan xxx | # devices carrying vlan xxx | ||||||
| ajax '/ajax/content/search/vlan' => require_login sub { | sub get_rs_vlan { | ||||||
|     my $q = param('q'); |     my $q = shift; | ||||||
|     send_error('Missing query', 400) unless $q; |  | ||||||
|     my $set; |     my $set; | ||||||
|  |  | ||||||
|     if ( $q =~ m/^\d+$/ ) { |     if ( $q =~ m/^\d+$/ ) { | ||||||
|         $set = schema('netdisco')->resultset('Device')->carrying_vlan({vlan => $q}); |         $set = schema('netdisco')->resultset('Device') | ||||||
|  |             ->carrying_vlan( { vlan => $q } ); | ||||||
|     } |     } | ||||||
|     else { |     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; |     return unless $set->count; | ||||||
|  |  | ||||||
|     content_type('text/html'); |     content_type('text/html'); | ||||||
|     template 'ajax/search/vlan.tt', { |     template 'ajax/search/vlan.tt', { results => $set, }, { layout => undef }; | ||||||
|       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; | true; | ||||||
|   | |||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | [% INCLUDE "download_as.tt" %] | ||||||
| <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | ||||||
|   <thead> |   <thead> | ||||||
|     <tr> |     <tr> | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								Netdisco/share/views/ajax/search/device_csv.tt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Netdisco/share/views/ajax/search/device_csv.tt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | [% USE CSV -%] | ||||||
|  | [% CSV.dump(headers) %] | ||||||
|  |  | ||||||
|  | [% WHILE (row = results.next) %] | ||||||
|  |   [% mylist = [] %] | ||||||
|  |   [% device = row.dns || row.ip %] | ||||||
|  |   [% FOREACH col IN [ device row.contact row.location row.name row.model row.os_ver row.ip row.serial] %] | ||||||
|  |     [% mylist.push(col) %] | ||||||
|  |   [% END %] | ||||||
|  |   [% CSV.dump(mylist) %] | ||||||
|  |  | ||||||
|  | [% END %] | ||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | [% INCLUDE "download_as.tt" %] | ||||||
| <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | ||||||
|   <thead> |   <thead> | ||||||
|     <tr> |     <tr> | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								Netdisco/share/views/ajax/search/port_csv.tt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Netdisco/share/views/ajax/search/port_csv.tt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | [% USE CSV -%] | ||||||
|  | [% CSV.dump([ 'Description' 'Port' 'Name' 'Vlan' ]) %] | ||||||
|  |  | ||||||
|  | [% WHILE (row = results.next) %] | ||||||
|  |   [% mylist = [] %] | ||||||
|  |   [% myport = "$row.ip [ $row.port ] (" _ row.device.dns _ ")" IF row.device.dns %] | ||||||
|  |   [% FOREACH col IN [ row.name myport row.descr row.vlan ] %] | ||||||
|  |     [% mylist.push(col) %] | ||||||
|  |   [% END %] | ||||||
|  |   [% CSV.dump(mylist) %] | ||||||
|  |  | ||||||
|  | [% END %] | ||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | [% INCLUDE "download_as.tt" %] | ||||||
| <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | <table class="table table-bordered table-condensed table-striped nd_floatinghead"> | ||||||
|   <thead> |   <thead> | ||||||
|     <tr> |     <tr> | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								Netdisco/share/views/ajax/search/vlan_csv.tt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Netdisco/share/views/ajax/search/vlan_csv.tt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | [% USE CSV -%] | ||||||
|  | [% CSV.dump([ 'Vlan' 'Device' 'Description' 'Model' 'OS' 'Vendor' ]) %] | ||||||
|  |  | ||||||
|  | [% WHILE (row = results.next) %] | ||||||
|  |   [% mylist = [] %] | ||||||
|  |   [% device = row.dns || row.ip %] | ||||||
|  |   [% FOREACH col IN [ row.vlan.vlan device row.vlan.description row.model row.os row.vendor ] %] | ||||||
|  |     [% mylist.push(col) %] | ||||||
|  |   [% END %] | ||||||
|  |   [% CSV.dump(mylist) %] | ||||||
|  |  | ||||||
|  | [% END %] | ||||||
							
								
								
									
										8
									
								
								Netdisco/share/views/download_as.tt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Netdisco/share/views/download_as.tt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | [% myuri = request.uri %] | ||||||
|  | [% UNLESS myuri.match('/report') %] | ||||||
|  |     [% myuri = myuri.remove('/ajax/content/') %] | ||||||
|  | [% END %] | ||||||
|  | <div> | ||||||
|  |   <p class="text-right">Download as: <a href="[% myuri %]&format=csv">CSV</a></p> | ||||||
|  | </div> | ||||||
|  |  | ||||||
		Reference in New Issue
	
	Block a user