]> iEval git - gruntmaster-page.git/blobdiff - lib/Gruntmaster/Page/Generic.pm
Import some changes from the mindcoding branch
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
index 2cb82d91d86720a5e79e3ff3573a72e83f34e9d0..15b887ea03c64d778fbf38738c3c5857d75603cf 100644 (file)
@@ -29,22 +29,29 @@ sub makepkg {
 }
 
 sub list {
-       my ($thing, $lang, $env, $ct) = @_;
+       my ($thing, $lang, $env) = @_;
        my %thing = %$thing;
-       my $req = Plack::Request->new($env);
-       debug $env => "Contest is $ct";
-       $thing{makers} //= sub { shift->resultset($thing{rsname}) };
-       my $rs = $thing{makers}->(db $env)->search(undef, {order_by => 'me.id'});
-       if (my $page = $req->param('page')) {
-               my $pages = $rs->count / PAGE_SIZE;
+       my %params;
+       #debug $env => "Contest is $ct";
+       $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) };
+       my $rs = $thing{makers}->($env);
+       $rs = $rs->search(undef, {order_by => 'me.id'}) unless $rs->is_ordered;
+       if (my $page = $env->{'gruntmaster.page'}) {
+               my $pages = int ($rs->count / PAGE_SIZE);
+               $params{default_page} = $page == -1;
+               $pages = 1 if $pages < 1;
+               $page = $pages if $page == -1;
+               @params{'page', 'pages'} = ($page, $pages);
                $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
        }
-       $rs = $rs->search(undef, {prefetch => $thing{prefetch}}) if exists $thing{prefetch};
+       $rs = $rs->search(undef, {
+               exists $thing{prefetch} ? (prefetch => $thing{prefetch}) : (),
+               exists $thing{columns} ? (columns => $thing{columns}) : (),
+       });
        my @thing = map +{rs => $_, $_->get_columns}, $rs->all;
-       @thing = map  { $thing{mangle}->(); $_ } @thing if exists $thing{mangle};
+       @thing = map  { $thing{mangle}->($env); $_ } @thing if exists $thing{mangle};
        @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
        @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
-       my %params;
        $thing{group} //= sub { $thing{id} };
        for (@thing) {
                my $group = $thing{group}->();
@@ -55,14 +62,12 @@ sub list {
 }
 
 sub entry {
-       my ($thing, $lang, $env, $id, $ct) = @_;
+       my ($thing, $lang, $env, $id) = @_;
        my %thing = %$thing;
-       ($id, $ct) = ($ct, $id) if $ct;
        debug $env => "Rsname is $thing{rsname} and id is $id";
-       $thing{makers} //= sub { shift->resultset($thing{rsname}) };
-       my %params = map {+ rs => $_, $_->get_columns } $thing{makers}->(db $env)->find($id);
-       $params{contest} = $ct if  $ct;
-       $thing{mangle}->(local $_ = \%params) if exists $thing{mangle};
+       my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id);
+       local $_ = \%params;
+       $thing{mangle}->($env) if exists $thing{mangle};
        wantarray ? %params : \%params
 }
 
@@ -74,7 +79,7 @@ sub create_thing {
        my $pkg = "Gruntmaster::Page::$ucid";
 
        putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/};
-       putsym "${pkg}::Entry::_generate",  sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", '<tmpl_var name>';
+       putsym "${pkg}::Entry::_generate",  sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", $thing{entry_title} // '<tmpl_var name>';
        putsym "${pkg}::Read::generate", sub { [200, headers shift, [encode_json list \%thing, @_]] } if makepkg "${pkg}::Read";
        putsym "${pkg}::Entry::Read::generate", sub { [200, headers shift, [encode_json entry \%thing, @_]] } if makepkg "${pkg}::Entry::Read";
 }
@@ -86,17 +91,19 @@ sub sortby (&);
 sub group  (&);
 sub mangle (&);
 sub prefetch;
+sub columns;
 
 sub thing (&){
        my %thing;
        no strict 'refs';
-       local *{"params"} = sub { @thing{qw/id rsname title/} = @_ };
+       local *{"params"} = sub { @thing{qw/id rsname title entry_title/} = @_ };
        local *{"choose"} = sub { $thing{choose} = shift };
        local *{"sortby"} = sub { $thing{sortby} = shift };
        local *{"mangle"} = sub { $thing{mangle} = shift };
        local *{"group"} = sub { $thing{group} = shift };
        local *{"makers"} = sub { $thing{makers} = shift };
        local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
+       local *{"columns"} = sub { $thing{columns} = \@_ };
        use strict 'refs';
 
        shift->();
@@ -114,20 +121,27 @@ thing {
 thing {
        params qw/pb Problem Problems/;
        prefetch 'owner';
-       makers { my ($db, $ct) = @_; $ct ? $db->contest($ct)->problems : $db->problems->search({private => 0}) };
+       makers {
+               my $env = $_[0];
+               my $db = db $env;
+               return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
+               return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'};
+               $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1});
+       };
        sortby { $a->{name} cmp $b->{name}};
        group { $_->{level} };
        mangle {
                my $env = shift;
                $_->{owner_name} = $_->{rs}->owner->name;
-               $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1;
+               $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1;
                eval {
-                       db($env)->open->create({
-                               contest => $_->{contest},
+                       db($env)->opens->create({
+                               contest => $env->{'gruntmaster.contest'},
                                problem => $_->{id},
                                owner   => $env->{REMOTE_USER},
+                               time    => time,
                        })
-               } if $_->{contest} && time >= $_->{rs}->contest->start;
+               } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start;
        };
 };
 
@@ -140,11 +154,18 @@ thing {
 };
 
 thing {
-       params qw/log Job/, 'Job log';
+       params qw/log Job/, 'Job log', 'Job <tmpl_var id>';
        prefetch 'owner', 'problem';
-       makers { shift->jobs->search({contest => shift}) };
+       makers {
+               my $env = $_[0];
+               my $db = db $env;
+               return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
+               return $db->jobs->search({problem => $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'};
+               $db->jobs->search({contest => $env->{'gruntmaster.contest'}})
+       };
        sortby { $b->{id} <=> $a->{id}};
        mangle {
+               my $env = shift;
                $_->{results} &&= decode_json $_->{results};
                $_->{owner_name} = $_->{rs}->owner->name;
                $_->{problem_name} = $_->{rs}->problem->name;
@@ -155,5 +176,6 @@ thing {
 
 putsym 'Gruntmaster::Page::Pb::Entry::vary',    sub { 'Authorization' };
 putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
+putsym 'Gruntmaster::Page::Log::max_age', sub { 10 };
 
 1
This page took 0.025181 seconds and 4 git commands to generate.