on call to /device, check for duplicates by dns

This commit is contained in:
Oliver Gorwits
2014-01-30 07:14:43 +00:00
parent d1b0e6be24
commit 132dc97bc6
3 changed files with 13 additions and 4 deletions

View File

@@ -154,20 +154,29 @@ hook 'before_template' => sub {
get '/device' => require_login sub {
my $q = param('q');
my $dev = schema('netdisco')->resultset('Device')->single({
my $schema = schema('netdisco')->resultset('Device');
# we are passed either dns or ip
my $dev = $schema->search({
-or => [
\[ 'host(me.ip) = ?' => [ bind_value => $q ] ],
'me.dns' => $q,
],
});
if (!defined $dev) {
if ($dev->count == 0) {
return redirect uri_for('/', {nosuchdevice => 1})->path_query;
}
# if passed dns, need to check for duplicates
# and use only ip for q param, if there are duplicates.
my $first = $dev->first;
my $others = ($schema->search({dns => $first->dns})->count() - 1);
params->{'tab'} ||= 'details';
template 'device', {
d => $dev,
display_name => ($others ? $first->ip : $first->dns),
device => params->{'tab'},
};
};

View File

@@ -81,7 +81,7 @@ get '/search' => require_login sub {
# redirect to device details for the one device
return redirect uri_for('/device', {
tab => 'details',
q => ($nd->first->dns || $nd->first->ip),
q => $nd->first->ip,
f => '',
})->path_query;
}