]>
iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Generic.pm
1 package Gruntmaster
::Page
::Generic
;
6 our $VERSION = '5999.000_001';
8 use Gruntmaster
::Page
::Base
;
9 use JSON qw
/encode_json decode_json/;
11 use Sub
::Name qw
/subname/;
13 use constant PAGE_SIZE
=> 10;
16 my ($key, $value) = @_;
18 subname
$key => $value if ref $value eq 'CODE';
23 my ($pkg, $id, $title) = @_;
24 my $fn = $pkg =~ s
,::,/,gr
;
25 return if $INC{"$fn.pm"};
27 Gruntmaster
::Page
::Base
->import_to($pkg, $id, $title);
32 my ($thing, $lang, $env) = @_;
35 #debug $env => "Contest is $ct";
36 $thing{makers
} //= sub { db
(shift)->resultset($thing{rsname
}) };
37 my $rs = $thing{makers
}->($env);
38 $rs = $rs->search(undef, {order_by
=> 'me.id'}) unless $rs->is_ordered;
39 if (my $page = $env->{'gruntmaster.page'}) {
40 my $pages = int ($rs->count / PAGE_SIZE
);
41 $pages = 1 if $pages < 1;
42 $page = $pages if $page == -1;
43 @params{'page', 'pages'} = ($page, $pages);
44 $rs = $rs->search(undef, {offset
=> ($page - 1) * PAGE_SIZE
, ($page == $pages ?
() : (rows
=> PAGE_SIZE
))});
46 $rs = $rs->search(undef, {
47 exists $thing{prefetch
} ?
(prefetch
=> $thing{prefetch
}) : (),
48 exists $thing{columns
} ?
(columns
=> $thing{columns
}) : (),
50 my @thing = map +{rs
=> $_, $_->get_columns}, $rs->all;
51 @thing = map { $thing{mangle
}->($env); $_ } @thing if exists $thing{mangle
};
52 @thing = grep { $thing{choose
}->() } @thing if exists $thing{choose
};
53 @thing = sort { $thing{sortby
}->() } @thing if exists $thing{sortby
};
54 $thing{group
} //= sub { $thing{id
} };
56 my $group = $thing{group
}->();
57 $params{$group} //= [];
58 push $params{$group}, $_
60 wantarray ?
%params : \
%params
64 my ($thing, $lang, $env, $id) = @_;
66 debug
$env => "Rsname is $thing{rsname} and id is $id";
67 my %params = map {+ rs
=> $_, $_->get_columns } db
($env)->resultset($thing{rsname
})->find($id);
69 $thing{mangle
}->($env) if exists $thing{mangle
};
70 wantarray ?
%params : \
%params
73 sub headers
($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
77 my $ucid = ucfirst $thing{id
};
78 my $pkg = "Gruntmaster::Page::$ucid";
80 putsym
"${pkg}::_generate", sub { $_[1]->param(list \
%thing, @_[2..$#_]) } if makepkg
$pkg, @thing{qw
/id title/};
81 putsym
"${pkg}::Entry::_generate", sub { $_[1]->param(entry \
%thing, @_[2..$#_]) } if makepkg
"${pkg}::Entry", "$thing{id}_entry", $thing{entry_title
} // '<tmpl_var name>';
82 putsym
"${pkg}::Read::generate", sub { [200, headers
shift, [encode_json list \
%thing, @_]] } if makepkg
"${pkg}::Read";
83 putsym
"${pkg}::Entry::Read::generate", sub { [200, headers
shift, [encode_json entry \
%thing, @_]] } if makepkg
"${pkg}::Entry::Read";
98 local *{"params"} = sub { @thing{qw
/id rsname title entry_title/} = @_ };
99 local *{"choose"} = sub { $thing{choose
} = shift };
100 local *{"sortby"} = sub { $thing{sortby
} = shift };
101 local *{"mangle"} = sub { $thing{mangle
} = shift };
102 local *{"group"} = sub { $thing{group
} = shift };
103 local *{"makers"} = sub { $thing{makers
} = shift };
104 local *{"prefetch"} = sub { $thing{prefetch
} = \
@_ };
105 local *{"columns"} = sub { $thing{columns
} = \
@_ };
112 ##################################################
115 params qw
/us User Users/;
116 choose
{ $_->{name
} =~ /\w/ };
117 sortby
{ lc $a->{name
} cmp lc $b->{name
} };
121 params qw
/pb Problem Problems/;
126 return $db->problems->search({owner
=> $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
127 return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'};
128 $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct
=> 1});
130 sortby
{ $a->{name
} cmp $b->{name
}};
131 group
{ $_->{level
} };
134 $_->{owner_name
} = $_->{rs
}->owner->name;
135 $_->{cansubmit
} = $env->{'gruntmaster.contest'} ?
time < db
($env)->contest($env->{'gruntmaster.contest'})->stop : 1;
137 db
($env)->open->create({
138 contest
=> $env->{'gruntmaster.contest'},
140 owner
=> $env->{REMOTE_USER
},
142 } if $env->{'gruntmaster.contest'} && time >= db
($env)->contest($env->{'gruntmaster.contest'})->start;
147 params qw
/ct Contest Contests/;
149 sortby
{ $b->{start
} <=> $a->{start
} };
150 group
{ time < $_->{start
} ?
'pending' : time > $_->{stop
} ?
'finished' : 'running' };
151 mangle
{ $_->{started
} = time >= $_->{start
}; $_->{owner_name
} = $_->{rs
}->owner->name };
155 params qw
/log Job/, 'Job log', 'Job <tmpl_var id>';
156 prefetch
'owner', 'problem';
160 return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
161 return $db->jobs->search({problem
=> $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'};
162 $db->jobs->search({contest
=> $env->{'gruntmaster.contest'}})
164 sortby
{ $b->{id
} <=> $a->{id
}};
167 $_->{results
} &&= decode_json
$_->{results
};
168 $_->{owner_name
} = $_->{rs
}->owner->name;
169 $_->{problem_name
} = $_->{rs
}->problem->name;
170 $_->{size
} = length $_->{source
};
172 $_->{pageprefix
} = $env->{'gruntmaster.page'} == -1 ?
'page/' : '';
176 putsym
'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' };
177 putsym
'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
178 putsym
'Gruntmaster::Page::Log::max_age', sub { 10 };
This page took 0.063862 seconds and 4 git commands to generate.