X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FPage%2FGeneric.pm;h=9e07d4c8a68814aa11189b8a8f5a30e7d9390864;hb=0a329295bb764112812177386b525ed3aa2b8c6d;hp=256add635bed2a31ed85d929cab7e710722ea57f;hpb=491e82eb192b76e7e8eece5aaf43b7aebb81a12d;p=gruntmaster-page.git diff --git a/lib/Gruntmaster/Page/Generic.pm b/lib/Gruntmaster/Page/Generic.pm index 256add6..9e07d4c 100644 --- a/lib/Gruntmaster/Page/Generic.pm +++ b/lib/Gruntmaster/Page/Generic.pm @@ -31,20 +31,25 @@ sub makepkg { sub list { my ($thing, $lang, $env) = @_; my %thing = %$thing; + 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 = $rs->count / PAGE_SIZE; + my $pages = int ($rs->count / PAGE_SIZE); + $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 = 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}->(); @@ -58,8 +63,7 @@ sub entry { my ($thing, $lang, $env, $id) = @_; my %thing = %$thing; debug $env => "Rsname is $thing{rsname} and id is $id"; - $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) }; - my %params = map {+ rs => $_, $_->get_columns } $thing{makers}->($env)->find($id); + my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id); $thing{mangle}->(local $_ = \%params) if exists $thing{mangle}; wantarray ? %params : \%params } @@ -72,7 +76,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", ''; + putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", $thing{entry_title} // ''; 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"; } @@ -84,17 +88,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->(); @@ -115,23 +121,23 @@ thing { makers { my $env = $_[0]; my $db = db $env; - return $db->contest($env->{'gruntmaster.contest'})->problems->search(undef, {order_by => 'problem.id'}) if exists $env->{'gruntmaster.contest'}; return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'}; - $db->problems->search({private => 0}); + 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}, + contest => $env->{'gruntmaster.contest'}, problem => $_->{id}, owner => $env->{REMOTE_USER}, }) - } if $_->{contest} && time >= $_->{rs}->contest->start; + } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start; }; }; @@ -144,7 +150,7 @@ thing { }; thing { - params qw/log Job/, 'Job log'; + params qw/log Job/, 'Job log', 'Job '; prefetch 'owner', 'problem'; makers { my $env = $_[0];