added importing doogal data for wards on postcode
This commit is contained in:
parent
9b7d8530b6
commit
c977cf3279
16 changed files with 4947 additions and 1 deletions
|
@ -43,6 +43,34 @@ sub post_suppliers {
|
|||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
sub post_postcodes {
|
||||
my $c = shift;
|
||||
|
||||
unless ($c->param('postcodes_csv')) {
|
||||
$c->flash(error => "No CSV file given");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ($c->req->is_limit_exceeded) {
|
||||
$c->flash(error => "CSV file size is too large");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('postcodes_csv');
|
||||
|
||||
my $filename = path($c->app->config->{upload_path}, time . 'postcodes.csv');
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue('csv_postcode_import' => [ $filename ]);
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
sub post_transactions {
|
||||
my $c = shift;
|
||||
|
||||
|
|
44
lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm
Normal file
44
lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm
Normal file
|
@ -0,0 +1,44 @@
|
|||
package Pear::LocalLoop::Import::LCCCsv::Postcodes;
|
||||
use Moo;
|
||||
|
||||
use Geo::UK::Postcode::Regex;
|
||||
|
||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
has '+csv_required_columns' => (
|
||||
builder => sub { return [ qw/
|
||||
postcode
|
||||
ward
|
||||
/ ]},
|
||||
);
|
||||
|
||||
sub import_csv {
|
||||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row);
|
||||
}
|
||||
}
|
||||
|
||||
use Devel::Dwarn;
|
||||
sub _row_to_result {
|
||||
my ( $self, $row ) = @_;
|
||||
|
||||
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $row->{postcode} );
|
||||
|
||||
my $ward = $self->schema->resultset('GbWard')->find_or_create(ward => $row->{ward});
|
||||
|
||||
my $postcode_r = $self->schema->resultset('GbPostcode')->find({
|
||||
outcode => $postcode_obj->{outcode},
|
||||
incode => $postcode_obj->{incode},
|
||||
});
|
||||
|
||||
return unless $postcode_r;
|
||||
return if $postcode_r->ward;
|
||||
|
||||
$postcode_r->update({ ward_id => $ward->id });
|
||||
}
|
||||
|
||||
1;
|
15
lib/Pear/LocalLoop/Plugin/Minion/Job/csv_postcode_import.pm
Normal file
15
lib/Pear/LocalLoop/Plugin/Minion/Job/csv_postcode_import.pm
Normal file
|
@ -0,0 +1,15 @@
|
|||
package Pear::LocalLoop::Plugin::Minion::Job::csv_postcode_import;
|
||||
use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
|
||||
|
||||
use Pear::LocalLoop::Import::LCCCsv::Postcodes;
|
||||
|
||||
sub run {
|
||||
my ( $self, $filename ) = @_;
|
||||
|
||||
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Postcodes->new(
|
||||
csv_file => $filename,
|
||||
schema => $self->app->schema
|
||||
)->import_csv;
|
||||
}
|
||||
|
||||
1;
|
|
@ -6,7 +6,7 @@ use warnings;
|
|||
|
||||
use base 'DBIx::Class::Schema';
|
||||
|
||||
our $VERSION = 28;
|
||||
our $VERSION = 29;
|
||||
|
||||
__PACKAGE__->load_namespaces;
|
||||
|
||||
|
|
|
@ -31,8 +31,19 @@ __PACKAGE__->add_columns(
|
|||
is_nullable => 1,
|
||||
default_value => undef,
|
||||
},
|
||||
ward_id => {
|
||||
data_type => 'integer',
|
||||
is_nullable => 1,
|
||||
default_value => undef,
|
||||
},
|
||||
);
|
||||
|
||||
__PACKAGE__->set_primary_key(qw/ outcode incode /);
|
||||
|
||||
__PACKAGE__->belongs_to(
|
||||
"ward",
|
||||
"Pear::LocalLoop::Schema::Result::GbWard",
|
||||
"ward_id",
|
||||
);
|
||||
|
||||
1;
|
||||
|
|
32
lib/Pear/LocalLoop/Schema/Result/GbWard.pm
Normal file
32
lib/Pear/LocalLoop/Schema/Result/GbWard.pm
Normal file
|
@ -0,0 +1,32 @@
|
|||
package Pear::LocalLoop::Schema::Result::GbWard;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use base 'DBIx::Class::Core';
|
||||
|
||||
__PACKAGE__->table('gb_wards');
|
||||
|
||||
__PACKAGE__->add_columns(
|
||||
id => {
|
||||
data_type => "integer",
|
||||
is_auto_increment => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
ward => {
|
||||
data_type => 'varchar',
|
||||
size => 100,
|
||||
is_nullable => 0,
|
||||
},
|
||||
);
|
||||
|
||||
__PACKAGE__->set_primary_key(qw/ id /);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"postcodes",
|
||||
"Pear::LocalLoop::Schema::Result::GbPostcode",
|
||||
{ "foreign.ward_id" => "self.id" },
|
||||
{ cascade_copy => 0, cascade_delete => 0 },
|
||||
);
|
||||
|
||||
1;
|
Reference in a new issue