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