X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FPage%2FGeneric.pm;h=15b887ea03c64d778fbf38738c3c5857d75603cf;hb=600e4c01f0fc265a61aea2830c26ed2bd6d77c73;hp=3fdd47c0a157d7e190ab5c5c6464bf1e5b53787e;hpb=e3ab4b036900e0ae2f0b0a39641b2aed82fce8f5;p=gruntmaster-page.git diff --git a/lib/Gruntmaster/Page/Generic.pm b/lib/Gruntmaster/Page/Generic.pm index 3fdd47c..15b887e 100644 --- a/lib/Gruntmaster/Page/Generic.pm +++ b/lib/Gruntmaster/Page/Generic.pm @@ -3,21 +3,19 @@ package Gruntmaster::Page::Generic; use 5.014000; use strict; use warnings; -our $VERSION = '0.001'; +our $VERSION = '5999.000_001'; -use Gruntmaster::Data; use Gruntmaster::Page::Base; use JSON qw/encode_json decode_json/; +use Plack::Request; +use Sub::Name qw/subname/; -sub hgetall { - my $hash = shift; - my $cp = $Gruntmaster::Data::contest ? "contest.$Gruntmaster::Data::contest." : ''; - map { { id => $_, HGETALL "$cp$hash.$_" } } SMEMBERS "$cp$hash" -} +use constant PAGE_SIZE => 10; sub putsym { my ($key, $value) = @_; no strict 'refs'; + subname $key => $value if ref $value eq 'CODE'; *{"$key"} = $value; } @@ -31,16 +29,29 @@ sub makepkg { } sub list { - my ($thing, $lang, $env, $ct) = @_; + my ($thing, $lang, $env) = @_; my %thing = %$thing; - undef $ct unless $thing{contest}; - debug $env => "Contest is $ct"; - local $Gruntmaster::Data::contest = $ct if $ct; - my @thing = hgetall $thing{hash}; - @thing = map { $thing{mangle}->(); $_ } @thing if exists $thing{mangle}; + 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, { + exists $thing{prefetch} ? (prefetch => $thing{prefetch}) : (), + exists $thing{columns} ? (columns => $thing{columns}) : (), + }); + my @thing = map +{rs => $_, $_->get_columns}, $rs->all; + @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}->(); @@ -51,13 +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 $thing{contest}; - local $Gruntmaster::Data::contest = $ct if $ct; - debug $env => "Hash is $thing{hash} and id is $id"; - my %params = HGETALL "$thing{hash}.$id"; - $thing{mangle}->(local $_ = \%params) if exists $thing{mangle}; + debug $env => "Rsname is $thing{rsname} and id is $id"; + 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 } @@ -69,27 +79,31 @@ 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"; } sub params; -sub contest; +sub makers (&); sub choose (&); sub sortby (&); sub group (&); sub mangle (&); +sub prefetch; +sub columns; sub thing (&){ my %thing; no strict 'refs'; - local *{"params"} = sub { @thing{qw/id hash 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 *{"contest"} = sub { $thing{contest} = 1 }; + local *{"makers"} = sub { $thing{makers} = shift }; + local *{"prefetch"} = sub { $thing{prefetch} = \@_ }; + local *{"columns"} = sub { $thing{columns} = \@_ }; use strict 'refs'; shift->(); @@ -99,30 +113,69 @@ sub thing (&){ ################################################## thing { - params qw/us user Users/; + params qw/us User Users/; choose { $_->{name} =~ /\w/ }; sortby { lc $a->{name} cmp lc $b->{name} }; }; thing { - params qw/pb problem Problems/; - contest; - sortby { $a->{name} cmp $b->{name} }; + params qw/pb Problem Problems/; + prefetch 'owner'; + 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 { $_->{owner_name} = do { local $Gruntmaster::Data::contest; user_name $_->{owner} } } + mangle { + my $env = shift; + $_->{owner_name} = $_->{rs}->owner->name; + $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1; + eval { + db($env)->opens->create({ + contest => $env->{'gruntmaster.contest'}, + problem => $_->{id}, + owner => $env->{REMOTE_USER}, + time => time, + }) + } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start; + }; }; thing { - params qw/ct contest Contests/; - sortby { $a->{start} <=> $b->{start} }; - group { time < $_->{start} ? 'pending' : time > $_->{end} ? 'finished' : 'running' }; - mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = do { local $Gruntmaster::Data::contest; user_name $_->{owner} } }; + params qw/ct Contest Contests/; + prefetch 'owner'; + sortby { $b->{start} <=> $a->{start} }; + group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' }; + mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name }; }; thing { - params qw/log job/, 'Job log'; - contest; - mangle { $_->{results} &&= decode_json $_->{results}; $_->{user_name} = do { local $Gruntmaster::Data::contest; user_name $_->{user} } } + params qw/log Job/, 'Job log', 'Job '; + prefetch 'owner', 'problem'; + 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; + $_->{size} = length $_->{source}; + delete $_->{source}; + } }; +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