]>
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 MG |
31 | sub list { |
32 | my ($thing, $lang, $env, $ct) = @_; | |
33 | my %thing = %$thing; | |
d3200993 | 34 | my $req = Plack::Request->new($env); |
81cce380 | 35 | debug $env => "Contest is $ct"; |
d3200993 MG |
36 | $thing{makers} //= sub { shift->resultset($thing{rsname}) }; |
37 | my $rs = $thing{makers}->(db $env)->search(undef, {order_by => 'me.id'}); | |
38 | if (my $page = $req->param('page')) { | |
39 | my $pages = $rs->count / PAGE_SIZE; | |
40 | $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))}); | |
41 | } | |
42 | $rs = $rs->search(undef, {prefetch => $thing{prefetch}}) if exists $thing{prefetch}; | |
43 | my @thing = map +{rs => $_, $_->get_columns}, $rs->all; | |
81cce380 MG |
44 | @thing = map { $thing{mangle}->(); $_ } @thing if exists $thing{mangle}; |
45 | @thing = grep { $thing{choose}->() } @thing if exists $thing{choose}; | |
46 | @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby}; | |
47 | my %params; | |
48 | $thing{group} //= sub { $thing{id} }; | |
49 | for (@thing) { | |
50 | my $group = $thing{group}->(); | |
51 | $params{$group} //= []; | |
52 | push $params{$group}, $_ | |
fdbf59e5 | 53 | } |
81cce380 | 54 | wantarray ? %params : \%params |
fdbf59e5 MG |
55 | } |
56 | ||
81cce380 MG |
57 | sub entry { |
58 | my ($thing, $lang, $env, $id, $ct) = @_; | |
59 | my %thing = %$thing; | |
d3200993 MG |
60 | ($id, $ct) = ($ct, $id) if $ct; |
61 | debug $env => "Rsname is $thing{rsname} and id is $id"; | |
62 | $thing{makers} //= sub { shift->resultset($thing{rsname}) }; | |
63 | my %params = map {+ rs => $_, $_->get_columns } $thing{makers}->(db $env)->find($id); | |
64 | $params{contest} = $ct if $ct; | |
81cce380 MG |
65 | $thing{mangle}->(local $_ = \%params) if exists $thing{mangle}; |
66 | wantarray ? %params : \%params | |
fdbf59e5 MG |
67 | } |
68 | ||
81cce380 MG |
69 | sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] } |
70 | ||
fdbf59e5 MG |
71 | sub create_thing { |
72 | my %thing = @_; | |
73 | my $ucid = ucfirst $thing{id}; | |
74 | my $pkg = "Gruntmaster::Page::$ucid"; | |
75 | ||
81cce380 MG |
76 | putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/}; |
77 | putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", '<tmpl_var name>'; | |
78 | putsym "${pkg}::Read::generate", sub { [200, headers shift, [encode_json list \%thing, @_]] } if makepkg "${pkg}::Read"; | |
79 | putsym "${pkg}::Entry::Read::generate", sub { [200, headers shift, [encode_json entry \%thing, @_]] } if makepkg "${pkg}::Entry::Read"; | |
fdbf59e5 MG |
80 | } |
81 | ||
82 | sub params; | |
d3200993 | 83 | sub makers (&); |
fdbf59e5 MG |
84 | sub choose (&); |
85 | sub sortby (&); | |
86 | sub group (&); | |
87 | sub mangle (&); | |
d3200993 | 88 | sub prefetch; |
fdbf59e5 MG |
89 | |
90 | sub thing (&){ | |
91 | my %thing; | |
92 | no strict 'refs'; | |
d3200993 | 93 | local *{"params"} = sub { @thing{qw/id rsname title/} = @_ }; |
fdbf59e5 MG |
94 | local *{"choose"} = sub { $thing{choose} = shift }; |
95 | local *{"sortby"} = sub { $thing{sortby} = shift }; | |
96 | local *{"mangle"} = sub { $thing{mangle} = shift }; | |
97 | local *{"group"} = sub { $thing{group} = shift }; | |
d3200993 MG |
98 | local *{"makers"} = sub { $thing{makers} = shift }; |
99 | local *{"prefetch"} = sub { $thing{prefetch} = \@_ }; | |
fdbf59e5 MG |
100 | use strict 'refs'; |
101 | ||
102 | shift->(); | |
103 | create_thing %thing | |
104 | } | |
105 | ||
106 | ################################################## | |
107 | ||
108 | thing { | |
d3200993 | 109 | params qw/us User Users/; |
fdbf59e5 MG |
110 | choose { $_->{name} =~ /\w/ }; |
111 | sortby { lc $a->{name} cmp lc $b->{name} }; | |
112 | }; | |
113 | ||
114 | thing { | |
d3200993 MG |
115 | params qw/pb Problem Problems/; |
116 | prefetch 'owner'; | |
117 | makers { my ($db, $ct) = @_; $ct ? $db->contest($ct)->problems : $db->problems->search({private => 0}) }; | |
118 | sortby { $a->{name} cmp $b->{name}}; | |
fdbf59e5 | 119 | group { $_->{level} }; |
d3200993 MG |
120 | mangle { |
121 | my $env = shift; | |
122 | $_->{owner_name} = $_->{rs}->owner->name; | |
123 | $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1; | |
124 | eval { | |
125 | db($env)->open->create({ | |
126 | contest => $_->{contest}, | |
127 | problem => $_->{id}, | |
128 | owner => $env->{REMOTE_USER}, | |
129 | }) | |
130 | } if $_->{contest} && time >= $_->{rs}->contest->start; | |
131 | }; | |
fdbf59e5 MG |
132 | }; |
133 | ||
134 | thing { | |
d3200993 MG |
135 | params qw/ct Contest Contests/; |
136 | prefetch 'owner'; | |
d9f11916 | 137 | sortby { $b->{start} <=> $a->{start} }; |
d3200993 MG |
138 | group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' }; |
139 | mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name }; | |
fdbf59e5 MG |
140 | }; |
141 | ||
142 | thing { | |
d3200993 MG |
143 | params qw/log Job/, 'Job log'; |
144 | prefetch 'owner', 'problem'; | |
145 | makers { shift->jobs->search({contest => shift}) }; | |
146 | sortby { $b->{id} <=> $a->{id}}; | |
147 | mangle { | |
148 | $_->{results} &&= decode_json $_->{results}; | |
149 | $_->{owner_name} = $_->{rs}->owner->name; | |
150 | $_->{problem_name} = $_->{rs}->problem->name; | |
151 | $_->{size} = length $_->{source}; | |
152 | delete $_->{source}; | |
153 | } | |
fdbf59e5 MG |
154 | }; |
155 | ||
d3200993 MG |
156 | putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' }; |
157 | putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 }; | |
158 | ||
fdbf59e5 | 159 | 1 |