diff --git a/README.md b/README.md index c1db91e..62ca17c 100644 --- a/README.md +++ b/README.md @@ -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): @@ -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" ``` @@ -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): @@ -145,6 +145,7 @@ or: ``` bin/rails proposals:batch:answer[admin@example.org,./proposal-answerss.csv] bin/rails proposals:batch:geoloc[admin@example.org,./proposal-geolocs.csv] +bin/rails proposals:batch:cost[admin@example.org,./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. \ No newline at end of file diff --git a/proposal_costs.rake b/proposal_costs.rake new file mode 100644 index 0000000..57fafb3 --- /dev/null +++ b/proposal_costs.rake @@ -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[admin@example.org,../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[admin@example.org,../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 diff --git a/proposal_notes.rake b/proposal_notes.rake new file mode 100644 index 0000000..52dd916 --- /dev/null +++ b/proposal_notes.rake @@ -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[admin@example.org,../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[admin@example.org,../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 \ No newline at end of file diff --git a/script_helpers.rb b/script_helpers.rb index 3f98f50..a152c85 100644 --- a/script_helpers.rb +++ b/script_helpers.rb @@ -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 @@ -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