]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/Generic.pm
Fix opens
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
CommitLineData
fdbf59e5
MG
1package Gruntmaster::Page::Generic;
2
3use 5.014000;
4use strict;
5use warnings;
e046c73a 6our $VERSION = '5999.000_001';
fdbf59e5 7
fdbf59e5 8use Gruntmaster::Page::Base;
5c6aea93 9use JSON qw/encode_json decode_json/;
d3200993
MG
10use Plack::Request;
11use Sub::Name qw/subname/;
fdbf59e5 12
d3200993 13use constant PAGE_SIZE => 10;
fdbf59e5
MG
14
15sub putsym {
16 my ($key, $value) = @_;
17 no strict 'refs';
d3200993 18 subname $key => $value if ref $value eq 'CODE';
fdbf59e5
MG
19 *{"$key"} = $value;
20}
21
22sub makepkg {
23 my ($pkg, $id, $title) = @_;
24 my $fn = $pkg =~ s,::,/,gr;
25 return if $INC{"$fn.pm"};
26 $INC{"$fn.pm"} = 1;
27 Gruntmaster::Page::Base->import_to($pkg, $id, $title);
5c6aea93 28 1
fdbf59e5
MG
29}
30
81cce380 31sub list {
491e82eb 32 my ($thing, $lang, $env) = @_;
81cce380 33 my %thing = %$thing;
58a0ba09 34 my %params;
491e82eb
MG
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'}) {
58a0ba09 40 my $pages = int ($rs->count / PAGE_SIZE);
6a6fea8d 41 $params{default_page} = $page == -1;
d4306a0b 42 $pages = 1 if $pages < 1;
dfc00182 43 $page = $pages if $page == -1;
58a0ba09 44 @params{'page', 'pages'} = ($page, $pages);
d3200993
MG
45 $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
46 }
a46fb222
MG
47 $rs = $rs->search(undef, {
48 exists $thing{prefetch} ? (prefetch => $thing{prefetch}) : (),
49 exists $thing{columns} ? (columns => $thing{columns}) : (),
50 });
d3200993 51 my @thing = map +{rs => $_, $_->get_columns}, $rs->all;
66d79db1 52 @thing = map { $thing{mangle}->($env); $_ } @thing if exists $thing{mangle};
81cce380
MG
53 @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
54 @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
81cce380
MG
55 $thing{group} //= sub { $thing{id} };
56 for (@thing) {
57 my $group = $thing{group}->();
58 $params{$group} //= [];
59 push $params{$group}, $_
fdbf59e5 60 }
81cce380 61 wantarray ? %params : \%params
fdbf59e5
MG
62}
63
81cce380 64sub entry {
491e82eb 65 my ($thing, $lang, $env, $id) = @_;
81cce380 66 my %thing = %$thing;
d3200993 67 debug $env => "Rsname is $thing{rsname} and id is $id";
2e2be9fd 68 my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id);
66d79db1
MG
69 local $_ = \%params;
70 $thing{mangle}->($env) if exists $thing{mangle};
81cce380 71 wantarray ? %params : \%params
fdbf59e5
MG
72}
73
81cce380
MG
74sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
75
fdbf59e5
MG
76sub create_thing {
77 my %thing = @_;
78 my $ucid = ucfirst $thing{id};
79 my $pkg = "Gruntmaster::Page::$ucid";
80
81cce380 81 putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/};
8e0d50d4 82 putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", $thing{entry_title} // '<tmpl_var name>';
81cce380
MG
83 putsym "${pkg}::Read::generate", sub { [200, headers shift, [encode_json list \%thing, @_]] } if makepkg "${pkg}::Read";
84 putsym "${pkg}::Entry::Read::generate", sub { [200, headers shift, [encode_json entry \%thing, @_]] } if makepkg "${pkg}::Entry::Read";
fdbf59e5
MG
85}
86
87sub params;
d3200993 88sub makers (&);
fdbf59e5
MG
89sub choose (&);
90sub sortby (&);
91sub group (&);
92sub mangle (&);
d3200993 93sub prefetch;
a46fb222 94sub columns;
fdbf59e5
MG
95
96sub thing (&){
97 my %thing;
98 no strict 'refs';
8e0d50d4 99 local *{"params"} = sub { @thing{qw/id rsname title entry_title/} = @_ };
fdbf59e5
MG
100 local *{"choose"} = sub { $thing{choose} = shift };
101 local *{"sortby"} = sub { $thing{sortby} = shift };
102 local *{"mangle"} = sub { $thing{mangle} = shift };
103 local *{"group"} = sub { $thing{group} = shift };
d3200993
MG
104 local *{"makers"} = sub { $thing{makers} = shift };
105 local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
a46fb222 106 local *{"columns"} = sub { $thing{columns} = \@_ };
fdbf59e5
MG
107 use strict 'refs';
108
109 shift->();
110 create_thing %thing
111}
112
113##################################################
114
115thing {
d3200993 116 params qw/us User Users/;
fdbf59e5
MG
117 choose { $_->{name} =~ /\w/ };
118 sortby { lc $a->{name} cmp lc $b->{name} };
119};
120
121thing {
d3200993
MG
122 params qw/pb Problem Problems/;
123 prefetch 'owner';
491e82eb
MG
124 makers {
125 my $env = $_[0];
126 my $db = db $env;
491e82eb 127 return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
58a0ba09 128 return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'};
0a329295 129 $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1});
491e82eb 130 };
d3200993 131 sortby { $a->{name} cmp $b->{name}};
fdbf59e5 132 group { $_->{level} };
d3200993
MG
133 mangle {
134 my $env = shift;
135 $_->{owner_name} = $_->{rs}->owner->name;
58a0ba09 136 $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1;
d3200993 137 eval {
ca8b5383 138 db($env)->opens->create({
58a0ba09 139 contest => $env->{'gruntmaster.contest'},
d3200993
MG
140 problem => $_->{id},
141 owner => $env->{REMOTE_USER},
23bde5b8 142 time => time,
d3200993 143 })
58a0ba09 144 } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start;
33c70ecc 145 say "Error when creating open: $@";
d3200993 146 };
fdbf59e5
MG
147};
148
149thing {
d3200993
MG
150 params qw/ct Contest Contests/;
151 prefetch 'owner';
d9f11916 152 sortby { $b->{start} <=> $a->{start} };
d3200993
MG
153 group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' };
154 mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name };
fdbf59e5
MG
155};
156
157thing {
8e0d50d4 158 params qw/log Job/, 'Job log', 'Job <tmpl_var id>';
d3200993 159 prefetch 'owner', 'problem';
491e82eb
MG
160 makers {
161 my $env = $_[0];
162 my $db = db $env;
163 return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
164 return $db->jobs->search({problem => $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'};
165 $db->jobs->search({contest => $env->{'gruntmaster.contest'}})
166 };
d3200993
MG
167 sortby { $b->{id} <=> $a->{id}};
168 mangle {
251f53e4 169 my $env = shift;
d3200993
MG
170 $_->{results} &&= decode_json $_->{results};
171 $_->{owner_name} = $_->{rs}->owner->name;
172 $_->{problem_name} = $_->{rs}->problem->name;
173 $_->{size} = length $_->{source};
174 delete $_->{source};
175 }
fdbf59e5
MG
176};
177
d3200993
MG
178putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' };
179putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
6529f1eb 180putsym 'Gruntmaster::Page::Log::max_age', sub { 10 };
d3200993 181
fdbf59e5 1821
This page took 0.05687 seconds and 4 git commands to generate.