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 { get '/device' => require_login sub {
my $q = param('q'); 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 => [ -or => [
\[ 'host(me.ip) = ?' => [ bind_value => $q ] ], \[ 'host(me.ip) = ?' => [ bind_value => $q ] ],
'me.dns' => $q, 'me.dns' => $q,
], ],
}); });
if (!defined $dev) { if ($dev->count == 0) {
return redirect uri_for('/', {nosuchdevice => 1})->path_query; 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'; params->{'tab'} ||= 'details';
template 'device', { template 'device', {
d => $dev, d => $dev,
display_name => ($others ? $first->ip : $first->dns),
device => params->{'tab'}, device => params->{'tab'},
}; };
}; };

View File

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

View File

@@ -116,7 +116,7 @@
</ul> </ul>
<form class="navbar-search pull-left" method="get" action="[% uri_for('/search') %]"> <form class="navbar-search pull-left" method="get" action="[% uri_for('/search') %]">
<input placeholder="Find Anything" class="search-query span3" <input placeholder="Find Anything" class="search-query span3"
id="nq" name="q" type="text" autocomplete="off"/> id="nq" name="q" value="[% display_name %]" type="text" autocomplete="off"/>
<div class="btn-group nd_navbar-search-group"> <div class="btn-group nd_navbar-search-group">
<button class="btn btn-inverse nd_navbar-search-icon"> <button class="btn btn-inverse nd_navbar-search-icon">
<span style="font-size: 18px;"> <span style="font-size: 18px;">