Skip to content

Commit

Permalink
add link parser
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Mar 1, 2021
1 parent e369188 commit 48b3721
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
75 changes: 58 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,68 @@ o.host="localhost"
o.save
```

1: Importing massive answers for proposals
------------------------------------------
Importing massive answers for proposals
---------------------------------------

This script answers a list of proposals, it requires an excel with headers with this format:

```
ID, status, text_ca, text_es, ...
123, accepted, una resposta, una respuesta, ...
```

or

```
id, state, answer/ca, answer/es, ...
123, acceptada, una resposta, una respuesta, ...
```

> NOTE:
> - Component setting "Publish proposal answers immediately" is honored, so, if activated, emails and notifications will be sent. Otherwise will just introduce the answers in the database.
> - Answers are are parsed to highlight links automatically and convert line breaks to `<br>`;
### Usage:

Copy the script into your `lib/tasks` of your Decidim installation:

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

Run in local (as testing):

```
bin/rails "proposals:batch:answer[[email protected],./pam-ciutat.csv]"
bin/rails "proposals:batch:answer[[email protected],./proposal-answers.csv]"
```

Massively geolocating proposals
-------------------------------

This script geolocates a list of proposals, it requires an excel with headers with this format:

```
ID, address, ...
123, some place, ...
```

or

```
i, adreça, ...
123, some place, ...
```

> NOTE:
> - Component setting "Geocoding enabled" is honored, so, if not activated, proposals won't be modified.
```
bin/rails proposals:batch:geoloc[[email protected],./proposal-geolocs.csv]
```

Running in heroku:
------------------

In Heroku you need to temporary copy the relevant files into a running container first.
As there's no "copy" command to do that you can use the service https://transfer.sh with this trick:
Expand All @@ -58,17 +103,15 @@ 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 pam-ciutat.csv https://transfer.sh/pam-ciutat.csv -H "Max-Days: 1"
curl --upload-file pam-districtes.csv https://transfer.sh/pam-districte.csv -H "Max-Days: 1"
curl --upload-file geolocs.csv https://transfer.sh/geolocs.csv -H "Max-Days: 1"
curl --upload-file proposal-answerss.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"
```

Which will return (for instance) the download addresses:

```
https://transfer.sh/23lG7/pam-ciutat.csv
https://transfer.sh/YwYAN/pam-districte.csv
https://transfer.sh/x3hUa/geolocs.csv
https://transfer.sh/YwYAN/proposal-answers.csv
https://transfer.sh/x3hUa/proposal-geolocs.csv
```

2. Login into a shell session in heroku
Expand All @@ -80,15 +123,14 @@ heroku run bash
3. Download the files inside the dyno:

```
wget https://transfer.sh/23lG7/pam-ciutat.csv
wget https://transfer.sh/YwYAN/pam-districte.csv
wget https://transfer.sh/x3hUa/golocs.csv
wget https://transfer.sh/YwYAN/proposal-answers.csv
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.2.tar.gz | tar --transform 's/^decidim-scripts-0.2//' -xvz -C lib/tasks
wget -qO- https://github.com/Platoniq/decidim-scripts/archive/0.3.tar.gz | tar --transform 's/^decidim-scripts-0.3//' -xvz -C lib/tasks
```

5. Run the script inside the shell session (2nd terminal):
Expand All @@ -101,9 +143,8 @@ bin/rails proposals:batch:geoloc
or:

```
bin/rails proposals:batch:answer[[email protected],./pam-ciutat.csv]
bin/rails proposals:batch:answer[[email protected],./pam-districtes.csv]
bin/rails proposals:batch:geoloc[[email protected],./geolocs.csv]
bin/rails proposals:batch:answer[[email protected],./proposal-answerss.csv]
bin/rails proposals:batch:geoloc[[email protected],./proposal-geolocs.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.
1 change: 1 addition & 0 deletions proposal_answers.rake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace :proposals do
raise_if_field_not_found(:state, values)
raise_if_field_not_found(:answer, values)
values[:state] = normalize_state(values[:state]) # throws UnprocessableError if fails
values[:answer] = parse_links(values[:answer])
@admin = admin
@values = values
@proposal = proposal_from_id(values[:id])
Expand Down
6 changes: 6 additions & 0 deletions script_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def normalize_state(state)
end
end

def parse_links(texts)
texts.map do |lang, text|
[lang, Decidim::ContentRenderers::LinkRenderer.new(text.strip).render.gsub("\n", "<br>")]
end.to_h
end

def show_help
puts HELP_TEXT
end
Expand Down

0 comments on commit 48b3721

Please sign in to comment.