]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Generic.pm
209691994107b08e071a1c56d9bcb0bcd1d379b1
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
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 my %params;
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'}) {
40 my $pages = int ($rs->count / PAGE_SIZE);
41 $params{default_page} = $page == -1;
42 $pages = 1 if $pages < 1;
43 $page = $pages if $page == -1;
44 @params{'page', 'pages'} = ($page, $pages);
45 $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
46 }
47 $rs = $rs->search(undef, {
48 exists $thing{prefetch} ? (prefetch => $thing{prefetch}) : (),
49 exists $thing{columns} ? (columns => $thing{columns}) : (),
50 });
51 my @thing = map +{rs => $_, $_->get_columns}, $rs->all;
52 @thing = map { $thing{mangle}->($env); $_ } @thing if exists $thing{mangle};
53 @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
54 @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
55 $thing{group} //= sub { $thing{id} };
56 for (@thing) {
57 my $group = $thing{group}->();
58 $params{$group} //= [];
59 push $params{$group}, $_
60 }
61 wantarray ? %params : \%params
62 }
63
64 sub entry {
65 my ($thing, $lang, $env, $id) = @_;
66 my %thing = %$thing;
67 debug $env => "Rsname is $thing{rsname} and id is $id";
68 my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id);
69 local $_ = \%params;
70 $thing{mangle}->($env) if exists $thing{mangle};
71 wantarray ? %params : \%params
72 }
73
74 sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
75
76 sub create_thing {
77 my %thing = @_;
78 my $ucid = ucfirst $thing{id};
79 my $pkg = "Gruntmaster::Page::$ucid";
80
81 putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/};
82 putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", $thing{entry_title} // '<tmpl_var name>';
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";
85 }
86
87 sub params;
88 sub makers (&);
89 sub choose (&);
90 sub sortby (&);
91 sub group (&);
92 sub mangle (&);
93 sub prefetch;
94 sub columns;
95
96 sub thing (&){
97 my %thing;
98 no strict 'refs';
99 local *{"params"} = sub { @thing{qw/id rsname title entry_title/} = @_ };
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 };
104 local *{"makers"} = sub { $thing{makers} = shift };
105 local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
106 local *{"columns"} = sub { $thing{columns} = \@_ };
107 use strict 'refs';
108
109 shift->();
110 create_thing %thing
111 }
112
113 ##################################################
114
115 thing {
116 params qw/us User Users/;
117 choose { $_->{name} =~ /\w/ };
118 sortby { lc $a->{name} cmp lc $b->{name} };
119 };
120
121 thing {
122 params qw/pb Problem Problems/;
123 prefetch 'owner';
124 makers {
125 my $env = $_[0];
126 my $db = db $env;
127 return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
128 return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'};
129 $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1});
130 };
131 sortby { $a->{name} cmp $b->{name}};
132 group { $_->{level} };
133 mangle {
134 my $env = shift;
135 $_->{owner_name} = $_->{rs}->owner->name;
136 $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1;
137 eval {
138 db($env)->open->create({
139 contest => $env->{'gruntmaster.contest'},
140 problem => $_->{id},
141 owner => $env->{REMOTE_USER},
142 })
143 } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start;
144 };
145 };
146
147 thing {
148 params qw/ct Contest Contests/;
149 prefetch 'owner';
150 sortby { $b->{start} <=> $a->{start} };
151 group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' };
152 mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name };
153 };
154
155 thing {
156 params qw/log Job/, 'Job log', 'Job <tmpl_var id>';
157 prefetch 'owner', 'problem';
158 makers {
159 my $env = $_[0];
160 my $db = db $env;
161 return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
162 return $db->jobs->search({problem => $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'};
163 $db->jobs->search({contest => $env->{'gruntmaster.contest'}})
164 };
165 sortby { $b->{id} <=> $a->{id}};
166 mangle {
167 my $env = shift;
168 $_->{results} &&= decode_json $_->{results};
169 $_->{owner_name} = $_->{rs}->owner->name;
170 $_->{problem_name} = $_->{rs}->problem->name;
171 $_->{size} = length $_->{source};
172 delete $_->{source};
173 }
174 };
175
176 putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' };
177 putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
178 putsym 'Gruntmaster::Page::Log::max_age', sub { 10 };
179
180 1
This page took 0.062058 seconds and 3 git commands to generate.