Fix CSV upload file reading
Handle consume_uploaded_entries returning [content] directly
instead of [{:ok, content}]. Add locale support for translations
in background tasks.
This commit is contained in:
parent
562265f212
commit
5acb5e304d
1 changed files with 25 additions and 7 deletions
|
|
@ -59,9 +59,13 @@ defmodule MvWeb.GlobalSettingsLive do
|
||||||
@max_errors 50
|
@max_errors 50
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, session, socket) do
|
||||||
{:ok, settings} = Membership.get_settings()
|
{:ok, settings} = Membership.get_settings()
|
||||||
|
|
||||||
|
# Get locale from session for translations
|
||||||
|
locale = session["locale"] || "de"
|
||||||
|
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||||
|
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, gettext("Settings"))
|
|> assign(:page_title, gettext("Settings"))
|
||||||
|
|
@ -70,6 +74,7 @@ defmodule MvWeb.GlobalSettingsLive do
|
||||||
|> assign(:import_state, nil)
|
|> assign(:import_state, nil)
|
||||||
|> assign(:import_progress, nil)
|
|> assign(:import_progress, nil)
|
||||||
|> assign(:import_status, :idle)
|
|> assign(:import_status, :idle)
|
||||||
|
|> assign(:locale, locale)
|
||||||
|> assign_form()
|
|> assign_form()
|
||||||
# Configure file upload with auto-upload enabled
|
# Configure file upload with auto-upload enabled
|
||||||
# Files are uploaded automatically when selected, no need for manual trigger
|
# Files are uploaded automatically when selected, no need for manual trigger
|
||||||
|
|
@ -586,6 +591,10 @@ defmodule MvWeb.GlobalSettingsLive do
|
||||||
actor: actor
|
actor: actor
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Get locale from socket for translations in background tasks
|
||||||
|
locale = socket.assigns[:locale] || "de"
|
||||||
|
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||||
|
|
||||||
if Config.sql_sandbox?() do
|
if Config.sql_sandbox?() do
|
||||||
# Run synchronously in tests to avoid Ecto Sandbox issues with async tasks
|
# Run synchronously in tests to avoid Ecto Sandbox issues with async tasks
|
||||||
{:ok, chunk_result} =
|
{:ok, chunk_result} =
|
||||||
|
|
@ -604,6 +613,9 @@ defmodule MvWeb.GlobalSettingsLive do
|
||||||
# Use start_child for fire-and-forget: no monitor, no Task messages
|
# Use start_child for fire-and-forget: no monitor, no Task messages
|
||||||
# We only use our own send/2 messages for communication
|
# We only use our own send/2 messages for communication
|
||||||
Task.Supervisor.start_child(Mv.TaskSupervisor, fn ->
|
Task.Supervisor.start_child(Mv.TaskSupervisor, fn ->
|
||||||
|
# Set locale in task process for translations
|
||||||
|
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||||
|
|
||||||
{:ok, chunk_result} =
|
{:ok, chunk_result} =
|
||||||
MemberCSV.process_chunk(
|
MemberCSV.process_chunk(
|
||||||
chunk,
|
chunk,
|
||||||
|
|
@ -674,13 +686,19 @@ defmodule MvWeb.GlobalSettingsLive do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp consume_and_read_csv(socket) do
|
defp consume_and_read_csv(socket) do
|
||||||
|
result =
|
||||||
consume_uploaded_entries(socket, :csv_file, fn %{path: path}, _entry ->
|
consume_uploaded_entries(socket, :csv_file, fn %{path: path}, _entry ->
|
||||||
case File.read(path) do
|
case File.read(path) do
|
||||||
{:ok, content} -> {:ok, content}
|
{:ok, content} -> {:ok, content}
|
||||||
{:error, reason} -> {:error, Exception.message(reason)}
|
{:error, reason} -> {:error, Exception.message(reason)}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
result
|
||||||
|> case do
|
|> case do
|
||||||
|
[content] when is_binary(content) ->
|
||||||
|
{:ok, content}
|
||||||
|
|
||||||
[{:ok, content}] when is_binary(content) ->
|
[{:ok, content}] when is_binary(content) ->
|
||||||
{:ok, content}
|
{:ok, content}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue