From 212ea3e9f788c2a83369b02b53c175c1afd8fe0e Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 27 Feb 2012 11:52:54 +0000 Subject: rough first draft of send-report refactor not. Work in progress at the moment --- perllib/FixMyStreet/SendReport/London.pm | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 perllib/FixMyStreet/SendReport/London.pm (limited to 'perllib/FixMyStreet/SendReport/London.pm') diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm new file mode 100644 index 000000000..3d4ce89c2 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -0,0 +1,115 @@ +package FixMyStreet::SendReport::Email; + +my %councils = (); +my @to; + +sub reset { + %councils = (); + @to = (); +} + +sub add_council { + my $council = shift; + my $name = shift; + + $councils{ $council } = $name; +} +sub construct_message { + my %h = @_; + return <{message} = construct_message( %$h ); + my $phone = $h->{phone}; + my $mobile = ''; + if ($phone && $phone =~ /^\s*07/) { + $mobile = $phone; + $phone = ''; + } + my ($first, $last) = $h->{name} =~ /^(\S*)(?: (.*))?$/; + my %params = ( + Key => mySociety::Config::get('LONDON_REPORTIT_KEY'), + Signature => Digest::MD5::md5_hex( $h->{confirmed} . mySociety::Config::get('LONDON_REPORTIT_SECRET') ), + Type => Utils::london_categories()->{$h->{category}}, + RequestDate => $h->{confirmed}, + RequestMethod => 'Web', + ExternalId => $h->{url}, + 'Customer.Title' => '', + 'Customer.FirstName' => $first, + 'Customer.Surname' => $last, + 'Customer.Email' => $h->{email}, + 'Customer.Phone' => $phone, + 'Customer.Mobile' => $mobile, + 'ProblemDescription' => $h->{message}, + ); + if ($h->{used_map}) { + $params{'Location.Latitude'} = $h->{latitude}; + $params{'Location.Longitude'} = $h->{longitude}; + } elsif (mySociety::PostcodeUtil::is_valid_postcode($h->{query})) { + # Didn't use map, and entered postcode, so use that. + $params{'Location.Postcode'} = $h->{query}; + } else { + # Otherwise, lat/lon is all we have, even if it's wrong. + $params{'Location.Latitude'} = $h->{latitude}; + $params{'Location.Longitude'} = $h->{longitude}; + } + if ($h->{has_photo}) { + $params{'Document1.Name'} = 'Photograph'; + $params{'Document1.MimeType'} = 'image/jpeg'; + $params{'Document1.URL'} = $h->{image_url}; + $params{'Document1.URLPublic'} = 'true'; + } + my $browser = LWP::UserAgent->new; + my $response = $browser->post( mySociety::Config::get('LONDON_REPORTIT_URL'), \%params ); + my $out = $response->content; + if ($response->code ne 200) { + print "Failed to post $h->{id} to London API, response was " . $response->code . " $out\n"; + return 1; + } + my ($id) = $out =~ /(.*?)<\/caseid>/; + my ($org) = $out =~ /(.*?)<\/organisation>/; + my ($team) = $out =~ /(.*?)<\/team>/; + + $org = london_lookup($org); + $problem->external_id( $id ); + $problem->external_body( $org ); + $problem->external_team( $team ); + return 0; +} + +sub london_lookup { + my $org = shift || ''; + my $str = "Unknown ($org)"; + open(FP, "$FindBin::Bin/../data/dft.csv"); + while () { + /^(.*?),(.*)/; + if ($org eq $1) { + $str = $2; + last; + } + } + close FP; + return $str; +} + +1; -- cgit v1.2.3 From 6b5a40f54eb4ef87724ecbd53513cf707b00d106 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 27 Feb 2012 13:43:57 +0000 Subject: sort out use statements --- perllib/FixMyStreet/SendReport/London.pm | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'perllib/FixMyStreet/SendReport/London.pm') diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm index 3d4ce89c2..cb76c7a24 100644 --- a/perllib/FixMyStreet/SendReport/London.pm +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -1,5 +1,11 @@ package FixMyStreet::SendReport::Email; +use Digest::MD5; +use LWP::UserAgent; +use LWP::Simple; + +use Utils; + my %councils = (); my @to; -- cgit v1.2.3 From 21b82d30b9348c3de50f711d8b7c80bcdaf2c756 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 27 Feb 2012 15:21:41 +0000 Subject: put something in the base class and make things inherit from it --- perllib/FixMyStreet/SendReport/London.pm | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'perllib/FixMyStreet/SendReport/London.pm') diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm index cb76c7a24..29368314f 100644 --- a/perllib/FixMyStreet/SendReport/London.pm +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -1,25 +1,16 @@ package FixMyStreet::SendReport::Email; +use Moose; +use namespace::autoclean; + +BEGIN { extends 'FixMyStreet::SendReport'; } + use Digest::MD5; use LWP::UserAgent; use LWP::Simple; use Utils; -my %councils = (); -my @to; - -sub reset { - %councils = (); - @to = (); -} - -sub add_council { - my $council = shift; - my $name = shift; - - $councils{ $council } = $name; -} sub construct_message { my %h = @_; return < Date: Tue, 3 Apr 2012 12:16:24 +0100 Subject: fix compile errors --- perllib/FixMyStreet/SendReport/London.pm | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'perllib/FixMyStreet/SendReport/London.pm') diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm index 29368314f..9296f2446 100644 --- a/perllib/FixMyStreet/SendReport/London.pm +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -1,7 +1,6 @@ -package FixMyStreet::SendReport::Email; +package FixMyStreet::SendReport::London; use Moose; -use namespace::autoclean; BEGIN { extends 'FixMyStreet::SendReport'; } @@ -88,9 +87,9 @@ sub send { my ($team) = $out =~ /(.*?)<\/team>/; $org = london_lookup($org); - $problem->external_id( $id ); - $problem->external_body( $org ); - $problem->external_team( $team ); + $row->external_id( $id ); + $row->external_body( $org ); + $row->external_team( $team ); return 0; } -- cgit v1.2.3 From f531995b83e8448de25adab411673ec38e555385 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Tue, 3 Apr 2012 16:48:11 +0100 Subject: update send failed data if fail to send a report add code in to save success and errors --- perllib/FixMyStreet/SendReport/London.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'perllib/FixMyStreet/SendReport/London.pm') diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm index 9296f2446..58ecb2375 100644 --- a/perllib/FixMyStreet/SendReport/London.pm +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -33,7 +33,7 @@ EOF sub send { return if mySociety::Config::get('STAGING_SITE'); - my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + my ( $self, $row, $h, $to, $template, $recips, $nomail ) = @_; $h->{message} = construct_message( %$h ); my $phone = $h->{phone}; @@ -80,6 +80,7 @@ sub send { my $out = $response->content; if ($response->code ne 200) { print "Failed to post $h->{id} to London API, response was " . $response->code . " $out\n"; + $self->error( "Failed to post $h->{id} to London API, response was " . $response->code . " $out" ); return 1; } my ($id) = $out =~ /(.*?)<\/caseid>/; @@ -90,6 +91,7 @@ sub send { $row->external_id( $id ); $row->external_body( $org ); $row->external_team( $team ); + $self->success(1); return 0; } -- cgit v1.2.3