diff options
author | Matthew Somerville <matthew@mysociety.org> | 2015-12-02 17:33:48 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2015-12-15 17:02:11 +0000 |
commit | 92dfeac83378cd49fbb59591685e5bf849d317e6 (patch) | |
tree | f87175f6539728e319dc5bd027b1b94fd7eaa26b /perllib | |
parent | 64d24b8627879fc68f9f883d6e24a9c7cb72449f (diff) |
Fix cobrand restriction of My/Nearby.
5c79337 simplified a bit too far, as after then a particular cobrand
could in Nearby and My only filter reports to a particular body, not
any other criteria. To fix this, introduce more generic functions in
the default cobrand to allow more flexibility.
Make sure a few tests delete their bodies fully so that new tests
pass when run as part of the suite.
Fixes #1289.
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 35 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/SeeSomething.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UKCouncils.pm | 16 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Comment.pm | 14 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 6 |
8 files changed, 58 insertions, 44 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 9a6c7bded..0fc87c2f6 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -84,7 +84,7 @@ sub index : Path : Args(0) { for ( FixMyStreet::DB::Result::Problem->visible_states() ); $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users; - my $comments = $c->model('DB::Comment')->summary_count( $c->cobrand->body_restriction ); + my $comments = $c->cobrand->updates->summary_count; my %comment_counts = map { $_->state => $_->get_column('state_count') } $comments->all; @@ -171,7 +171,7 @@ sub timeline : Path( 'timeline' ) : Args(0) { push @{$time{$_->whenanswered->epoch}}, { type => 'quesAnswered', date => $_->whenanswered, obj => $_ } if $_->whenanswered; } - my $updates = $c->model('DB::Comment')->timeline( $c->cobrand->body_restriction ); + my $updates = $c->cobrand->updates->timeline; foreach ($updates->all) { push @{$time{$_->created->epoch}}, { type => 'update', date => $_->created, obj => $_} ; @@ -622,9 +622,7 @@ sub reports : Path('reports') { } if (@$query) { - my $updates = $c->model('DB::Comment') - ->to_body($c->cobrand->body_restriction) - ->search( + my $updates = $c->cobrand->updates->search( { -or => $query, }, @@ -967,9 +965,7 @@ sub users: Path('users') : Args(0) { sub update_edit : Path('update_edit') : Args(1) { my ( $self, $c, $id ) = @_; - my $update = $c->model('DB::Comment') - ->to_body($c->cobrand->body_restriction) - ->search({ id => $id })->first; + my $update = $c->cobrand->updates->search({ id => $id })->first; $c->detach( '/page_error_404_not_found' ) unless $update; diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 3c4ce2cf7..81a9d7a93 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -36,6 +36,7 @@ sub my : Path : Args(0) { my $states = $c->stash->{filter_problem_states}; my $params = { state => [ keys %$states ], + user => $c->user->id, }; my $category = $c->get_param('filter_category'); @@ -44,9 +45,7 @@ sub my : Path : Args(0) { $c->stash->{filter_category} = $category; } - my $rs = $c->user->problems - ->to_body($c->cobrand->body_restriction) - ->search( $params, { + my $rs = $c->cobrand->problems->search( $params, { order_by => { -desc => 'confirmed' }, rows => 50 } )->page( $p_page ); @@ -77,7 +76,7 @@ sub my : Path : Args(0) { $c->stash->{updates} = \@updates; $c->stash->{updates_pager} = $rs->pager; - my @categories = $c->user->problems->search( undef, { + my @categories = $c->cobrand->problems->search( { user => $c->user->id }, { columns => [ 'category' ], distinct => 1, order_by => [ 'category' ], diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 48c997047..e7a4dad72 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -47,24 +47,45 @@ sub country { =head1 problems -Returns a ResultSet of Problems, restricted to a subset if we're on a cobrand -that only wants some of the data. +Returns a ResultSet of Problems, potentially restricted to a subset if we're on +a cobrand that only wants some of the data. =cut sub problems { my $self = shift; - return $self->{c}->model('DB::Problem'); + return $self->problems_restriction($self->{c}->model('DB::Problem')); } -=head1 body_restriction +=head1 updates -Return an extra query parameter to restrict reports to those sent to a -particular body. +Returns a ResultSet of Comments, potentially restricted to a subset if we're on +a cobrand that only wants some of the data. =cut -sub body_restriction {} +sub updates { + my $self = shift; + return $self->updates_restriction($self->{c}->model('DB::Comment')); +} + +=head1 problems_restriction/updates_restriction + +Used to restricts reports and updates in a cobrand in a particular way. Do +nothing by default. + +=cut + +sub problems_restriction { + my ($self, $rs) = @_; + return $rs; +} + +sub updates_restriction { + my ($self, $rs) = @_; + return $rs; +} + sub site_key { return 0; } diff --git a/perllib/FixMyStreet/Cobrand/SeeSomething.pm b/perllib/FixMyStreet/Cobrand/SeeSomething.pm index 9ae15fd8d..22750aafa 100644 --- a/perllib/FixMyStreet/Cobrand/SeeSomething.pm +++ b/perllib/FixMyStreet/Cobrand/SeeSomething.pm @@ -10,11 +10,6 @@ sub council_name { return 'See Something Say Something'; } sub council_url { return 'seesomething'; } sub area_types { [ 'MTD' ] } -sub body_restriction { - my $self = shift; - return $self->council_id; -} - sub area_check { my ( $self, $params, $context ) = @_; diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index 0fb8c23d0..b4b91b7dd 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -22,11 +22,6 @@ sub path_to_web_templates { ]; } -sub body_restriction { - my $self = shift; - return $self->council_id; -} - sub site_key { my $self = shift; return $self->council_url; @@ -36,9 +31,14 @@ sub restriction { return { cobrand => shift->moniker }; } -sub problems { - my $self = shift; - return $self->{c}->model('DB::Problem')->to_body($self->council_id); +sub problems_restriction { + my ($self, $rs) = @_; + return $rs->to_body($self->council_id); +} + +sub updates_restriction { + my ($self, $rs) = @_; + return $rs->to_body($self->council_id); } sub base_url { diff --git a/perllib/FixMyStreet/DB/ResultSet/Comment.pm b/perllib/FixMyStreet/DB/ResultSet/Comment.pm index 3059baab1..9f254a018 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Comment.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Comment.pm @@ -5,20 +5,20 @@ use strict; use warnings; sub to_body { - my ($rs, $body_restriction) = @_; - return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $body_restriction); + my ($rs, $bodies) = @_; + return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $bodies, 1); } sub timeline { - my ( $rs, $body_restriction ) = @_; + my ( $rs ) = @_; my $prefetch = $rs->result_source->storage->sql_maker->quote_char ? [ qw/user/ ] : []; - return $rs->to_body($body_restriction)->search( + return $rs->search( { state => 'confirmed', created => { '>=', \"current_timestamp-'7 days'::interval" }, @@ -30,17 +30,13 @@ sub timeline { } sub summary_count { - my ( $rs, $body_restriction ) = @_; + my ( $rs ) = @_; my $params = { group_by => ['me.state'], select => [ 'me.state', { count => 'me.id' } ], as => [qw/state state_count/], }; - if ($body_restriction) { - $rs = $rs->to_body($body_restriction); - $params->{join} = 'problem'; - } return $rs->search(undef, $params); } diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm index a6b00ef7b..9db1c6525 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -4,6 +4,11 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +sub to_body { + my ($rs, $bodies) = @_; + return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $bodies, 1); +} + sub nearby { my ( $rs, $c, $dist, $ids, $limit, $mid_lat, $mid_lon, $interval, $category, $states ) = @_; @@ -21,7 +26,7 @@ sub nearby { if $ids; $params->{category} = $category if $category; - $rs = FixMyStreet::DB::ResultSet::Problem::to_body($rs, $c->cobrand->body_restriction); + $rs = $c->cobrand->problems_restriction($rs); my $attrs = { prefetch => 'problem', diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index f82f0135c..488030928 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -16,13 +16,15 @@ sub set_restriction { } sub to_body { - my ($rs, $bodies) = @_; + my ($rs, $bodies, $join) = @_; return $rs unless $bodies; unless (ref $bodies eq 'ARRAY') { $bodies = [ map { ref $_ ? $_->id : $_ } $bodies ]; } + $join = { join => 'problem' } if $join; $rs = $rs->search( - \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ] + \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ], + $join ); return $rs; } |