Add creating and basic editing of translation

This commit is contained in:
Thelonius Kort
2023-01-04 14:42:13 +01:00
parent f4b5abef5a
commit d589d84b40
15 changed files with 235 additions and 47 deletions

View File

@ -9,9 +9,9 @@ defmodule Outlook.InternalTree do
|> Html.to_html()
end
def render_html_preview(tree) do
def render_html_preview(tree, target \\ "1") do
tree
|> Html.to_html_preview("1")
|> Html.to_html_preview(target)
end
require Logger

View File

@ -35,7 +35,10 @@ defmodule Outlook.Translations do
** (Ecto.NoResultsError)
"""
def get_translation!(id), do: Repo.get!(Translation, id)
def get_translation!(id) do
Repo.get!(Translation, id)
|> Repo.preload([:article])
end
@doc """
Creates a translation.

View File

@ -0,0 +1,25 @@
defmodule Outlook.Translations.Basic do
alias Outlook.InternalTree.InternalNode
alias Outlook.InternalTree.TranslationUnit
def internal_tree_to_tunit_map(tree) do
collect_translation_units(tree)
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end)
|> Enum.into(%{})
end
defp collect_translation_units([%InternalNode{type: :element} = node | rest]) do
collect_translation_units(node.content) ++ collect_translation_units(rest)
end
defp collect_translation_units([%TranslationUnit{} = tunit | rest]) do
[tunit | collect_translation_units(rest)]
end
defp collect_translation_units([_|rest]) do
[] ++ collect_translation_units(rest)
end
defp collect_translation_units([]), do: []
end

View File

@ -24,7 +24,7 @@ defmodule Outlook.Translations.Translation do
def changeset(translation, attrs) do
translation
|> cast(attrs, [:lang, :title, :teaser, :date, :public, :unauthorized, :article_id])
|> cast(attrs, [:content], force_changes: true)
|> cast(attrs, [:content])
|> validate_required([:lang, :title, :content, :date, :public, :unauthorized, :article_id])
|> unique_constraint([:lang, :article_id],
message: "translation for this language already exists",