Преглед изворни кода

Add scripts to load polling station and polling district data

tags/last-sinatra-version
Adrian Short пре 9 година
родитељ
комит
8a950b8174
3 измењених фајлова са 62 додато и 0 уклоњено
  1. +13
    -0
      scripts/load-polling-district-postcodes.rb
  2. +25
    -0
      scripts/load-polling-districts.rb
  3. +24
    -0
      scripts/load-polling-stations.rb

+ 13
- 0
scripts/load-polling-district-postcodes.rb Прегледај датотеку

@@ -0,0 +1,13 @@
require 'csv'
require './models'

CSV.foreach(ARGV.shift) do |row|
pc = row[0].strip
puts pc
if @postcode = Postcode.get(pc)
@postcode.polling_station_id = row[1]
@postcode.save
else
puts "#{pc} not found"
end
end

+ 25
- 0
scripts/load-polling-districts.rb Прегледај датотеку

@@ -0,0 +1,25 @@
# load polling districts data

require './models'
require 'csv'
require 'pp'

DataMapper::Model.raise_on_save_failure = true
PollingStation.destroy

CSV.foreach(ARGV.shift, :headers => true) do |row|
begin
@p = PollingStation.create(
:id => row['District'].strip,
:easting => row['Eastings'],
:northing => row['Northings'],
:lat => row['lat'],
:lng => row['lng']
)
@p.save
rescue
pp @p
# puts p.errors
end
end

+ 24
- 0
scripts/load-polling-stations.rb Прегледај датотеку

@@ -0,0 +1,24 @@
# load polling stations data (polling-stations.csv)

require './models'
require 'csv'
require 'pp'

DataMapper::Model.raise_on_save_failure = true

CSV.foreach(ARGV.shift, :headers => false) do |row|
begin
if @p = PollingStation.get(row[1])
@p.name = row[2]
@p.address = row[3]
@p.postcode = row[4]
@p.save
else
puts "#{row[1]} not found"
end
rescue
puts @p.saved?
pp @p
@p.errors.each { |r| puts r }
end
end

Loading…
Откажи
Сачувај