Refactor begin hooks (closes #16)

This commit is contained in:
Oliver Gorwits
2012-04-09 15:02:06 +01:00
parent 3e8cfe5c50
commit 5ce85fd7bf
5 changed files with 36 additions and 31 deletions

View File

@@ -3,6 +3,7 @@
[ENHANCEMENTS]
* Native copy of the Netdisco sort_port routine (#17)
* Refactor begin hooks (closes #16)
[BUG FIXES]

View File

@@ -26,6 +26,17 @@ hook 'before_template' => sub {
$Template::Directive::WHILE_MAX = 10_000;
};
# set up query strings which describe default search options.
# these are used in both Device and Search templates
hook 'before_template' => sub {
var('query_defaults' => { map { ($_ => "tab=$_") } qw/node device/ });
var('query_defaults')->{node} .= "\&$_=". (param($_) || '')
for qw/stamps vendor archived partial/;
var('query_defaults')->{device} .= "\&$_=". (param($_) || '')
for qw/matchall/;
};
get '/' => sub {
template 'index';
};

View File

@@ -6,7 +6,7 @@ use Dancer::Plugin::DBIC;
use Digest::MD5 ();
hook 'before' => sub {
if (! session('user') && request->path !~ m{/login$}) {
if (! session('user') && request->path ne uri_for('/login')->path) {
if (setting('environment') eq 'development' and setting('no_auth')) {
session(user => 'developer');
}

View File

@@ -33,9 +33,9 @@ hook 'before' => sub {
{ name => 'n_archived', label => 'Archived Data', default => '' },
]);
# set up default search options for each type
if (request->path =~ m{/device$}) {
if (not param('tab') or param('tab') ne 'ports' or scalar keys %{params()} < 4) {
if (request->path eq uri_for('/device')->path) {
# new searches will have these defaults in the ports sidebar
if (not param('tab') or param('tab') ne 'ports') {
foreach my $col (@{ var('port_columns') }) {
params->{$col->{name}} = 'checked' if $col->{default} eq 'on';
}

View File

@@ -10,29 +10,33 @@ use List::MoreUtils ();
use Net::DNS ();
hook 'before' => sub {
# make hash lookups of query lists
foreach my $opt (qw/model vendor os_ver/) {
my $p = (ref [] eq ref param($opt) ? param($opt) : (param($opt) ? [param($opt)] : []));
var("${opt}_lkp" => { map { $_ => 1 } @$p });
}
# set up default search options for each type
if (request->path =~ m{/search$}) {
if (request->path eq uri_for('/search')->path) {
# new searches have these defaults in their sidebars
if (not param('tab') or param('tab') ne 'node') {
params->{'stamps'} = 'checked';
}
if (not param('tab') or param('tab') ne 'device') {
params->{'matchall'} = 'checked';
}
# used in the device search sidebar to populate select inputs
var('model_list' => [
schema('netdisco')->resultset('Device')->get_distinct('model')
]);
var('os_ver_list' => [
schema('netdisco')->resultset('Device')->get_distinct('os_ver')
]);
var('vendor_list' => [
schema('netdisco')->resultset('Device')->get_distinct('vendor')
]);
# used in the device search sidebar template to set selected items
foreach my $opt (qw/model vendor os_ver/) {
my $p = (ref [] eq ref param($opt) ? param($opt)
: (param($opt) ? [param($opt)] : []));
var("${opt}_lkp" => { map { $_ => 1 } @$p });
}
}
# set up query string defaults for hyperlinks to templates with forms
var('query_defaults' => { map { ($_ => "tab=$_") } qw/node device/ });
var('query_defaults')->{node} .= "\&$_=". (param($_) || '')
for qw/stamps vendor archived partial/;
var('query_defaults')->{device} .= "\&$_=". (param($_) || '')
for qw/matchall/;
};
# device with various properties or a default match-all
@@ -174,17 +178,6 @@ ajax '/ajax/content/search/port' => sub {
};
get '/search' => sub {
# set up property lists for device search
var('model_list' => [
schema('netdisco')->resultset('Device')->get_distinct('model')
]);
var('os_ver_list' => [
schema('netdisco')->resultset('Device')->get_distinct('os_ver')
]);
var('vendor_list' => [
schema('netdisco')->resultset('Device')->get_distinct('vendor')
]);
my $q = param('q');
if (not param('tab')) {
if (not $q) {