add frontend update/del forms, and display port count

This commit is contained in:
Oliver Gorwits
2013-05-06 00:45:18 +01:00
parent f0899e16b3
commit f75f1e5cbf
5 changed files with 100 additions and 3 deletions

View File

@@ -180,6 +180,16 @@ __PACKAGE__->has_many(
=head1 ADDITIONAL COLUMNS
=head2 port_count
Returns the number of ports on this device. Enable this
column by applying the C<with_port_count()> modifier to C<search()>.
=cut
sub port_count { return (shift)->get_column('port_count') }
=head2 uptime_age
Formatted version of the C<uptime> field.

View File

@@ -491,4 +491,34 @@ sub get_distinct_col {
)->get_column($col)->all;
}
=head2 with_port_count
This is a modifier for any C<search()> which
will add the following additional synthesized column to the result set:
=over 4
=item port_count
=back
=cut
sub with_port_count {
my ($rs, $cond, $attrs) = @_;
return $rs
->search_rs($cond, $attrs)
->search({},
{
'+columns' => { port_count =>
$rs->result_source->schema->resultset('DevicePort')
->search(
{ 'dp.ip' => { -ident => 'me.ip' } },
{ alias => 'dp' }
)->count_rs->as_query
},
});
}
1;

View File

@@ -5,18 +5,64 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
use Try::Tiny;
register_admin_task({
tag => 'pseudodevice',
label => 'Manage Pseudo Devices',
});
sub _sanity_ok {
my $happy = 0;
try {
return 0 unless length param('dns')
and param('dns') =~ m/^[[:print:]]+$/
and param('dns') !~ m/[[:space:]]/;
my $ip = NetAddr::IP::Lite->new(param('ip'));
return 0 if $ip->addr eq '0.0.0.0';
return 0 unless length param('ports')
and param('ports') =~ m/^[[:digit:]]+$/;
$happy = 1;
};
return $happy;
}
ajax '/ajax/content/admin/pseudodevice/add' => sub {
forward '/ajax/content/admin/pseudodevice'
unless _sanity_ok();
try {
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->create({
ip => param('ip'),
dns => param('dns'),
vendor => 'netdisco',
last_discover => \'now()',
});
$device->ports->populate([
['port'],
map {["Port$_"]} @{[1 .. param('ports')]},
]);
});
};
forward '/ajax/content/admin/pseudodevice';
};
ajax '/ajax/content/admin/pseudodevice' => sub {
my $set = schema('netdisco')->resultset('Device')
->search(
{vendor => 'netdisco'},
{order_by => { -desc => 'last_discover' }},
);
)->with_port_count;
content_type('text/html');
template 'ajax/admintask/pseudodevice.tt', {