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