Export participants to different formats

* CSV
* PDF (requires Python dependency `weasyprint`)
* ODS (requires Python dependency `pyexcel_ods3`)

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-10-04 18:59:41 +02:00
parent 5dd24e1c9e
commit 0c667b13ae
121 changed files with 10817 additions and 6872 deletions

View File

@@ -1,5 +1,5 @@
defmodule Mobilizon.Service.ICalendarTest do
use Mobilizon.DataCase
use Mobilizon.DataCase, async: true
import Mobilizon.Factory

View File

@@ -0,0 +1,28 @@
defmodule Mobilizon.Service.Export.Participants.CommonTest do
use Mobilizon.DataCase, async: true
import Mobilizon.Factory
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Export.Participants.Common
test "convert participants to list items" do
participant = insert(:participant)
actor = insert(:actor)
name = Actor.display_name_and_username(actor)
assert [^name, _, ""] = Common.to_list({participant, actor})
end
test "convert participants with metadata to list items" do
participant = insert(:participant, metadata: %{message: "a message"})
actor = insert(:actor)
name = Actor.display_name_and_username(actor)
assert [^name, _, "a message"] = Common.to_list({participant, actor})
end
test "convert anonymous participants to list items" do
participant = insert(:participant)
actor = insert(:actor, domain: nil, preferred_username: "anonymous")
assert ["Anonymous participant", _, ""] = Common.to_list({participant, actor})
end
end

View File

@@ -0,0 +1,22 @@
defmodule Mobilizon.Service.Export.Participants.CSVTest do
use Mobilizon.DataCase
import Mobilizon.Factory
alias Mobilizon.Events.Event
alias Mobilizon.Service.Export.Participants.CSV
describe "export event participants to csv" do
test "export basic infos" do
%Event{} = event = insert(:event)
insert(:participant, event: event, role: :creator)
insert(:participant, event: event, role: :participant)
insert(:participant, event: event, role: :not_approved)
assert CSV.ready?()
assert {:ok, path} = CSV.export(event)
assert content = File.read!("uploads/exports/csv/" <> path)
assert content =~ "Participant name,Participant status,Participant message"
end
end
end

View File

@@ -0,0 +1,62 @@
defmodule Mobilizon.Service.Export.Participants.ODSTest do
use Mobilizon.DataCase
import Mobilizon.Factory
alias Mobilizon.Events.Event
alias Mobilizon.Service.Export.Participants.ODS
setup do
test_format?()
end
describe "export event participants to ods" do
test "export basic infos" do
%Event{} = event = insert(:event)
insert(:participant, event: event, role: :creator)
insert(:participant, event: event, role: :participant)
insert(:participant, event: event, role: :not_approved)
set_exports(Mobilizon.Service.Export.Participants.CSV)
refute ODS.ready?()
end
test "enable the exporter" do
set_exports(Mobilizon.Service.Export.Participants.ODS)
%Event{} = event = insert(:event)
insert(:participant, event: event, role: :creator)
insert(:participant, event: event, role: :participant)
insert(:participant, event: event, role: :not_approved)
assert ODS.dependencies_ok?()
assert ODS.enabled?()
assert {:ok, path} = ODS.export(event)
assert File.exists?("uploads/exports/ods/" <> path)
set_exports(Mobilizon.Service.Export.Participants.CSV)
end
end
@spec set_exports(module()) :: :ok
defp set_exports(module) do
Mobilizon.Config.put(:exports,
formats: [module]
)
end
@spec test_format? :: :ok | {:ok, skip: true}
defp test_format? do
case System.get_env("EXPORT_FORMATS") do
nil ->
{:ok, skip: true}
formats ->
if "ods" in String.split(formats, ",") do
:ok
else
{:ok, skip: true}
end
end
end
end

View File

@@ -0,0 +1,62 @@
defmodule Mobilizon.Service.Export.Participants.PDFTest do
use Mobilizon.DataCase
import Mobilizon.Factory
alias Mobilizon.Events.Event
alias Mobilizon.Service.Export.Participants.PDF
setup do
test_format?()
end
describe "export event participants to PDF" do
test "export basic infos" do
%Event{} = event = insert(:event)
insert(:participant, event: event, role: :creator)
insert(:participant, event: event, role: :participant)
insert(:participant, event: event, role: :not_approved)
set_exports(Mobilizon.Service.Export.Participants.CSV)
refute PDF.ready?()
end
test "enable the exporter" do
set_exports(Mobilizon.Service.Export.Participants.PDF)
%Event{} = event = insert(:event)
insert(:participant, event: event, role: :creator)
insert(:participant, event: event, role: :participant)
insert(:participant, event: event, role: :not_approved)
assert PDF.dependencies_ok?()
assert PDF.enabled?()
assert {:ok, path} = PDF.export(event)
assert File.exists?("uploads/exports/pdf/" <> path)
set_exports(Mobilizon.Service.Export.Participants.CSV)
end
end
@spec set_exports(module()) :: :ok
defp set_exports(module) do
Mobilizon.Config.put(:exports,
formats: [module]
)
end
@spec test_format? :: :ok | {:ok, skip: true}
defp test_format? do
case System.get_env("EXPORT_FORMATS") do
nil ->
{:ok, skip: true}
formats ->
if "pdf" in String.split(formats, ",") do
:ok
else
{:ok, skip: true}
end
end
end
end