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