From: Marius Gavrilescu Date: Mon, 28 Sep 2015 10:24:03 +0000 (+0300) Subject: Merge branch 'nodbic' into newmc-nodbic X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=5b035519c22363f5ef7134b7e1479aee1f30925f;hp=f48eff9821f4f5f4a2967a779df4874461765be3;p=gruntmaster-page.git Merge branch 'nodbic' into newmc-nodbic --- diff --git a/Makefile.PL b/Makefile.PL index a1698a1..d832309 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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 => { diff --git a/app.psgi b/app.psgi index 837dd60..20c8cb5 100644 --- 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->(@_) } } diff --git a/lib/Plack/App/Gruntmaster.pm b/lib/Plack/App/Gruntmaster.pm index 4872da5..c7c6620 100644 --- a/lib/Plack/App/Gruntmaster.pm +++ b/lib/Plack/App/Gruntmaster.pm @@ -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], []] }, } } diff --git a/lib/Plack/App/Gruntmaster/HTML.pm b/lib/Plack/App/Gruntmaster/HTML.pm index 6799fb1..80ecac4 100644 --- a/lib/Plack/App/Gruntmaster/HTML.pm +++ b/lib/Plack/App/Gruntmaster/HTML.pm @@ -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); } diff --git a/t/mech.t b/t/mech.t index 990de76..10e7248 100644 --- 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'); diff --git a/tmpl/ct.en b/tmpl/ct.en index 52cf518..9169ce0 100644 --- a/tmpl/ct.en +++ b/tmpl/ct.en @@ -1,35 +1,8 @@ -
-

Running contests

- -
NameStart dateStop dateOwner +
NameStart dateStop dateOwnerStatus
Contest name......Owner name +
Contest name......Owner nameRunning
-
- -
-

Pending contests

- - - - - -
NameStart dateStop dateOwner -
Contest name......Owner name -
-
- -
-

Finished contests

- - - - - -
NameStart dateStop dateOwner -
Contest name......Owner name -
-
diff --git a/tmpl/pb.en b/tmpl/pb.en index 0b8827f..b86aca6 100644 --- a/tmpl/pb.en +++ b/tmpl/pb.en @@ -1,14 +1,11 @@ -
-

Beginner

- + -
NameAuthorOwner
NameAuthorOwnerLevel
NameauthorOwner name +
NameauthorOwner nameBeginner
-
diff --git a/tmpl/pb_entry.en b/tmpl/pb_entry.en index 7f04faf..0732636 100644 --- a/tmpl/pb_entry.en +++ b/tmpl/pb_entry.en @@ -15,11 +15,6 @@

Submit solution

-
-The contest has finished.
-To submit solutions to this problem, please visit the problem outside the contest. -
-