aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/geocode50
-rwxr-xr-xbin/setup-contacts204
-rwxr-xr-xbin/update-schema2
3 files changed, 247 insertions, 9 deletions
diff --git a/bin/geocode b/bin/geocode
index 2559f7a3c..f6bf79149 100755
--- a/bin/geocode
+++ b/bin/geocode
@@ -12,10 +12,23 @@ geocode - commandline tool to test geocoders
$ bin/geocode --cobrand=bromley "Glebe Rd"
# ... if you want to use config that you have in conf/general.bing.yml
- $ bin/geocode --override-config=general.bing --cobrand=bromley "Glebe Rd"
+ $ bin/geocode --override-config=general.yml.bing --cobrand=bromley "Glebe Rd"
+
+ # ... if your config only has a single entry in ALLOWED_COBRANDS
+ $ bin/geocode --override-config=general.yml.zurich "Im eisernen Zeit"
## ... output from geocoder
+=head1 OPTIONS
+
+ --help|h show this help
+ --use-cache use the configured GEO_CACHE (default OFF)
+
+ # the following options take a string argument:
+ --geocoder e.g. OSM/Bing/Google/Zurich
+ --cobrand (default, bromley, zurich etc.)
+ --override-config e.g. conf/general.yml
+
=cut
use strict;
@@ -32,6 +45,7 @@ BEGIN {
use Data::Dumper;
use Pod::Usage;
use feature 'say';
+use File::Temp 'tempdir';
use Getopt::Long;
@@ -41,13 +55,12 @@ GetOptions \%options,
'geocoder=s',
'help|h',
'cobrand=s',
- 'override-config=s';
+ 'override-config=s',
+ 'use-cache';
my $s = join ' ', @ARGV
or pod2usage(0);
-pod2usage(0) unless $options{cobrand};
-
local $ENV{FMS_OVERRIDE_CONFIG} = $options{'override-config'} if $options{'override-config'};
eval 'use FixMyStreet';
@@ -63,22 +76,41 @@ my $geocoder_type = $options{geocoder} || do {
} or pod2usage(0);
my $geocoder_name = "FixMyStreet::Geocode::${geocoder_type}";
+chomp $geocoder_name;
my $code_ref = $geocoder_name->can('string')
or die "$geocoder_name is not a valid geocoder?";
-my @allowed_cobrands = FixMyStreet::Cobrand->get_allowed_cobrands();
+my @allowed_cobrands = @{ FixMyStreet::Cobrand->get_allowed_cobrands() };
+
+my $cobrand_option = $options{cobrand}
+ || do {
+ $allowed_cobrands[0]->{moniker} if scalar @allowed_cobrands == 1;
+ } or pod2usage(0);
-my $cobrand_name = FixMyStreet::Cobrand->get_class_for_moniker($options{cobrand});
+my $cobrand_name = FixMyStreet::Cobrand->get_class_for_moniker($cobrand_option);
my $cobrand = $cobrand_name->new();
say "USING COBRAND $cobrand_name";
-if ($cobrand->moniker ne lc($options{cobrand})) {
- say "!!! asked for $options{cobrand}";
+if ($cobrand->moniker ne lc($cobrand_option)) {
+ say "!!! asked for $cobrand_option";
say "!!! Check ALLOWED_COBRANDS setting in conf/general.yml (or supplied --override-config file)";
say Dumper(\@allowed_cobrands);
}
+say "USING GEOCODER $geocoder_name "
+ . ( $options{'use-cache'} ?
+ 'WITH ' . FixMyStreet->config('GEO_CACHE') :
+ 'WITHOUT cache');
+
my $c = FixMyStreet::App->new();
$c->stash->{cobrand} = $cobrand;
-say Dumper( $code_ref->( $s, $c ) );
+FixMyStreet::override_config({
+ # if we're not using cache, then set GEO_CACHE to a temporary directory
+ $options{'use-cache'} ?
+ () :
+ ( GEO_CACHE => (tempdir( CLEANUP => 1 ) . '/') ),
+}, sub {
+ my $result = $code_ref->( $s, $c );
+ say Dumper $result;
+});
diff --git a/bin/setup-contacts b/bin/setup-contacts
new file mode 100755
index 000000000..d562ae71d
--- /dev/null
+++ b/bin/setup-contacts
@@ -0,0 +1,204 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+require 5.8.0;
+use feature 'say';
+
+BEGIN {
+ use File::Basename qw(dirname);
+ use File::Spec;
+ my $d = dirname(File::Spec->rel2abs($0));
+ require "$d/../setenv.pl";
+}
+
+use FixMyStreet::App;
+
+my $moniker = $ARGV[0];
+
+my $c = FixMyStreet::App->new();
+my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($moniker)->new({ c => $c });
+$c->stash->{cobrand} = $cobrand;
+
+die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force
+say "Applying contacts for $cobrand";
+
+
+ensure_bodies();
+setup_contacts();
+
+=head2 setup_contacts, ensure_bodies
+
+routines to update extra data for contacts. These can be called by
+a script:
+
+ bin/setup-contacts zurich
+
+=cut
+
+sub ensure_bodies {
+ my @bodies = $cobrand->body_details_data;
+
+ my $bodies_rs = $c->model('DB::Body');
+
+ for my $body (@bodies) {
+ # following should work (having added Unique name/parent constraint, but doesn't)
+ # $bodies_rs->find_or_create( $body, { $parent ? ( key => 'body_name_parent_key' ) : () } );
+ # let's keep it simple and just allow unique names
+ next if $bodies_rs->search({ name => $body->{name} })->count;
+ if (my $area_id = delete $body->{area_id}) {
+ $body->{body_areas} = [ { area_id => $area_id } ];
+ }
+ my $parent = $body->{parent};
+ if ($parent and ! ref $parent) {
+ $body->{parent} = { name => $parent };
+ }
+ $bodies_rs->find_or_create( $body );
+ }
+}
+
+sub setup_contacts {
+ die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force
+
+ my @contact_details = $cobrand->contact_details_data;
+
+ for my $detail (@contact_details) {
+ update_contact( $detail, $description );
+ }
+}
+
+sub update_contact {
+ my ($contact_data, $description) = @_;
+
+ my $contact_rs = $c->model('DB::Contact');
+
+ my $category = $contact_data->{category} or die "No category provided";
+ $description ||= "Update contact";
+
+ my $contact = ensure_contact($contact_data, $description)
+ or return; # e.g. nothing returned if deleted
+
+ if (my $fields = $contact_data->{fields}) {
+
+ my @fields = map { get_field_extra($_) } @$fields;
+ my $note = sprintf 'Fields edited by automated script%s', $description ? " ($description)" : '';
+ $contact->set_extra_fields(@fields);
+ $contact->set_inflated_columns({
+ confirmed => 1,
+ deleted => 0,
+ editor => 'automated script',
+ whenedited => \'NOW()',
+ note => "Updated fields $description",
+ });
+ $contact->update;
+ }
+}
+
+sub ensure_contact {
+ my ($contact_data, $description) = @_;
+
+ my $category = $contact_data->{category} or die "No category provided";
+ $description ||= "Ensure contact exists $category";
+
+ my $email = temp_email_to_update(); # will only be set if newly created
+
+ my $body = get_body_for_contact($contact_data) or die "No body found for $category";
+
+ my $contact_rs = $c->model('DB::Contact');
+
+ my $category_details = $contact_data->{category_details} || {};
+
+ if (my $old_name = delete $contact_data->{rename_from}) {
+ if (my $old_category = $contact_rs->find({
+ category => $old_name,
+ , body => $body,
+ })) {
+ $old_category->update({
+ category => $category,
+ whenedited => \'NOW()',
+ note => "Renamed $description",
+ %{ $category_details || {} },
+ });
+ return $old_category;
+ }
+ }
+
+ if ($contact_data->{delete}) {
+ my $contact = $contact_rs->search({
+ body_id => $body->id,
+ category => $category,
+ deleted => 0
+ });
+ if ($contact->count) {
+ print sprintf "Deleting: %s\n", $category;
+ $contact->update({
+ deleted => 1,
+ editor => 'automated script',
+ whenedited => \'NOW()',
+ note => "Deleted by script $description",
+ });
+ }
+ return;
+ }
+
+ return $contact_rs->find_or_create(
+ {
+ body => $body,
+ category => $category,
+
+ confirmed => 1,
+ deleted => 0,
+ email => $email,
+ editor => 'automated script',
+ note => 'created by automated script',
+ send_method => '',
+ whenedited => \'NOW()',
+ %{ $category_details || {} },
+ },
+ {
+ key => 'contacts_body_id_category_idx'
+ }
+ );
+}
+
+sub get_field_extra {
+ my ($field) = @_;
+
+ my %default = (
+ variable => 'true',
+ order => '1',
+ required => 'no',
+ datatype => 'string',
+ datatype_description => 'a string',
+ );
+
+ if (($field->{datatype} || '') eq 'boolean') {
+ %default = (
+ %default,
+ datatype => 'singlevaluelist',
+ datatype_description => 'Yes or No',
+ values => { value => [
+ { key => ['No'], name => ['No'] },
+ { key => ['Yes'], name => ['Yes'] },
+ ] },
+ );
+ }
+
+ return { %default, %$field };
+}
+
+sub temp_email_to_update { 'test@example.com' }
+
+sub get_body_for_contact {
+ my ($contact_data) = @_;
+ if (my $body_name = $contact_data->{body_name}) {
+ return $c->model('DB::Body')->find({ name => $body_name });
+ }
+ if ($cobrand->can('contact_details_data_body_default')) {
+ return $cobrand->contact_details_data_body_default;
+ }
+ return;
+ # TODO: for UK Councils use
+ # $c->model('DB::Body')->find(id => $cobrand->council_id);
+ # # NB: (or better that's the area in BodyAreas)
+}
diff --git a/bin/update-schema b/bin/update-schema
index d40df1689..1af08b002 100755
--- a/bin/update-schema
+++ b/bin/update-schema
@@ -195,6 +195,8 @@ else {
# By querying the database schema, we can see where we're currently at
# (assuming schema change files are never half-applied, which should be the case)
sub get_db_version {
+ return '0038' if column_exists('admin_log', 'time_spent');
+ return '0037' if table_exists('response_templates');
return '0036' if constraint_contains('problem_cobrand_check', 'a-z0-9_');
return '0035' if column_exists('problem', 'bodies_missing');
return '0034' if ! function_exists('ms_current_timestamp');