diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-06-13 13:53:55 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-06-13 16:20:20 +0100 |
commit | f36c0293cfb0fef048e7f48fbf59ab2bf6d66e02 (patch) | |
tree | e2f7cdee0d465665bfa6c2d11d595af5d2b21584 | |
parent | 9525f5765dd3621673ba688fda195de1963455df (diff) |
Catch errors whilst quote_char is set.
That way we can make sure we always reset quote_char even if something breaks.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 59 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 178 |
2 files changed, 124 insertions, 113 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 95969016f..9d6c7d922 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -13,6 +13,7 @@ use List::Util 'first'; use List::MoreUtils 'uniq'; use mySociety::ArrayUtils; use Text::CSV; +use Try::Tiny; use FixMyStreet::SendReport; use FixMyStreet::SMS; @@ -142,43 +143,47 @@ sub timeline : Path( 'timeline' ) : Args(0) { my %time; - $c->model('DB')->schema->storage->sql_maker->quote_char( '"' ); - $c->model('DB')->schema->storage->sql_maker->name_sep( '.' ); + try { + $c->model('DB')->schema->storage->sql_maker->quote_char( '"' ); + $c->model('DB')->schema->storage->sql_maker->name_sep( '.' ); - my $probs = $c->cobrand->problems->timeline; + my $probs = $c->cobrand->problems->timeline; - foreach ($probs->all) { - push @{$time{$_->created->epoch}}, { type => 'problemCreated', date => $_->created, obj => $_ }; - push @{$time{$_->confirmed->epoch}}, { type => 'problemConfirmed', date => $_->confirmed, obj => $_ } if $_->confirmed; - push @{$time{$_->whensent->epoch}}, { type => 'problemSent', date => $_->whensent, obj => $_ } if $_->whensent; - } - - my $questionnaires = $c->model('DB::Questionnaire')->timeline( $c->cobrand->restriction ); + foreach ($probs->all) { + push @{$time{$_->created->epoch}}, { type => 'problemCreated', date => $_->created, obj => $_ }; + push @{$time{$_->confirmed->epoch}}, { type => 'problemConfirmed', date => $_->confirmed, obj => $_ } if $_->confirmed; + push @{$time{$_->whensent->epoch}}, { type => 'problemSent', date => $_->whensent, obj => $_ } if $_->whensent; + } - foreach ($questionnaires->all) { - push @{$time{$_->whensent->epoch}}, { type => 'quesSent', date => $_->whensent, obj => $_ }; - push @{$time{$_->whenanswered->epoch}}, { type => 'quesAnswered', date => $_->whenanswered, obj => $_ } if $_->whenanswered; - } + my $questionnaires = $c->model('DB::Questionnaire')->timeline( $c->cobrand->restriction ); - my $updates = $c->cobrand->updates->timeline; + foreach ($questionnaires->all) { + push @{$time{$_->whensent->epoch}}, { type => 'quesSent', date => $_->whensent, obj => $_ }; + push @{$time{$_->whenanswered->epoch}}, { type => 'quesAnswered', date => $_->whenanswered, obj => $_ } if $_->whenanswered; + } - foreach ($updates->all) { - push @{$time{$_->created->epoch}}, { type => 'update', date => $_->created, obj => $_} ; - } + my $updates = $c->cobrand->updates->timeline; - my $alerts = $c->model('DB::Alert')->timeline_created( $c->cobrand->restriction ); + foreach ($updates->all) { + push @{$time{$_->created->epoch}}, { type => 'update', date => $_->created, obj => $_} ; + } - foreach ($alerts->all) { - push @{$time{$_->whensubscribed->epoch}}, { type => 'alertSub', date => $_->whensubscribed, obj => $_ }; - } + my $alerts = $c->model('DB::Alert')->timeline_created( $c->cobrand->restriction ); - $alerts = $c->model('DB::Alert')->timeline_disabled( $c->cobrand->restriction ); + foreach ($alerts->all) { + push @{$time{$_->whensubscribed->epoch}}, { type => 'alertSub', date => $_->whensubscribed, obj => $_ }; + } - foreach ($alerts->all) { - push @{$time{$_->whendisabled->epoch}}, { type => 'alertDel', date => $_->whendisabled, obj => $_ }; - } + $alerts = $c->model('DB::Alert')->timeline_disabled( $c->cobrand->restriction ); - $c->model('DB')->schema->storage->sql_maker->quote_char( '' ); + foreach ($alerts->all) { + push @{$time{$_->whendisabled->epoch}}, { type => 'alertDel', date => $_->whendisabled, obj => $_ }; + } + } catch { + die $_; + } finally { + $c->model('DB')->schema->storage->sql_maker->quote_char( '' ); + }; $c->stash->{time} = \%time; diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index b2a0e331d..817fc48a1 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -7,6 +7,7 @@ use RABX; use List::Util qw(min); use Scalar::Util 'blessed'; use DateTime::Format::Pg; +use Try::Tiny; use strict; use warnings; @@ -1172,95 +1173,100 @@ sub admin_stats { sub export_as_csv { my ($self, $c, $params) = @_; - $c->model('DB')->schema->storage->sql_maker->quote_char('"'); - my $csv = $c->stash->{csv} = { - problems => $c->model('DB::Problem')->search_rs( - $params, - { - join => ['admin_log_entries', 'user'], - distinct => 1, - columns => [ - 'id', 'created', - 'latitude', 'longitude', - 'cobrand', 'category', - 'state', 'user_id', - 'external_body', - 'title', 'detail', - 'photo', - 'whensent', 'lastupdate', - 'service', - 'extra', - { sum_time_spent => { sum => 'admin_log_entries.time_spent' } }, - 'name', 'user.id', 'user.email', 'user.phone', 'user.name', - ] - } - ), - headers => [ - 'Report ID', 'Created', 'Sent to Agency', 'Last Updated', - 'E', 'N', 'Category', 'Status', 'Closure Status', - 'UserID', 'User email', 'User phone', 'User name', - 'External Body', 'Time Spent', 'Title', 'Detail', - 'Media URL', 'Interface Used', 'Council Response', - 'Strasse', 'Mast-Nr.', 'Haus-Nr.', 'Hydranten-Nr.', - ], - columns => [ - 'id', 'created', 'whensent',' lastupdate', 'local_coords_x', - 'local_coords_y', 'category', 'state', 'closure_status', - 'user_id', 'user_email', 'user_phone', 'user_name', - 'body_name', 'sum_time_spent', 'title', 'detail', - 'media_url', 'service', 'public_response', - 'strasse', 'mast_nr',' haus_nr', 'hydranten_nr', - ], - extra_data => sub { - my $report = shift; - - my $body_name = ""; - if ( my $external_body = $report->body($c) ) { - $body_name = $external_body->name || '[Unknown body]'; - } + try { + $c->model('DB')->schema->storage->sql_maker->quote_char('"'); + my $csv = $c->stash->{csv} = { + problems => $c->model('DB::Problem')->search_rs( + $params, + { + join => ['admin_log_entries', 'user'], + distinct => 1, + columns => [ + 'id', 'created', + 'latitude', 'longitude', + 'cobrand', 'category', + 'state', 'user_id', + 'external_body', + 'title', 'detail', + 'photo', + 'whensent', 'lastupdate', + 'service', + 'extra', + { sum_time_spent => { sum => 'admin_log_entries.time_spent' } }, + 'name', 'user.id', 'user.email', 'user.phone', 'user.name', + ] + } + ), + headers => [ + 'Report ID', 'Created', 'Sent to Agency', 'Last Updated', + 'E', 'N', 'Category', 'Status', 'Closure Status', + 'UserID', 'User email', 'User phone', 'User name', + 'External Body', 'Time Spent', 'Title', 'Detail', + 'Media URL', 'Interface Used', 'Council Response', + 'Strasse', 'Mast-Nr.', 'Haus-Nr.', 'Hydranten-Nr.', + ], + columns => [ + 'id', 'created', 'whensent',' lastupdate', 'local_coords_x', + 'local_coords_y', 'category', 'state', 'closure_status', + 'user_id', 'user_email', 'user_phone', 'user_name', + 'body_name', 'sum_time_spent', 'title', 'detail', + 'media_url', 'service', 'public_response', + 'strasse', 'mast_nr',' haus_nr', 'hydranten_nr', + ], + extra_data => sub { + my $report = shift; + + my $body_name = ""; + if ( my $external_body = $report->body($c) ) { + $body_name = $external_body->name || '[Unknown body]'; + } - my $detail = $report->detail; - my $public_response = $report->get_extra_metadata('public_response') || ''; - my $metas = $report->get_extra_fields(); - my %extras; - foreach my $field (@$metas) { - $extras{$field->{name}} = $field->{value}; - } + my $detail = $report->detail; + my $public_response = $report->get_extra_metadata('public_response') || ''; + my $metas = $report->get_extra_fields(); + my %extras; + foreach my $field (@$metas) { + $extras{$field->{name}} = $field->{value}; + } - # replace newlines with HTML <br/> element - $detail =~ s{\r?\n}{ <br/> }g; - $public_response =~ s{\r?\n}{ <br/> }g if $public_response; - - # Assemble photo URL, if report has a photo - my $photo_to_display = $c->cobrand->allow_photo_display($report); - my $media_url = (@{$report->photos} && $photo_to_display) - ? $c->cobrand->base_url . $report->photos->[$photo_to_display-1]->{url} - : ''; - - return { - whensent => $report->whensent, - lastupdate => $report->lastupdate, - user_id => $report->user_id, - user_email => $report->user->email || '', - user_phone => $report->user->phone || '', - user_name => $report->name, - closure_status => $report->get_extra_metadata('closure_status') || '', - body_name => $body_name, - sum_time_spent => $report->get_column('sum_time_spent') || 0, - detail => $detail, - media_url => $media_url, - service => $report->service || 'Web interface', - public_response => $public_response, - strasse => $extras{'strasse'} || '', - mast_nr => $extras{'mast_nr'} || '', - haus_nr => $extras{'haus_nr'} || '', - hydranten_nr => $extras{'hydranten_nr'} || '' - }; - }, - filename => 'stats', + # replace newlines with HTML <br/> element + $detail =~ s{\r?\n}{ <br/> }g; + $public_response =~ s{\r?\n}{ <br/> }g if $public_response; + + # Assemble photo URL, if report has a photo + my $photo_to_display = $c->cobrand->allow_photo_display($report); + my $media_url = (@{$report->photos} && $photo_to_display) + ? $c->cobrand->base_url . $report->photos->[$photo_to_display-1]->{url} + : ''; + + return { + whensent => $report->whensent, + lastupdate => $report->lastupdate, + user_id => $report->user_id, + user_email => $report->user->email || '', + user_phone => $report->user->phone || '', + user_name => $report->name, + closure_status => $report->get_extra_metadata('closure_status') || '', + body_name => $body_name, + sum_time_spent => $report->get_column('sum_time_spent') || 0, + detail => $detail, + media_url => $media_url, + service => $report->service || 'Web interface', + public_response => $public_response, + strasse => $extras{'strasse'} || '', + mast_nr => $extras{'mast_nr'} || '', + haus_nr => $extras{'haus_nr'} || '', + hydranten_nr => $extras{'hydranten_nr'} || '' + }; + }, + filename => 'stats', + }; + $c->forward('/dashboard/generate_csv'); + } catch { + die $_; + } finally { + $c->model('DB')->schema->storage->sql_maker->quote_char(''); }; - $c->forward('/dashboard/generate_csv'); - $c->model('DB')->schema->storage->sql_maker->quote_char(''); } sub problem_confirm_email_extras { |