diff --git a/Changes b/Changes index 88f8566b..6bc4c6c8 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,8 @@ -0.7 - +0.7 - 2012-11-25 [NEW FEATURES] + * Add Port Control (admin table) features * Implement DBIx::Class::Schema::Versioned support - DB schema is now versioned * Navbar query for device with single hit redirects to Device details page * Show device name on device details views (closes #30) diff --git a/Netdisco/lib/Netdisco/Web.pm b/Netdisco/lib/Netdisco/Web.pm index 9b7fbf4b..f4861ba4 100644 --- a/Netdisco/lib/Netdisco/Web.pm +++ b/Netdisco/lib/Netdisco/Web.pm @@ -12,6 +12,7 @@ use URI::QueryParam (); # part of URI, to add helper methods use Netdisco::Web::AuthN; use Netdisco::Web::Search; use Netdisco::Web::Device; +use Netdisco::Web::PortControl; use Netdisco::Web::Inventory; hook 'before_template' => sub { diff --git a/Netdisco/lib/Netdisco/Web/PortControl.pm b/Netdisco/lib/Netdisco/Web/PortControl.pm new file mode 100644 index 00000000..9bcf299d --- /dev/null +++ b/Netdisco/lib/Netdisco/Web/PortControl.pm @@ -0,0 +1,65 @@ +package Netdisco::Web::PortControl; + +use Dancer ':syntax'; +use Dancer::Plugin::Ajax; +use Dancer::Plugin::DBIC; +use Try::Tiny; + +ajax '/ajax/portcontrol' => sub { + try { + my $log = sprintf 'd:[%s] p:[%s] f:[%s]. a:[%s] v[%s]', + param('device'), (param('port') || ''), param('field'), + (param('action') || ''), (param('value') || ''); + + my %action_map = ( + 'location' => 'location', + 'contact' => 'contact', + 'c_port' => 'portcontrol', + 'c_name' => 'portname', + 'c_vlan' => 'vlan', + ); + + my $action = $action_map{ param('field') }; + my $subaction = ($action eq 'portcontrol' + ? (param('action') ."-other") + : param('value')); + + schema('netdisco')->resultset('Admin')->create({ + device => param('device'), + port => param('port'), + action => $action, + subaction => $subaction, + status => 'queued', + username => session('user'), + userip => request->remote_address, + log => $log, + }); + } + catch { + send_error('Failed to parse params or add DB record'); + }; + + content_type('application/json'); + to_json({}); +}; + +ajax '/ajax/userlog' => sub { + my $user = session('user'); + send_error('No username') unless $user; + + my $rs = schema('netdisco')->resultset('Admin')->search({ + username => $user, + action => [qw/portcontrol vlan location/], + finished => { '>' => \"(now() - interval '5 seconds')" }, + }); + + my %status = ( + 'done' => $rs->search({status => 'done'})->count(), + 'error' => $rs->search({status => 'error'})->count(), + ); + + content_type('application/json'); + to_json(\%status); +}; + +true; diff --git a/Netdisco/public/javascripts/netdisco_portcontrol.js b/Netdisco/public/javascripts/netdisco_portcontrol.js index be32305f..c021060f 100644 --- a/Netdisco/public/javascripts/netdisco_portcontrol.js +++ b/Netdisco/public/javascripts/netdisco_portcontrol.js @@ -16,7 +16,9 @@ function port_control (e) { toastr.info('Submitted change request'); } ,error: function() { - toastr.error('Failed to submit change request') + toastr.error('Failed to submit change request'); + document.execCommand('undo'); + $(e).blur(); } }); } @@ -26,6 +28,22 @@ function port_control (e) { $.ajax({ url: uri_base + '/ajax/userlog' ,success: function(data) { + // console.log(data); + + if (data['error'] == 1 ) { + toastr.error('1 recent failed change request'); + } + else if (data['error'] > 1) { + toastr.error(data['error'] + ' recent failed change requests'); + } + + if (data['done'] == 1 ) { + toastr.success('1 recent successful change request'); + } + else if (data['done'] > 1) { + toastr.success(data['done'] + ' recent successful change requests'); + } + // Schedule next request when the current one's complete setTimeout(worker, 5000); } diff --git a/Netdisco/views/js/device.js b/Netdisco/views/js/device.js index af63b886..637505d9 100644 --- a/Netdisco/views/js/device.js +++ b/Netdisco/views/js/device.js @@ -62,8 +62,8 @@ } else if (nl) { - event.preventDefault(); $(this).blur(); + event.preventDefault(); dirty = false; port_control(this); // save }