]> iEval git - gruntmaster-page.git/blobdiff - lib/Gruntmaster/Page/Generic.pm
Add title to job entries
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
index 37e793105b1de231c1b04ce024e01baebb833318..fe9525dc44a1feb392f30f290adcdd9450d0896f 100644 (file)
@@ -3,22 +3,19 @@ package Gruntmaster::Page::Generic;
 use 5.014000;
 use strict;
 use warnings;
+our $VERSION = '5999.000_001';
 
-use Gruntmaster::Data;
 use Gruntmaster::Page::Base;
-use JSON qw/decode_json/;
-our @ISA = qw/Gruntmaster::Page::Base/;
-our $VERSION = '0.001';
-
-sub hgetall {
-       my $hash = shift;
-       my $cp = $Gruntmaster::Data::contest ? "contest.$Gruntmaster::Data::contest." : '';
-       map { { id => $_, HGETALL "$cp$hash.$_" } } SMEMBERS "$cp$hash"
-}
+use JSON qw/encode_json decode_json/;
+use Plack::Request;
+use Sub::Name qw/subname/;
+
+use constant PAGE_SIZE => 10;
 
 sub putsym {
        my ($key, $value) = @_;
        no strict 'refs';
+       subname $key => $value if ref $value eq 'CODE';
        *{"$key"} = $value;
 }
 
@@ -28,72 +25,77 @@ sub makepkg {
        return if $INC{"$fn.pm"};
        $INC{"$fn.pm"} = 1;
        Gruntmaster::Page::Base->import_to($pkg, $id, $title);
-       putsym "${pkg}::ISA", ['Gruntmaster::Page::Base'];
+       1
 }
 
-sub make_generate {
-       my %thing = @_;
-       sub {
-               my ($self, $htc, $lang, $env, $ct) = @_;
-               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};
-               @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}->();
-                       $params{$group} //= [];
-                       push $params{$group}, $_
-               }
-               $htc->param(%params);
+sub list {
+       my ($thing, $lang, $env) = @_;
+       my %thing = %$thing;
+       #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;
+               $page = $pages if $page == -1;
+               $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};
+       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}->();
+               $params{$group} //= [];
+               push $params{$group}, $_
        }
+       wantarray ? %params : \%params
 }
 
-sub make_entry_generate{
-       my %thing = @_;
-       sub {
-               my ($self, $htc, $lang, $env, $id, $ct) = @_;
-               ($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};
-               %params = (%params, $thing{hook}->(local $_ = \%params)) if exists $thing{hook};
-               $htc->param(%params);
-       }
+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);
+       $thing{mangle}->(local $_ = \%params) if exists $thing{mangle};
+       wantarray ? %params : \%params
 }
 
+sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
+
 sub create_thing {
        my %thing = @_;
        my $ucid = ucfirst $thing{id};
        my $pkg = "Gruntmaster::Page::$ucid";
 
-       putsym "${pkg}::_generate", make_generate %thing if makepkg $pkg, @thing{qw/id title/};
-       putsym "${pkg}::Entry::_generate", make_entry_generate %thing if makepkg "${pkg}::Entry", "$thing{id}_entry", '<tmpl_var name>';
+       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", $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";
 }
 
 sub params;
-sub contest;
+sub makers (&);
 sub choose (&);
 sub sortby (&);
 sub group  (&);
 sub mangle (&);
-sub hook   (&);
+sub prefetch;
 
 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 *{"hook"} = sub { $thing{hook} = shift };
+       local *{"makers"} = sub { $thing{makers} = shift };
+       local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
        use strict 'refs';
 
        shift->();
@@ -103,36 +105,66 @@ 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->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});
+       };
+       sortby { $a->{name} cmp $b->{name}};
        group { $_->{level} };
+       mangle {
+               my $env = shift;
+               $_->{owner_name} = $_->{rs}->owner->name;
+               $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1;
+               eval {
+                       db($env)->open->create({
+                               contest => $_->{contest},
+                               problem => $_->{id},
+                               owner   => $env->{REMOTE_USER},
+                       })
+               } if $_->{contest} && time >= $_->{rs}->contest->start;
+       };
 };
 
 thing {
-       params qw/ct contest Contests/;
-       mangle {
-               $_->{start} = strftime '%c', localtime $_->{start};
-               $_->{end} = strftime '%c', localtime $_->{end};
-       };
-       sortby { $a->{start} <=> $b->{start} };
-       group { time < $_->{start} ? 'pending' : time > $_->{end} ? 'finished' : 'running' };
-       hook { started => time >= $_->{start} };
+       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;
+       params qw/log Job/, 'Job log', 'Job <tmpl_var id>';
+       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 {
                $_->{results} &&= decode_json $_->{results};
-               $_->{time} = sprintf "%.4fs", $_->{time} for values ($_->{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 };
+
 1
This page took 0.028332 seconds and 4 git commands to generate.