From c1c920585581d189e9d98e1e20c39d689469c5b2 Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 11 Feb 2021 18:00:01 +0100 Subject: WTF --- app/models/resource.rb | 5 +-- lib/tasks/import.rake | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/import.rake diff --git a/app/models/resource.rb b/app/models/resource.rb index 8df106c..633258f 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -14,7 +14,7 @@ class Resource < ApplicationRecord store_accessor :feature, :geometry, :properties - validates_associated :agent + #validates_associated :agent validates :name, presence: true, @@ -62,8 +62,9 @@ class Resource < ApplicationRecord [:name, :summary, :description, :email, :source, :address, :postal_code, :city, :phone_number, :website].each do |prop| # Define a reader define_method prop do - properties[prop.to_s] + feature['properties'][prop.to_s] end + # Define a writer define_method :"#{prop}=" do |v| feature['properties'][prop.to_s] = v end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake new file mode 100644 index 0000000..6fedbe2 --- /dev/null +++ b/lib/tasks/import.rake @@ -0,0 +1,96 @@ +namespace :import do + desc "Check doc/import for resources to import against existing database" + task check: :environment do + begin + features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })['features'] + rescue Exception => e + p e + $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?" + exit(1) + end + + # Make a first pass to see if we have existing records + + existing_records = {} + i = 0 # temp + known_records = new_records = 0 + + + features.each do |feature| + i+= 1 + name = feature['name'] + phone_number = feature['phone_number'] + email = feature['email'] + website = feature['website'] + + unless name.blank? + r0 = Resource.where("feature #>> '{properties,name}' = :name", name: name) + $stderr.puts "No existing resource with name '%s'" % name if r0.blank? + end + + unless phone_number.blank? + r1 = Resource.where("feature #>> '{properties,phone_number}' = :num", num: PhonyRails.normalize_number(phone_number, default_country_code: 'BE')) + $stderr.puts "No existing resource with phone '%s'" % phone_number if r1.blank? + end + + unless email.blank? + r2 = Resource.where("feature #>> '{properties,email}' = :email", email: email) + $stderr.puts "No existing resource with email = '%s'" % email if r2.blank? + end + + unless website.blank? + r3 = Resource.where("feature #>> '{properties,website}' = :url", url: website) + $stderr.puts "No existing resource with website = '%s" % website if r3.blank? + end + + unless r0.blank? && r1.blank? && r2.blank? && r3.blank? + $stderr.print '!' + known_records += 1 +=begin + $stderr.puts "Some potential resources found..." + p r0 + p r1 + p r2 + p r3 +=end + else + new_records += 1 + $stderr.print "." + end + + # Normalize fields + + # stop at 3 + #i >= 3 && exit(0) + end + $stderr.puts "Task complete (known: #{known_records}, new: #{new_records}, total: #{features.count})" + end + + desc "Import new resources" + task import: :environment do + dewey = Agent.find_by(name: 'Dewey') + concertes = Agent.find_by(name: 'ConcertES') + + $stderr.puts "Resources: #{Resource.count}" + + begin + features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false }) + rescue Exception => e + p e + $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?" + exit(1) + end + + $stderr.puts "Features: #{features['features'].count}" + + features['features'].each do |feat| + ag = feat['properties']['source'] == 'ConcertES' ? concertes : dewey + $stderr.puts "Creating Resource ( #{feat['properties']['name']} ) for #{ag.name}" + r = Resource.new(feature: feat, agent_id: ag.id) + p r + p r.errors unless r.valid? + exit unless r.save + $stderr.puts "Created Resource #{r.uuid}" + end + end +end -- cgit v1.2.3