From ac2976940203f3c7eeda71c5086e85930845a0c3 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Fri, 20 Sep 2013 19:28:49 +0100 Subject: [PATCH] use single route handler for ajax and csv response --- .../Netdisco/Web/Plugin/Device/Addresses.pm | 31 ++-------- .../Web/Plugin/Report/ApChannelDist.pm | 33 +++------- .../Web/Plugin/Report/DuplexMismatch.pm | 26 +++----- .../Netdisco/Web/Plugin/Report/HalfDuplex.pm | 1 + .../Web/Plugin/Report/PortUtilization.pm | 24 ++------ .../App/Netdisco/Web/Plugin/Search/Device.pm | 61 ++++--------------- .../App/Netdisco/Web/Plugin/Search/Port.pm | 48 +++------------ .../App/Netdisco/Web/Plugin/Search/VLAN.pm | 43 +++---------- 8 files changed, 58 insertions(+), 209 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Addresses.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Addresses.pm index 3633b9b8..83f5186e 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Addresses.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Device/Addresses.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Device::Addresses; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -10,7 +9,7 @@ use App::Netdisco::Web::Plugin; register_device_tab( { tag => 'addresses', label => 'Addresses' } ); # device interface addresses -ajax '/ajax/content/device/addresses' => require_login sub { +get '/ajax/content/device/addresses' => require_login sub { my $q = param('q'); my $device @@ -20,32 +19,14 @@ ajax '/ajax/content/device/addresses' => require_login sub { my $set = $device->device_ips->search( {}, { order_by => 'alias' } ); return unless $set->count; - content_type('text/html'); - template 'ajax/device/addresses.tt', { results => $set, }, - { layout => undef }; -}; - -get '/device/addresses' => require_login sub { - my $q = param('q'); - my $format = param('format'); - - my $device - = schema('netdisco')->resultset('Device')->search_for_device($q) - or send_error( 'Bad device', 400 ); - - my $set = $device->device_ips->search( {}, { order_by => 'alias' } ); - return unless $set->count; - - if ( $format eq 'csv' ) { - - header( 'Content-Type' => 'text/comma-separated-values' ); - header( 'Content-Disposition' => - "attachment; filename=\"addresses.csv\"" ); - template 'ajax/device/addresses_csv.tt', { results => $set, }, + if (request->is_ajax) { + template 'ajax/device/addresses.tt', { results => $set, }, { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/device/addresses_csv.tt', { results => $set, }, + { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/ApChannelDist.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/ApChannelDist.pm index 56935d48..8bdf8c07 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/ApChannelDist.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/ApChannelDist.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Report::ApChannelDist; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -11,10 +10,11 @@ register_report( { category => 'Wireless', tag => 'apchanneldist', label => 'Access Point Channel Distribution', + provides_csv => 1, } ); -sub get_rs_apchdist { +get '/ajax/content/report/apchanneldist' => require_login sub { my $set = schema('netdisco')->resultset('DevicePortWireless')->search( { channel => { '!=', '0' } }, { select => [ 'channel', { count => 'channel' } ], @@ -23,35 +23,16 @@ sub get_rs_apchdist { order_by => { -desc => [qw/count/] }, }, ); - return $set; -} - -ajax '/ajax/content/report/apchanneldist' => require_login sub { - my $set = get_rs_apchdist(); - return unless $set->count; - content_type('text/html'); - template 'ajax/report/apchanneldist.tt', { results => $set, }, - { layout => undef }; -}; - -get '/ajax/content/report/apchanneldist' => require_login sub { - my $format = param('format'); - my $set = get_rs_apchdist(); - - return unless $set->count; - - if ( $format eq 'csv' ) { - - header( 'Content-Type' => 'text/comma-separated-values' ); - header( 'Content-Disposition' => - "attachment; filename=\"apchanneldist.csv\"" ); - template 'ajax/report/apchanneldist_csv.tt', { results => $set, }, + if (request->is_ajax) { + template 'ajax/report/apchanneldist.tt', { results => $set, }, { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/report/apchanneldist_csv.tt', { results => $set, }, + { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/DuplexMismatch.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/DuplexMismatch.pm index 7cd99e0c..35806b48 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/DuplexMismatch.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/DuplexMismatch.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Report::DuplexMismatch; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -11,33 +10,22 @@ register_report( { category => 'Port', tag => 'duplexmismatch', label => 'Duplex Mismatches Between Devices', + provides_csv => 1, } ); -ajax '/ajax/content/report/duplexmismatch' => require_login sub { +get '/ajax/content/report/duplexmismatch' => require_login sub { my $set = schema('netdisco')->resultset('Virtual::DuplexMismatch'); return unless $set->count; - content_type('text/html'); - template 'ajax/report/duplexmismatch.tt', { results => $set, }, - { layout => undef }; -}; - -get '/ajax/content/report/duplexmismatch' => require_login sub { - my $format = param('format'); - my $set = schema('netdisco')->resultset('Virtual::DuplexMismatch'); - return unless $set->count; - - if ( $format eq 'csv' ) { - - header( 'Content-Type' => 'text/comma-separated-values' ); - header( 'Content-Disposition' => - "attachment; filename=\"duplexmismatch.csv\"" ); - template 'ajax/report/duplexmismatch_csv.tt', { results => $set, }, + if (request->is_ajax) { + template 'ajax/report/duplexmismatch.tt', { results => $set, }, { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/report/duplexmismatch_csv.tt', { results => $set, }, + { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm index 065786ac..bf698d39 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/HalfDuplex.pm @@ -10,6 +10,7 @@ register_report({ category => 'Port', tag => 'halfduplex', label => 'Ports in Half Duplex Mode', + provides_csv => 1, }); get '/ajax/content/report/halfduplex' => require_login sub { diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortUtilization.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortUtilization.pm index 657b82ad..a9701974 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortUtilization.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortUtilization.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Report::PortUtilization; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -11,33 +10,22 @@ register_report( { category => 'Device', tag => 'portutilization', label => 'Port Utilization', + provides_csv => 1, } ); -ajax '/ajax/content/report/portutilization' => require_login sub { - return unless schema('netdisco')->resultset('Device')->count; - my $set = schema('netdisco')->resultset('Virtual::PortUtilization'); - - content_type('text/html'); - template 'ajax/report/portutilization.tt', { results => $set, }, - { layout => undef }; -}; - get '/ajax/content/report/portutilization' => require_login sub { - my $format = param('format'); return unless schema('netdisco')->resultset('Device')->count; my $set = schema('netdisco')->resultset('Virtual::PortUtilization'); - if ( $format eq 'csv' ) { - - header( 'Content-Type' => 'text/comma-separated-values' ); - header( 'Content-Disposition' => - "attachment; filename=\"portutilization.csv\"" ); - template 'ajax/report/portutilization_csv.tt', { results => $set, }, + if (request->is_ajax) { + template 'ajax/report/portutilization.tt', { results => $set, }, { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/report/portutilization_csv.tt', { results => $set, }, + { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm index 58aef8f8..7e42246f 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Search::Device; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -14,68 +13,32 @@ 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 -sub get_rs_device { - my $q = shift; - my $has_opt = shift; - +get '/ajax/content/search/device' => require_login sub { + my $has_opt = List::MoreUtils::any {param($_)} + qw/name location dns ip description model os_ver vendor/; 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'); - template 'ajax/search/device.tt', { - results => $set, - }, { 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 }; + if (request->is_ajax) { + template 'ajax/search/device.tt', {results => $set}, + { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/search/device_csv.tt', { + results => $set, + headers => $headers, + }, { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm index f3e07550..986d87b9 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Search::Port; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -9,9 +8,10 @@ use App::Netdisco::Web::Plugin; register_search_tab({ tag => 'port', label => 'Port' }); -sub get_rs_port { - my $q = shift; - +# device ports with a description (er, name) matching +get '/ajax/content/search/port' => require_login sub { + my $q = param('q'); + send_error('Missing query', 400) unless $q; my $set; if ($q =~ m/^\d+$/) { @@ -27,44 +27,16 @@ sub get_rs_port { $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'); - template 'ajax/search/port.tt', { - results => $set, - }, { 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 }; + if (request->is_ajax) { + template 'ajax/search/port.tt', {results => $set}, + { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/search/port_csv.tt', {results => $set}, + { layout => undef }; } }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm index 974c4e65..ee4c7911 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm @@ -1,7 +1,6 @@ package App::Netdisco::Web::Plugin::Search::VLAN; use Dancer ':syntax'; -use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; @@ -10,9 +9,9 @@ use App::Netdisco::Web::Plugin; register_search_tab( { tag => 'vlan', label => 'VLAN' } ); # devices carrying vlan xxx -sub get_rs_vlan { - my $q = shift; - +get '/ajax/content/search/vlan' => require_login sub { + my $q = param('q'); + send_error( 'Missing query', 400 ) unless $q; my $set; if ( $q =~ m/^\d+$/ ) { @@ -23,40 +22,16 @@ sub get_rs_vlan { $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 }; -}; - -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 }; + if (request->is_ajax) { + template 'ajax/search/vlan.tt', { results => $set} + { layout => undef }; } else { - return; + header( 'Content-Type' => 'text/comma-separated-values' ); + template 'ajax/search/vlan_csv.tt', { results => $set} + { layout => undef }; } };