Skip to content

Commit

Permalink
Merge 59696d3 into 34010d3
Browse files Browse the repository at this point in the history
  • Loading branch information
struan authored Aug 3, 2021
2 parents 34010d3 + 59696d3 commit df1855c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Add per-photo moderation. #3055
- Redaction support for photos.
- UK Councils no questionnaires for non-updating users
- Script to export/import response templates, #3549
- Development improvements:
- Include failure count in send report error output, #3316
- Sort output in export script. #3323
Expand Down
23 changes: 18 additions & 5 deletions bin/export-import-data
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use v5.14;
use warnings;
# avoid wide character warnings
no warnings 'utf8';

BEGIN {
use File::Basename qw(dirname);
Expand All @@ -20,6 +22,7 @@ my ($opt, $usage) = describe_options(
[ 'name=s', "Name of body" ],
[ 'import=s', "File to import" ],
[ 'commit', "Actually commit changes to the database" ],
[ 'update-templates', "Update existing templates (default is to skip)" ],
[ 'categories=s', "pipe-separated list of categories to export contacts for and filter templates by" ],
[ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
Expand All @@ -32,7 +35,7 @@ my $body = FixMyStreet::DB->resultset("Body")->find({ name => $opt->name }) or d
my @categories = split(/\|/, ($opt->{categories} || ''));

if ($opt->import) {
import($opt->import);
import($opt->import, $opt->update_templates);
} else {
export();
}
Expand Down Expand Up @@ -102,6 +105,7 @@ sub export {

sub import {
my $file = shift;
my $update_templates = shift;

my $json = path($file)->slurp;
my $out = $J->decode($json);
Expand Down Expand Up @@ -130,20 +134,29 @@ sub import {

foreach (@{$out->{templates}}) {
my $existing = $body->response_templates->search({ title => $_->{title} })->single;
if ($existing) {
if ($existing && !$update_templates) {
warn "Template with title $_->{title} already exists, skipping";
next;
}
my $template = $body->response_templates->new({
my $template = $body->response_templates->update_or_new({
title => $_->{title},
text => $_->{text},
state => $_->{state},
auto_response => $_->{auto_response},
external_status_code => $_->{external_status_code},
});
$template->insert;
$template->insert unless $template->in_storage;
foreach (@{$_->{categories}}) {
my $contact = $body->contacts->find({ category => $_ }) or die "Cannot find category $_ for template " . $template->title . "\n";
my $contact = $body->contacts->find({ category => $_ });
unless ( $contact ) {
my $msg = "Cannot find category $_ for template " . $template->title . "\n";
if ($update_templates) {
warn $msg;
next;
} else {
die $msg;
}
}
$template->contact_response_templates->find_or_create({
contact_id => $contact->id,
});
Expand Down

0 comments on commit df1855c

Please sign in to comment.