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 =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 =head2 uptime_age
Formatted version of the C<uptime> field. Formatted version of the C<uptime> field.

View File

@@ -491,4 +491,34 @@ sub get_distinct_col {
)->get_column($col)->all; )->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; 1;

View File

@@ -5,18 +5,64 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
use Try::Tiny;
register_admin_task({ register_admin_task({
tag => 'pseudodevice', tag => 'pseudodevice',
label => 'Manage Pseudo Devices', 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 { ajax '/ajax/content/admin/pseudodevice' => sub {
my $set = schema('netdisco')->resultset('Device') my $set = schema('netdisco')->resultset('Device')
->search( ->search(
{vendor => 'netdisco'}, {vendor => 'netdisco'},
{order_by => { -desc => 'last_discover' }}, {order_by => { -desc => 'last_discover' }},
); )->with_port_count;
content_type('text/html'); content_type('text/html');
template 'ajax/admintask/pseudodevice.tt', { template 'ajax/admintask/pseudodevice.tt', {

View File

@@ -196,6 +196,10 @@ td {
margin-bottom: 2px; margin-bottom: 2px;
} }
.nd_inline_form {
display: inline;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* tabs */ /* tabs */

View File

@@ -20,11 +20,18 @@
</tr> </tr>
[% WHILE (row = results.next) %] [% WHILE (row = results.next) %]
<tr> <tr>
<form name="update">
<td class="center_cell"><a class="nd_linkcell" <td class="center_cell"><a class="nd_linkcell"
href="[% device_ports %]&q=[% row.dns | uri %]">[% row.dns | html_entity %]</a></td> href="[% device_ports %]&q=[% row.dns | uri %]">[% row.dns | html_entity %]</a></td>
<td class="center_cell">[% row.ip | html_entity %]</td> <td class="center_cell">[% row.ip | html_entity %]</td>
<td class="center_cell">[% row.ports.count | html_entity %]</td> <td class="center_cell"><input name="ports" type="number" value="[% row.port_count | html_entity %]"</td>
<td class="center_cell"><a class="btn btn-small" href="#"><i class="icon-plus-sign"></i> Add</a></td> <td class="center_cell">
<button class="btn" name="update" type="submit"><i class="icon-save text-warning"></i></button>
</form>
<form name="del" class="nd_inline_form">
<button class="btn" name="del" type="submit"><i class="icon-trash text-error"></i></button>
</form>
</td>
</tr> </tr>
[% END %] [% END %]
</tbody> </tbody>