Merge branch 'nodbic' into newmc-nodbic newmc-nodbic
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 28 Sep 2015 10:24:03 +0000 (13:24 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 28 Sep 2015 10:24:03 +0000 (13:24 +0300)
Makefile.PL
app.psgi
lib/Plack/App/Gruntmaster.pm
lib/Plack/App/Gruntmaster/HTML.pm
t/mech.t
tmpl/ct.en
tmpl/pb.en
tmpl/pb_entry.en

index a1698a17c2b3b70366fc737fd16f8e10a86a4689..d83230927ebff8d03c23bdb53d2c66ab45b65932 100644 (file)
@@ -16,7 +16,6 @@ WriteMakefile(
                qw/CSS::Minifier::XS          0
                   SVG::SpriteMaker           0
                   File::Slurp                0
-                  Test::MockTime             0
                   Test::More                 0
                   Test::WWW::Mechanize::PSGI 0/,
        },
@@ -43,6 +42,7 @@ WriteMakefile(
                   Plack::Middleware::Auth::Complex 0
                   Plack::Util                      0
                   Scope::Upper                     0
+                  Sort::ByExample                  0
                   Web::Simple                      0.019/,
        },
        META_MERGE        => {
index 837dd60ab51e100408d84934eb6d7fd80a4a724c..20c8cb56f57a73f3c17f44ce03535e6aff9267bb 100644 (file)
--- a/app.psgi
+++ b/app.psgi
@@ -23,15 +23,11 @@ CSP
        $csp =~ s/\n/; /gr;
 }
 
-my $db;
-
 sub add_database {
        my $app = $_[0];
        sub {
-               my ($env) = @_;
-               $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
-               $env->{'gruntmaster.dbic'} = $db;
-               $app->($env)
+               dbinit $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:' unless db;
+               $app->(@_)
        }
 }
 
index 4872da563fd8506f262c248cf21fa41a88c1a25f..c7c6620ed9f52bfe4952b15ebad4d029cbba8a15 100644 (file)
@@ -41,19 +41,16 @@ use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain
 
 my ($env, $privacy);
 
-sub db { $env->{'gruntmaster.dbic'} }
-
 sub remote_user {
-       my $user = $env->{REMOTE_USER};
-       $user &&= db->user($user);
-       $user
+       unless ($env->{'gruntmaster.user'}) {
+               my $user = $env->{REMOTE_USER};
+               $user &&= user_entry $user;
+               $env->{'gruntmaster.user'} = $user;
+       }
+       $env->{'gruntmaster.user'}
 }
 
-sub admin   { remote_user && remote_user->admin }
-sub contest { db->contest ($_{contest}) }
-sub problem { db->problem ($_{problem}) }
-sub job     { db->job     ($_{job})     }
-sub user    { db->user    ($_{user})    }
+sub admin   { remote_user && remote_user->{admin} }
 
 sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
 sub reply    { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
@@ -83,14 +80,16 @@ sub dispatch_request{
                sub (/robots.txt) { NOT_FOUND },
 
                sub (/src/:job) {
-                       return NOT_FOUND if !job;
-                       my $isowner = remote_user && remote_user->id eq job->rawowner;
-                       my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
+                       my $job = db->select(jobs => '*', {id => $_{job}})->hash;
+                       return NOT_FOUND if !$job;
+                       my $isowner = remote_user && remote_user->{id} eq $job->{owner};
+                       my $contest = $job->{contest} && contest_entry $job->{contest};
+                       my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
                        forbid !$isowner && $private;
                        my $privacy = $private ? 'private' : 'public';
                        my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
                        push @headers, (Vary => 'Authorization') if $private;
-                       [200, \@headers, [job->source]]
+                       [200, \@headers, [$job->{source}]]
                },
 
                sub (?:format~) {
@@ -107,19 +106,15 @@ sub dispatch_request{
                },
 
                sub (/st/:contest) {
-                       response st => 'Standings', {
-                               st => [ contest->standings ],
-                               problems => [
-                                       map { [$_->id, $_->name] }
-                                       sort { $a->value <=> $b->value }
-                                       map { $_->problem } contest->contest_problems],
-                       }, 10
+                       my @pb = map { [$_->{id}, $_->{name}] } @{problem_list contest => $_{contest}};
+                       response st => 'Standings', {problems => \@pb, st => standings $_{contest}}, 10
                },
 
                sub (/ed/:contest) {
-                       forbid !contest->is_finished;
-                       my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
-                       response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : ();
+                       my $contest = db->select(contests => '*', {id => $_{contest}})->hash;
+                       forbid time < $contest->{stop};
+                       my $pblist = problem_list contest => $_{contest}, solution => 1;
+                       response ed => 'Editorial of ' . $contest->{name}, {pb => $pblist, editorial => $contest->{editorial}}
                },
 
                sub (/login) {
@@ -129,39 +124,51 @@ sub dispatch_request{
 
                sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
 
-               sub (/us/)                                        { response us => 'Users', {us => db->user_list} },
-               sub (/ct/  + ?:owner~)                            { response ct => 'Contests', db->contest_list(%_), 300 },
+               sub (/us/)                                        { response us => 'Users', {us => user_list} },
+               sub (/ct/  + ?:owner~)                            { response ct => 'Contests', {ct => contest_list(%_)}, 300 },
                sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
                        forbid $_{private};
-                       response log => 'Job list', db->job_list(%_), 5
+                       my ($jobs, $pageinfo) = job_list(%_);
+                       response log => 'Job log', {log => $jobs, %$pageinfo}, 5
                },
                sub (/pb/  + ?:owner~&:contest~&:private~)                  {
                        forbid $_{private};
-                       forbid contest && contest->is_pending;
-                       response pb => 'Problems', db->problem_list(%_)
+                       my $pending = $_{contest} && !contest_entry($_{contest})->{started};
+                       forbid $pending;
+                       response pb => 'Problems', {pb => problem_list %_}
                },
 
-               sub (/us/:user)    { response us_entry => user->name, db->user_entry($_{user}) },
-               sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 },
+               sub (/us/:user)    {
+                       my $user = user_entry $_{user};
+                       response us_entry => $user->{name}, $user
+               },
+               sub (/ct/:contest) {
+                       my $contest = contest_entry $_{contest};
+                       response ct_entry => $contest->{name}, $contest, 60
+               },
                sub (/log/:job)    {
-                       forbid job->private;
-                       response log_entry => "Job  $_{job}", db->job_entry($_{job}), 10
+                       my $job = job_entry $_{job};
+                       forbid $job->{private};
+                       response log_entry => "Job  $_{job}", $job, 10
                },
                sub (/pb/:problem + ?contest~) {
                        my (undef, undef, $contest) = @_;
                        $_{contest} = $contest;
-                       return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
-                       forbid problem->private && !contest;
-                       if (contest) {
-                               return redirect "/pb/$_{problem}" unless contest->is_running;
+                       $contest = $contest && contest_entry $_{contest};
+                       return NOT_FOUND if $contest && !contest_has_problem $_{contest}, $_{problem};
+                       my $problem = problem_entry $_{problem}, $_{contest};
+                       forbid $problem->{private} && !$contest;
+                       if ($contest) {
+                               return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished};
                                forbid !remote_user;
                                $privacy = 'private';
                        }
-                       response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : ();
+                       response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : ();
                },
                sub (/sol/:problem) {
-                       forbid problem->private;
-                       response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
+                       my $problem = problem_entry $_{problem};
+                       forbid $problem->{private};
+                       response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}};
                },
 
                sub (/) { redispatch_to '/index' },
@@ -173,18 +180,22 @@ sub dispatch_request{
                sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
                        my (undef, undef, $prog) = @_;
                        forbid !remote_user;
-                       my $private = (problem->private && !contest) ? 1 : 0;
-                       return reply 'This contest has finished' if contest && contest->is_finished;
-                       return reply 'This contest has not yet started' if contest && contest->is_pending;
-                       return reply 'This problem is private' if !admin && $private;
-                       return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
+                       my $problem = problem_entry $_{problem};
+                       my $private = $problem->{private} ? 1 : 0;
+                       if ($_{contest}) {
+                               $private = 0;
+                               my $contest = contest_entry $_{contest};
+                               return reply 'This contest has not yet started' if !$contest->{started};
+                               return reply 'This contest has finished' if $contest->{finished};
+                               return reply 'This problem is private' if !admin && $private;
+                               return reply 'This problem does not belong to this contest' unless contest_has_problem $_{contest}, $_{problem};
+                       }
                        return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
-                       return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
-                       remote_user->update({lastjob => time});
+                       return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
 
                        my $source = $prog ? read_file $prog->path : $_{source_code};
                        unlink $prog->path if $prog;
-                       my $newjob = db->jobs->create({
+                       my $id = create_job(
                                maybe contest => $_{contest},
                                private => $private,
                                date => time,
@@ -192,10 +203,10 @@ sub dispatch_request{
                                format => $_{prog_format},
                                problem => $_{problem},
                                source => $source,
-                               owner => remote_user->id,
-                       });
+                               owner => remote_user->{id},
+                       );
 
-                       [303, [Location => '/log/' . $newjob->id], []]
+                       [303, [Location => '/log/' . $id], []]
                },
        }
 }
index 6799fb107af2e2608a753798e64c1b7fd48b382b..80ecac4c9f627c25f628472c23750ac0fea01490 100644 (file)
@@ -8,13 +8,16 @@ use HTML::Element::Library;
 use HTML::TreeBuilder;
 use POSIX qw//;
 use Data::Dumper qw/Dumper/;
+use Sort::ByExample
+  sorter => {-as => 'pb_sort', example => [qw/beginner easy medium hard/], xform => sub {$_->{level}}},
+  sorter => {-as => 'ct_sort', example => [qw/Running Pending Finished/], xform => sub {$_->{status}}};
 
 my $optional_end_tags = {%HTML::Tagset::optionalEndTag, tr => 1, td => 1, th => 1};
 
 sub ftime ($)   { POSIX::strftime '%c', localtime shift }
 sub literal ($) {
        my ($html) = @_;
-       return unless $html;
+       return '' unless $html;
        my $b = HTML::TreeBuilder->new;
        $b->ignore_unknown(0);
        $b->parse($html);
@@ -46,7 +49,7 @@ sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) }
 
 sub HTML::Element::namedlink {
        my ($self, $id, $name) = @_;
-       $name = $id unless $name =~ /[[:graph:]]/;
+       $name = $id unless $name && $name =~ /[[:graph:]]/;
        $self = $self->find('a');
        $self->edit_href(sub {s/id/$id/});
        $self->replace_content($name);
@@ -161,7 +164,8 @@ sub process_ct {
                $tr->fclass('name')->namedlink($data->{id}, $data->{name});
                $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name});
        };
-       $args{$_} ? $tree->fid($_)->find('tbody')->find('tr')->iter3($args{$_}, $iter) : $tree->fid($_)->detach for qw/running pending finished/;
+       $_->{status} = $_->{finished} ? 'Finished' : $_->{started} ? 'Running' : 'Pending' for @{$args{ct}};
+       $tree->find('tbody')->find('tr')->iter3([ct_sort @{$args{ct}}], $iter);
 }
 
 sub process_pb_entry {
@@ -190,16 +194,11 @@ sub process_pb_entry {
                $_->detach for $tree->fclass('rc'); # requires contest
                $tree->fid('solution_modal')->replace_content(literal $args{solution});
        }
-       if ($args{cansubmit}) {
-               $tree->fid('nosubmit')->detach;
-               $tree->look_down(name => 'problem')->attr(value => $args{id});
-               my $contest = $tree->look_down(name => 'contest');
-               $contest->attr(value => $args{args}{contest}) if $args{args}{contest};
-               $contest->detach unless $args{args}{contest}
-       } else {
-               $tree->fid('nosubmit')->find('a')->edit_href(sub{s/id/$args{id}/});
-               $tree->fid('submit')->detach
-       }
+
+       $tree->look_down(name => 'problem')->attr(value => $args{id});
+       my $contest = $tree->look_down(name => 'contest');
+       $contest->attr(value => $args{args}{contest}) if $args{args}{contest};
+       $contest->detach unless $args{args}{contest}
 }
 
 sub process_sol {
@@ -209,21 +208,17 @@ sub process_sol {
 
 sub process_pb {
        my ($tree, %args) = @_;
-       my $titer = sub {
+       my $iter = sub {
                my ($data, $tr) = @_;
                $tr->set_child_content(class => 'author', $data->{author});
+               $tr->set_child_content(class => 'level', ucfirst $data->{level});
                $tr->fclass('name')->namedlink($data->{id}, $data->{name});
                $tr->fclass('name')->find('a')->edit_href(sub {$_ .= "?contest=$args{contest}"}) if $args{contest};
                $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name});
                $tr->find('td')->attr(class => $tr->find('td')->attr('class').' warning') if $data->{private} && !$args{contest};
        };
-       my $iter = sub {
-               my ($data, $div) = @_;
-               $div->attr(id => $data);
-               $div->find('h2')->replace_content(ucfirst $data);
-               $div->find('tbody')->find('tr')->iter3($args{$data}, $titer);
-       };
-       $tree->fid('beginner')->iter3([grep {$args{$_}} qw/beginner easy medium hard/], $iter);
+
+       $tree->find('tbody')->find('tr')->iter3([pb_sort @{$args{pb}}], $iter);
        $tree->fid('open-alert')->detach unless $args{contest};
 }
 
@@ -312,7 +307,7 @@ sub process_ed {
                $div->set_child_content(class => 'solution', literal $data->{solution});
                $div->fclass('problem')->namedlink($data->{id}, $data->{name});
        };
-       my @pb = map { @{$args{$_} // []} } qw/beginner easy medium hard/;
+       my @pb = sort { $a->{value} <=> $b->{value} } @{$args{pb}};
        $tree->fclass('well')->iter3(\@pb, $iter);
 }
 
index 990de7659332b1909e87bbc8b7614c6ea4a2d608..10e7248567a2328076c6ba8ad2b4ef506cdb2db4 100644 (file)
--- a/t/mech.t
+++ b/t/mech.t
@@ -2,77 +2,55 @@
 use strict;
 use warnings;
 
-use Test::MockTime ':all';
-use Test::More tests => 14;
+use Test::More;
 use Test::WWW::Mechanize::PSGI;
 
 use Gruntmaster::Data;
 use JSON::MaybeXS qw/decode_json/;
 use Log::Log4perl qw/:easy/;
 
-set_fixed_time 25;
+BEGIN {
+       eval {
+               dbinit 'dbi:Pg:dbname=gmtest'; 1;
+       } or plan skip_all => 'Cannot connect to test database (gmtest).'. "Error: $@";
+       plan tests => 22;
+}
 
+Log::Log4perl->easy_init($OFF);
 my $mech = Test::WWW::Mechanize::PSGI->new(app => do 'app.psgi');
-my $result;
-my $content;
 
-Log::Log4perl->easy_init($OFF);
+$mech->get_ok('/');
+$mech->title_is('Gruntmaster 6000', 'title');
 
-sub get {
-       my ($url, $expect_fail) = @_;
-       my $param = $url =~ /[?]/ ? '&format=json' : '?format=json';
-       $expect_fail ? $mech->get("$url$param") : $mech->get_ok("$url$param");
-       $result = $mech->response->code;
-       $content = $result == 200 ? decode_json $mech->content(decoded_by_headers => 1) : '';
-}
+$mech->get_ok('/pb/');
+$mech->title_is('Problems', 'title');
+
+$mech->get_ok('/pb/arc');
+$mech->title_is('Problem in archive', 'title');
+
+$mech->get_ok('/ct/');
+$mech->title_is('Contests', 'title');
+
+$mech->get_ok('/ct/fc');
+$mech->title_is('Finished contest');
+
+$mech->get_ok('/log/');
+$mech->title_is('Job log', 'title');
+
+$mech->get_ok('/log/1');
+$mech->title_is('Job 1', 'title');
+
+$mech->get_ok('/us/');
+$mech->title_is('Users', 'title');
+
+$mech->get_ok('/us/MGV');
+# testdata.sql does not set name for users, therefore not checking title
+#$mech->title_is('Marius Gavrilescu', 'title');
+
+$mech->get_ok('/src/1.c');
+
+$mech->get_ok('/st/fc');
+$mech->title_is('Standings', 'title');
 
-our $db = Gruntmaster::Data->connect('dbi:SQLite:dbname=:memory:');
-$db->deploy;
-
-$db->users->create({id => 'MGV', admin => 1});
-$db->contests->create({id => 'fc', start => 10, stop => 20, name => 'Finished contest', owner => 'MGV'});
-$db->contests->create({id => 'rc', start => 20, stop => 30, name => 'Running contest', owner => 'MGV'});
-$db->contests->create({id => 'pc', start => 30, stop => 40, name => 'Pending contest', owner => 'MGV'});
-
-my @pbjunk = (
-       name      => 'Problem',
-       generator => 'Undef',
-       runner    => 'File',
-       judge     => 'Absolute',
-       level     => 'beginner',
-       value     => 100,
-       owner     => 'MGV',
-       statement => '...',
-       testcnt   => 1,
-       timeout   => 1
-);
-
-$db->problems->create({id => 'fca', private => 0, @pbjunk});
-$db->problems->create({id => 'rca', private => 0, @pbjunk});
-$db->problems->create({id => 'pca', private => 0, @pbjunk});
-$db->problems->create({id => 'arc', private => 0, @pbjunk});
-$db->problems->create({id => 'prv', private => 1, @pbjunk});
-$db->contest_problems->create({problem => "${_}a", contest => $_}) for qw/fc rc pc/;
-
-sub problem_list () { [sort map {$_->{id}} @{$content->{beginner}}] }
-get '/pb/';
-is_deeply problem_list, [qw/arc fca/], '/pb/ has correct problems';
-get '/pb/?contest=pc', 1;
-is $result, 401, '/pb/?contest=pc returns 401';
-get '/pb/?contest=rc';
-is_deeply problem_list, [qw/rca/], '/pb/?contest=rc has correct problems';
-get '/pb/?contest=fc';
-is_deeply problem_list, [qw/fca/], '/pb/?contest=fc has correct problems';
-
-get '/us/';
-my ($mgv) = @{$content->{us}};
-is $mgv->{id}, 'MGV', 'first (and only) user is MGV';
-is $mgv->{admin}, 1, 'MGV is an admin';
-
-get '/ct/';
-my $pc = $content->{pending}->[0];
-my $rc = $content->{running}->[0];
-my $fc = $content->{finished}->[0];
-is $pc->{id}, 'pc', 'pc is pending';
-is $rc->{id}, 'rc', 'rc is running';
-is $fc->{id}, 'fc', 'fc is running';
+$mech->get_ok('/ed/fc');
+$mech->title_is('Editorial of Finished contest', 'title');
index 52cf518929092d6a129306909519ce2e3f1c20dd..9169ce078b5e3df15cc309122558f0de9f256ab8 100644 (file)
@@ -1,35 +1,8 @@
-<div id="running">
-<h1>Running contests</h1>
 <table class="table table-striped">
 <thead>
-<tr><th>Name<th>Start date<th>Stop date<th>Owner
+<tr><th>Name<th>Start date<th>Stop date<th>Owner<th>Status
 </thead>
 
 <tbody>
-<tr><td class="name"><a href="/ct/id">Contest name</a><td class="start">...<td class="stop">...<td class="owner"><a href="/us/id">Owner name</a>
+<tr><td class="name"><a href="/ct/id">Contest name</a><td class="start">...<td class="stop">...<td class="owner"><a href="/us/id">Owner name</a><td class="status">Running
 </table>
-</div>
-
-<div id="pending">
-<h1>Pending contests</h1>
-<table class="table table-striped">
-<thead>
-<tr><th>Name<th>Start date<th>Stop date<th>Owner
-</thead>
-
-<tbody>
-<tr><td class="name"><a href="/ct/id">Contest name</a><td class="start">...<td class="stop">...<td class="owner"><a href="/us/id">Owner name</a>
-</table>
-</div>
-
-<div id="finished">
-<h1>Finished contests</h1>
-<table class="table table-striped">
-<thead>
-<tr><th>Name<th>Start date<th>Stop date<th>Owner
-</thead>
-
-<tbody>
-<tr><td class="name"><a href="/ct/id">Contest name</a><td class="start">...<td class="stop">...<td class="owner"><a href="/us/id">Owner name</a>
-</table>
-</div>
index 0b8827f234abe84756d93fcb552f9f47a3ffda54..b86aca6601b757af52e80a6a3c40aa16b9443b3b 100644 (file)
@@ -1,14 +1,11 @@
 <div id="open-alert" class="alert alert-warning" role="alert">
-<h4>Friendly reminder</h4>
+<h3>Friendly reminder</h3>
 The timer for a problem starts when you open the problem. You will lose points if you open all problems at the same time.
 </div>
 
-<div id="beginner">
-<h2>Beginner</h2>
 <table class="table table-striped table-fixed">
-<thead><tr><th>Name<th>Author<th>Owner</thead>
+<thead><tr><th>Name<th>Author<th>Owner<th>Level</thead>
 
 <tbody>
-<tr><td class="name"><a href="id">Name</a><td class="author">author<td class="owner"><a href="/us/id">Owner name</a>
+<tr><td class="name"><a href="id">Name</a><td class="author">author<td class="owner"><a href="/us/id">Owner name</a><td class="level">Beginner
 </table>
-</div>
index 7f04fafe8a21e0b3933a67bb0b9f8bad9102dc20..0732636929a70b9b5a03fcd650d68423a28dfa81 100644 (file)
 
 <h1>Submit solution</h1>
 
-<div id="nosubmit">
-The contest has finished.<br>
-To submit solutions to this problem, please visit the problem <a href="/pb/id">outside&nbsp;the&nbsp;contest</a>.
-</div>
-
 <div id="submit">
 <form id="submitform" action="/action/submit" method="POST" enctype="multipart/form-data" role="form">
 <input type="hidden" name="problem" value="problem_id">
This page took 0.026364 seconds and 4 git commands to generate.