aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormatthew <matthew>2008-10-09 17:18:02 +0000
committermatthew <matthew>2008-10-09 17:18:02 +0000
commita6f1014f947ca9b4b38b5774cedc209e6c35f361 (patch)
tree9cea5afc7a22031ce63bf3d5c163dccdd46f68b7
parentc414325ae37cb61c974f26f040866f2d071eeb56 (diff)
Various tweaks, import script.
-rwxr-xr-xbin/import-flickr12
-rw-r--r--templates/emails/partial2
-rwxr-xr-xweb/import.cgi102
-rwxr-xr-xweb/index.cgi37
4 files changed, 131 insertions, 22 deletions
diff --git a/bin/import-flickr b/bin/import-flickr
index 10ccba7ed..86ab6af97 100755
--- a/bin/import-flickr
+++ b/bin/import-flickr
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: import-flickr,v 1.8 2008-10-09 14:20:53 matthew Exp $
+# $Id: import-flickr,v 1.9 2008-10-09 17:18:02 matthew Exp $
use strict;
require 5.8.0;
@@ -74,23 +74,17 @@ sub problem_create {
my ($name, $email) = dbh()->selectrow_array("select name, email from partial_user where service='flickr' and nsid=?", {}, $owner);
my ($easting, $northing) = (0,0);
$name ||= '';
- my $areas = '';
if ($lat && $lon) {
# XXX This appears to be going wrong :(
($easting, $northing) = mySociety::GeoUtil::wgs84_to_national_grid($lat, $lon, 'G');
- $areas = mySociety::MaPit::get_voting_areas_by_location(
- { easting=>$easting, northing=>$northing },
- 'polygon', [qw(WMC CTY CED DIS DIW MTD MTW COI COP LGD LGE UTA UTE UTW LBO LBW LAC SPC WAC NIE)]
- );
- $areas = ',' . join(',', sort keys %$areas) . ',';
}
my $id = dbh()->selectrow_array("select nextval('problem_id_seq')");
Utils::workaround_pg_bytea("insert into problem
(id, postcode, easting, northing, title, detail, name,
email, phone, photo, state, used_map, anonymous, category, areas)
values
- (?, '', ?, ?, ?, '', ?, ?, '', ?, 'partial', 't', 'f', '', ?)", 7,
- $id, $easting, $northing, $title, $name, $email, $image, $areas
+ (?, '', ?, ?, ?, '', ?, ?, '', ?, 'partial', 't', 'f', '', '')", 7,
+ $id, $easting, $northing, $title, $name, $email, $image
);
dbh()->do('insert into flickr_imported (id, problem_id) values (?, ?)', {}, $photo_id, $id);
diff --git a/templates/emails/partial b/templates/emails/partial
index 5f61d4abc..2dfc6de79 100644
--- a/templates/emails/partial
+++ b/templates/emails/partial
@@ -1,6 +1,6 @@
Subject: Your new report on FixMyStreet
-Hi <?=$values['name']?>,
+Hi<?=$values['name']?>,
We've stored the report you uploaded to FixMyStreet via
<?=$values['service']?>. To check the details we have,
diff --git a/web/import.cgi b/web/import.cgi
new file mode 100755
index 000000000..9c27c4702
--- /dev/null
+++ b/web/import.cgi
@@ -0,0 +1,102 @@
+#!/usr/bin/perl -w -I../perllib
+
+# import.cgi
+# Script to which things like iPhones can POST new data
+#
+# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
+# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
+#
+# $Id: import.cgi,v 1.1 2008-10-09 17:18:03 matthew Exp $
+
+use strict;
+use Standard;
+use mySociety::AuthToken;
+use mySociety::Email;
+use mySociety::EmailUtil;
+
+sub main {
+ my $q = shift;
+ print $q->header(-charset => 'utf-8', -content_type => 'text/plain');
+
+ my @vars = qw(service title detail name email phone easting northing lat lon);
+ my %input = map { $_ => $q->param($_) || '' } @vars;
+ my @errors;
+
+ my $fh = $q->upload('photo');
+ if ($fh) {
+ my $err = Page::check_photo($q, $fh);
+ push @errors, $err if $err;
+ }
+
+ push @errors, 'You must supply a service' unless $input{service};
+ push @errors, 'Please enter a subject' unless $input{title} && $input{title} =~ /\S/;
+ push @errors, 'Please enter your name' unless $input{name} && $input{name} =~ /\S/;
+
+ if (!$input{email} || $input{email} !~ /\S/) {
+ push @errors, 'Please enter your email';
+ } elsif (!mySociety::EmailUtil::is_valid_email($input{email})) {
+ push @errors, 'Please enter a valid email';
+ }
+
+ if ($input{lat}) {
+ ($input{easting}, $input{northing}) = mySociety::GeoUtil::wgs84_to_national_grid($input{lat}, $input{lon}, 'G');
+ }
+ # TODO: Get location from photo if present in EXIF data?
+
+ my $photo;
+ if ($fh) {
+ try {
+ $photo = Page::process_photo($fh);
+ } catch Error::Simple with {
+ my $e = shift;
+ push @errors, "That photo doesn't appear to have uploaded correctly ($e), please try again.";
+ };
+ }
+
+ unless ($photo || ($input{easting} && $input{northing})) {
+ push @errors, 'Either a location or a photo must be provided.';
+ }
+
+ if (@errors) {
+ print map { "ERROR:$_\n" } @errors;
+ return;
+ }
+
+ # Store what we have so far in the database
+ my $id = dbh()->selectrow_array("select nextval('problem_id_seq')");
+ Utils::workaround_pg_bytea("insert into problem
+ (id, postcode, easting, northing, title, detail, name,
+ email, phone, photo, state, used_map, anonymous, category, areas)
+ values
+ (?, '', ?, ?, ?, ?, ?, ?, ?, ?, 'partial', 't', 'f', '', '')", 9,
+ $id, $input{easting}, $input{northing}, $input{title},
+ $input{detail}, $input{name}, $input{email}, $input{phone}, $photo);
+
+ # Send checking email
+ my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/partial");
+ my $token = mySociety::AuthToken::store('partial', $id);
+ my %h = (
+ name => $input{name} ? ' ' . $input{name} : '',
+ url => mySociety::Config::get('BASE_URL') . '/L/' . $token,
+ service => $input{service},
+ );
+
+ my $body = mySociety::Email::construct_email({
+ _template_ => $template,
+ _parameters_ => \%h,
+ To => $input{name} ? [ [ $input{email}, $input{name} ] ] : $input{email},
+ From => [ mySociety::Config::get('CONTACT_EMAIL'), 'FixMyStreet' ],
+ });
+
+ my $result = mySociety::EmailUtil::send_email($body, mySociety::Config::get('CONTACT_EMAIL'), $input{email});
+ if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) {
+ dbh()->commit();
+ print 'SUCCESS';
+ } else {
+ dbh()->rollback();
+ print 'ERROR:Could not send email';
+ }
+}
+
+Page::do_fastcgi(\&main);
+
diff --git a/web/index.cgi b/web/index.cgi
index ce774c243..b0ebe5ab7 100755
--- a/web/index.cgi
+++ b/web/index.cgi
@@ -6,7 +6,7 @@
# Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: index.cgi,v 1.213 2008-10-09 14:30:24 matthew Exp $
+# $Id: index.cgi,v 1.214 2008-10-09 17:18:03 matthew Exp $
use strict;
use Standard;
@@ -46,7 +46,8 @@ sub main {
my $id = mySociety::AuthToken::retrieve('partial', $partial);
if ($id) {
my @row = dbh()->selectrow_array(
- "select easting, northing, name, email, title from problem where id=? and state='partial'", {}, $id);
+ "select easting, northing, name, email, title, (photo is not null) as has_photo
+ from problem where id=? and state='partial'", {}, $id);
if (@row) {
$q->param('anonymous', 1);
$q->param('submit_map', 1);
@@ -55,6 +56,7 @@ sub main {
$q->param('name', $row[2]);
$q->param('email', $row[3]);
$q->param('title', $row[4]);
+ $q->param('has_photo', $row[5]);
$q->param('partial', $partial);
}
}
@@ -345,11 +347,13 @@ sub submit_problem {
if ($id) {
dbh()->do("update problem set postcode=?, easting=?, northing=?, title=?, detail=?,
name=?, email=?, phone=?, state='confirmed', council=?, used_map='t',
- anonymous=?, category=?, confirmed=ms_current_timestamp(),
+ anonymous=?, category=?, areas=?, confirmed=ms_current_timestamp(),
lastupdate=ms_current_timestamp() where id=?", {}, $input{pc}, $input{easting}, $input{northing},
$input{title}, $input{detail}, $input{name}, $input{email},
$input{phone}, $input{council}, $input{anonymous} ? 'f' : 't',
- $input{category}, $id);
+ $input{category}, $areas, $id);
+ Utils::workaround_pg_bytea('update problem set photo=? where id=?', 1, $image, $id)
+ if $image;
dbh()->commit();
$out = $q->p(sprintf(_('You have successfully confirmed your report and you can now <a href="%s">view it on the site</a>.'), "/report/$id"));
$out .= CrossSell::display_advert($q, $input{email}, $input{name});
@@ -361,13 +365,13 @@ Please <a href="/contact">let us know what went on</a> and we\'ll look into it.'
$id = dbh()->selectrow_array("select nextval('problem_id_seq');");
Utils::workaround_pg_bytea("insert into problem
(id, postcode, easting, northing, title, detail, name,
- email, phone, photo, state, council, used_map, anonymous, category, areas, send_questionnaire)
+ email, phone, photo, state, council, used_map, anonymous, category, areas)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'unconfirmed', ?, ?, ?, ?, ?, ?)", 10,
$id, $input{pc}, $input{easting}, $input{northing}, $input{title},
$input{detail}, $input{name}, $input{email}, $input{phone}, $image,
$input{council}, $used_map, $input{anonymous} ? 'f': 't', $input{category},
- $areas, $q->{site} eq 'emptyhomes' ? 'f' : 't');
+ $areas);
my %h = ();
$h{title} = $input{title};
$h{detail} = $input{detail};
@@ -424,8 +428,14 @@ sub display_form {
$easting = Page::tile_to_os($pin_x);
$northing = Page::tile_to_os($pin_y);
} elsif ($input{partial} && $input{pc} && !$input{easting} && !$input{northing}) {
- my ($x, $y, $e, $n, $error) = Page::geocode($input{pc});
- $easting = $e; $northing = $n;
+ my ($x, $y, $error);
+ try {
+ ($x, $y, $easting, $northing, $error) = Page::geocode($input{pc});
+ } catch Error::Simple with {
+ $error = shift;
+ };
+ return Page::geocode_choice($error, '/') if ref($error) eq 'ARRAY';
+ return front_page($q, $error) if $error;
$input{x} = int(Page::os_to_tile($easting));
$input{y} = int(Page::os_to_tile($northing));
$px = Page::os_to_px($easting, $input{x});
@@ -603,12 +613,15 @@ EOF
<div><label for="form_detail">Details:</label>
<textarea name="detail" id="form_detail" rows="7" cols="26">$input_h{detail}</textarea></div>
EOF
+ my $partial_id;
if (my $token = $input{partial}) {
- my $id = mySociety::AuthToken::retrieve('partial', $token);
- if ($id) {
- $out .= '<p>The photo you uploaded was:</p> <input type="hidden" name="partial" value="' . $token . '">';
- $out .= '<p><img src="/photo?id=' . $id . '"></p>';
+ $partial_id = mySociety::AuthToken::retrieve('partial', $token);
+ if ($partial_id) {
+ $out .= '<input type="hidden" name="partial" value="' . $token . '">';
}
+ }
+ if ($partial_id && $q->param('has_photo')) {
+ $out .= "<p>The photo you uploaded was:</p> <p><img src='/photo?id=$partial_id'></p>";
} else {
$out .= <<EOF;
<div id="fileupload_flashUI" style="display:none">