[NUUG fiksgatami] [patch] Allow mobile apps to get category list for a given position

Petter Reinholdtsen pere at hungry.com
Sat Mar 26 17:45:26 CET 2011


The persons working on the Android app for fiksgatami observed that
there is no simple way to get the relevant categories for a given
position.  To avoid this, I propose to extend import.cgi to allow the
app to query the set of categories.  Matthew, does this look like a
sensible approach to provide such information?  Missing documentation,
as I did not want to document it before I know how it should behave.

Here is a patch to solve it.  Below it is also a small patch to make
the import URL reported by the script configurable and avoid the
hardcoded value.

diff --git a/web/import.cgi b/web/import.cgi
index 371e70f..a747b28 100755
--- a/web/import.cgi
+++ b/web/import.cgi
@@ -15,11 +15,12 @@ use Utils;
 use mySociety::AuthToken;
 use mySociety::Config;
 use mySociety::EmailUtil;
+use mySociety::DBHandle qw(select_all);
 
 sub main {
     my $q = shift;
 
-    my @vars = qw(service subject detail name email phone easting northing lat lon id phone_id);
+    my @vars = qw(service categories subject detail name email phone easting northing lat lon id phone_id);
     my %input = map { $_ => $q->param($_) || '' } @vars;
     my @errors;
 
@@ -51,6 +52,16 @@ sub main {
     }
 
     push @errors, 'You must supply a service' unless $input{service};
+    if (defined $input{categories}) {
+        my @categories = categories_for_coordinate($latitude, $longitude);
+        if (@categories) {
+            print "SUCCESS\n";
+            print "CATEGORY: $_\n" foreach (@categories);
+        } else {
+            print "ERROR: No categories for $latitude $longitude\n";
+        }
+        return;
+    }
     push @errors, 'Please enter a subject' unless $input{subject} && $input{subject} =~ /\S/;
     push @errors, 'Please enter your name' unless $input{name} && $input{name} =~ /\S/;
 
@@ -128,6 +139,7 @@ sub main {
 Page::do_fastcgi(\&main);
 
 sub docs {
+    my $base = mySociety::Config::get('BASE_URL');
     print <<EOF;
 <p>You may inject problem reports into FixMyStreet programatically using this
 simple interface. Upon receipt, an email will be sent to the address given,
@@ -139,7 +151,7 @@ the report has been successfully received, or if not, a list of errors, one per
 line each starting with <samp>ERROR:</samp>.
 
 <p>You may submit the following information by POST to this URL
-(i.e. <samp>http://www.fixmystreet.com/import</samp> ):</p>
+(i.e. <samp>$base/import</samp> ):</p>
 <dl>
 <dt>service
 <dd>
@@ -170,3 +182,19 @@ Name of application/service using this interface.
 EOF
 }
 
+# Look up categories for a given coordinate
+sub categories_for_coordinate {
+    my ($lat, $long) = @_;
+    my $all_councils = mySociety::MaPit::call( 'point', "4326/$long,$lat" );
+    if (%$all_councils) {
+        my $categories =
+            select_all("select area_id, category from contacts " .
+                       "where deleted='f' " .
+                       "and area_id in (" . join(',', keys %$all_councils) . ')');
+        my %categorymap = (_('Other') => 1);
+        map { $categorymap{$_->{category}} = 1 } @$categories;
+        return sort keys %categorymap;
+    } else {
+        return ();
+    }
+}

diff --git a/web/import.cgi b/web/import.cgi
index 371e70f..a747b28 100755
--- a/web/import.cgi
+++ b/web/import.cgi
@@ -128,6 +139,7 @@ sub main {
 Page::do_fastcgi(\&main);
 
 sub docs {
+    my $base = mySociety::Config::get('BASE_URL');
     print <<EOF;
 <p>You may inject problem reports into FixMyStreet programatically using this
 simple interface. Upon receipt, an email will be sent to the address given,
@@ -139,7 +151,7 @@ the report has been successfully received, or if not, a list of errors, one per
 line each starting with <samp>ERROR:</samp>.
 
 <p>You may submit the following information by POST to this URL
-(i.e. <samp>http://www.fixmystreet.com/import</samp> ):</p>
+(i.e. <samp>$base/import</samp> ):</p>
 <dl>
 <dt>service
 <dd>

Happy hacking,
-- 
Petter Reinholdtsen


More information about the fiksgatami mailing list