-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f20a498
commit 245dec2
Showing
4 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[[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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters