Skip to content

Commit

Permalink
add cost importer
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Mar 17, 2021
1 parent f20a498 commit 245dec2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Copy the script into your `lib/tasks` of your Decidim installation:

```
cd lib/tasks
wget -qO- https://github.com/Platoniq/decidim-scripts/archive/0.4.tar.gz | tar --transform 's/^decidim-scripts-0.4//' -xvz
wget -qO- https://github.com/Platoniq/decidim-scripts/archive/0.5.tar.gz | tar --transform 's/^decidim-scripts-0.5//' -xvz
```

Run in local (as testing):
Expand Down Expand Up @@ -103,7 +103,7 @@ As there's no "copy" command to do that you can use the service https://transfer
1. From your computer, send the files to transfer.sh:

```
curl --upload-file proposal-answerss.csv https://transfer.sh/proposal-answers.csv -H "Max-Days: 1"
curl --upload-file proposal-answers.csv https://transfer.sh/proposal-answers.csv -H "Max-Days: 1"
curl --upload-file proposal-geolocs.csv https://transfer.sh/proposal-geolocs.csv -H "Max-Days: 1"
```

Expand All @@ -130,7 +130,7 @@ wget https://transfer.sh/x3hUa/proposal-geolocs.csv
4. Download the scripts into the `lib/tasks` folder:

```
wget -qO- https://github.com/Platoniq/decidim-scripts/archive/0.4.tar.gz | tar --transform 's/^decidim-scripts-0.4//' -xvz -C lib/tasks
wget -qO- https://github.com/Platoniq/decidim-scripts/archive/0.5.tar.gz | tar --transform 's/^decidim-scripts-0.5//' -xvz -C lib/tasks
```

5. Run the script inside the shell session (2nd terminal):
Expand All @@ -145,6 +145,7 @@ or:
```
bin/rails proposals:batch:answer[[email protected],./proposal-answerss.csv]
bin/rails proposals:batch:geoloc[[email protected],./proposal-geolocs.csv]
bin/rails proposals:batch:cost[[email protected],./proposal-costs.csv]
```

NOTE: in case the program runs out of memory in Redis, just execute it again after a minute or so to allow for the queue to empty.
73 changes: 73 additions & 0 deletions proposal_costs.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

# Run this script without parameter to show some help
#
# bin/rails proposals:batch:import
#
# bin/rails "proposals:batch:cost[[email protected],../pam-ciutat.csv]"
#
# In heroku you must copy first the script and the files, you can use the command nc to do that (see README.md)
#
# heroku run rake "proposals:batch:cost[[email protected],../pam-ciutat.csv]"
#

require_relative 'script_helpers'

namespace :proposals do
namespace :batch do
include ScriptHelpers

desc 'Import costs to proposals from a CSV'
task :cost, %i[admin csv] => :environment do |_task, args|
process_csv(args) do |admin, line|
processor = ProposalCostProcessor.new(admin, normalize(line))
processor.process!
end
end

class ProposalCostProcessor
def initialize(admin, values)
# raise_if_field_not_found(:cost, values)
# raise_if_field_not_found(:cost_report, values)
# raise_if_field_not_found(:execution_period, values)
@admin = admin
@values = values
@proposal = proposal_from_id(values[:id])
unless @proposal.component.current_settings.answers_with_costs?
raise UnprocessableError, "Component for proposal [#{@proposal.id}] needs to have costs enabled!"
end
end

attr_reader :admin, :values, :proposal, :latitude, :longitude

def process!
print "Updating proposal #{proposal.id} with cost [#{values[:cost]}]"
fields = {
cost: values[:cost],
cost_report: values[:cost_report],
execution_period: values[:execution_period]
}
if(values[:address])
geolocate
fields[:address] = values[:address]
fields[:latitude] = latitude
fields[:longitude] = longitude
end
Decidim.traceability.update!(
proposal,
admin,
fields
)
show_success('Cost updated!')
end

def geolocate
results = Geocoder.search(values[:address])
@latitude = results.first.latitude
@longitude = results.first.longitude
rescue StandardError => e
print " -#{e.message}- "
end
end
end
end
30 changes: 30 additions & 0 deletions proposal_notes.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Run this script without parameter to show some help
#
# bin/rails proposals:batch:import
#
# bin/rails "proposals:batch:geoloc[[email protected],../geolocs.csv]"
#
# In heroku you must copy first the script and the files, you can use the command nc to do that (see README.md)
#
# heroku run rake "proposals:batch:geoloc[[email protected],../geolocs.csv]"
#

require_relative 'script_helpers'

namespace :proposals do
namespace :export do
task :notes, %i[component csv] => :environment do |_task, args|
proposals = Decidim::Proposals::Proposal.where(component: args.component)
notes = Decidim::Proposals::ProposalNote.where(proposal: proposals)
headers = ["Note ID", "Proposal ID", "Proposal title", "Nota", "Emails", "URL"]
CSV.open(args.csv, "wb") do |csv|
csv << headers
notes.find_each do |note|
csv << [note.id, note.proposal.id, note.proposal.title["ca"] || note.proposal.title["es"], note.body, emails_from(note.body).join("\n"), url_for_proposal(note.proposal)]
end
end
end
end
end
17 changes: 16 additions & 1 deletion script_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def show_success(msg)
end

def normalize(line)
values = { answer: {} }
values = { answer: {}, cost_report: {}, execution_period: {} }
line.each do |key, value|
case key
when /^id$/i
Expand All @@ -82,12 +82,27 @@ def normalize(line)
values[:answer][:es] = value
when /^adreça$|^dirección$|^address$/i
values[:address] = value
when /^cost$/i
values[:cost] = value
when /^informe de cost$/i
values[:cost_report][:ca] = value
when /^per[ií]ode d['’]execuci[oó]$/i
values[:execution_period][:ca] = value
end
end
raise_if_field_not_found(:id, values)
values
end

def emails_from(text)
text.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)
end

def url_for_proposal(proposal)
# "https://#{proposal.organization.host}/processes/#{proposal.participatory_space.slug}/f/#{proposal.component.id}/proposals/#{proposal.id}"
"https://www.decidim.barcelona/processes/#{proposal.participatory_space.slug}/f/#{proposal.component.id}/proposals/#{proposal.id}"
end

def raise_if_field_not_found(field, values)
raise UnprocessableError, "#{field.upcase} field not found for [#{values[:id]}]" unless values[field].present?
end
Expand Down

0 comments on commit 245dec2

Please sign in to comment.