]>
iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Generic.pm
1 package Gruntmaster
::Page
::Generic
;
6 our $VERSION = '0.001';
9 use Gruntmaster
::Page
::Base
;
10 use JSON qw
/encode_json decode_json/;
14 my $cp = $Gruntmaster::Data
::contest ?
"contest.$Gruntmaster::Data::contest." : '';
15 map { { id
=> $_, HGETALL
"$cp$hash.$_" } } SMEMBERS
"$cp$hash"
19 my ($key, $value) = @_;
25 my ($pkg, $id, $title) = @_;
26 my $fn = $pkg =~ s
,::,/,gr
;
27 return if $INC{"$fn.pm"};
29 Gruntmaster
::Page
::Base
->import_to($pkg, $id, $title);
34 my ($thing, $lang, $env, $ct) = @_;
36 undef $ct unless $thing{contest
};
37 debug
$env => "Contest is $ct";
38 local $Gruntmaster::Data
::contest
= $ct if $ct;
39 my @thing = hgetall
$thing{hash
};
40 @thing = map { $thing{mangle
}->(); $_ } @thing if exists $thing{mangle
};
41 @thing = grep { $thing{choose
}->() } @thing if exists $thing{choose
};
42 @thing = sort { $thing{sortby
}->() } @thing if exists $thing{sortby
};
44 $thing{group
} //= sub { $thing{id
} };
46 my $group = $thing{group
}->();
47 $params{$group} //= [];
48 push $params{$group}, $_
50 wantarray ?
%params : \
%params
54 my ($thing, $lang, $env, $id, $ct) = @_;
56 ($id, $ct) = ($ct, $id) if $thing{contest
};
57 local $Gruntmaster::Data
::contest
= $ct if $ct;
58 debug
$env => "Hash is $thing{hash} and id is $id";
59 my %params = HGETALL
$ct ?
"contest.$ct.$thing{hash}.$id" : "$thing{hash}.$id";
60 $thing{mangle
}->(local $_ = \
%params) if exists $thing{mangle
};
61 wantarray ?
%params : \
%params
64 sub headers
($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
68 my $ucid = ucfirst $thing{id
};
69 my $pkg = "Gruntmaster::Page::$ucid";
71 putsym
"${pkg}::_generate", sub { $_[1]->param(list \
%thing, @_[2..$#_]) } if makepkg
$pkg, @thing{qw
/id title/};
72 putsym
"${pkg}::Entry::_generate", sub { $_[1]->param(entry \
%thing, @_[2..$#_]) } if makepkg
"${pkg}::Entry", "$thing{id}_entry", '<tmpl_var name>';
73 putsym
"${pkg}::Read::generate", sub { [200, headers
shift, [encode_json list \
%thing, @_]] } if makepkg
"${pkg}::Read";
74 putsym
"${pkg}::Entry::Read::generate", sub { [200, headers
shift, [encode_json entry \
%thing, @_]] } if makepkg
"${pkg}::Entry::Read";
87 local *{"params"} = sub { @thing{qw
/id hash title/} = @_ };
88 local *{"choose"} = sub { $thing{choose
} = shift };
89 local *{"sortby"} = sub { $thing{sortby
} = shift };
90 local *{"mangle"} = sub { $thing{mangle
} = shift };
91 local *{"group"} = sub { $thing{group
} = shift };
92 local *{"contest"} = sub { $thing{contest
} = 1 };
99 ##################################################
102 params qw
/us user Users/;
103 choose
{ $_->{name
} =~ /\w/ };
104 sortby
{ lc $a->{name
} cmp lc $b->{name
} };
108 params qw
/pb problem Problems/;
110 sortby
{ $a->{name
} cmp $b->{name
} };
111 group
{ $_->{level
} };
112 mangle
{ $_->{owner_name
} = do { local $Gruntmaster::Data
::contest
; user_name
$_->{owner
} } }
116 params qw
/ct contest Contests/;
117 sortby
{ $a->{start
} <=> $b->{start
} };
118 group
{ time < $_->{start
} ?
'pending' : time > $_->{end
} ?
'finished' : 'running' };
119 mangle
{ $_->{started
} = time >= $_->{start
}; $_->{owner_name
} = do { local $Gruntmaster::Data
::contest
; user_name
$_->{owner
} } };
123 params qw
/log job/, 'Job log';
125 mangle
{ $_->{results
} &&= decode_json
$_->{results
}; $_->{user_name
} = do { local $Gruntmaster::Data
::contest
; user_name
$_->{user
} } }
This page took 0.052108 seconds and 4 git commands to generate.