From 12fa21885aab8f7ff47c80d8bb8717cc92c7bc05 Mon Sep 17 00:00:00 2001 From: ks0m1c_dharma Date: Thu, 22 Feb 2024 01:01:15 +0800 Subject: [PATCH 1/4] Admin Controls for Event --- config/config.exs | 2 ++ lib/vyasa/medium/event.ex | 1 - lib/vyasa/medium/store.ex | 2 +- lib/vyasa_web.ex | 3 +++ lib/vyasa_web/admin.ex | 56 +++++++++++++++++++++++++++++++++++++++ lib/vyasa_web/router.ex | 7 +++++ mix.exs | 3 ++- mix.lock | 2 ++ 8 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 lib/vyasa_web/admin.ex diff --git a/config/config.exs b/config/config.exs index 93216f60..eca12f75 100644 --- a/config/config.exs +++ b/config/config.exs @@ -31,6 +31,8 @@ config :vyasa, VyasaWeb.Endpoint, # at the `config/runtime.exs`. config :vyasa, Vyasa.Mailer, adapter: Swoosh.Adapters.Local +config :live_admin, ecto_repo: Vyasa.Repo + # Configure esbuild (the version is required) config :esbuild, version: "0.17.11", diff --git a/lib/vyasa/medium/event.ex b/lib/vyasa/medium/event.ex index 8e9822b2..dbd35628 100644 --- a/lib/vyasa/medium/event.ex +++ b/lib/vyasa/medium/event.ex @@ -1,6 +1,5 @@ defmodule Vyasa.Medium.Event do use Ecto.Schema - import Ecto.Changeset alias Vyasa.Written.{Verse, Source} diff --git a/lib/vyasa/medium/store.ex b/lib/vyasa/medium/store.ex index 42454612..191ccd49 100644 --- a/lib/vyasa/medium/store.ex +++ b/lib/vyasa/medium/store.ex @@ -70,7 +70,7 @@ defmodule Vyasa.Medium.Store do defp signer(action, path) do ExAws.Config.new(:s3, s3_config()) |> ExAws.S3.presigned_url(action, @bucket, path, - [expires_in: 88888, virtual_host: false, query_params: [{"ContentType", "application/octet-stream"}]]) + [expires_in: 88888, virtual_host: false, query_params: [{"response-content-disposition", "inline"}]]) end defp local_path(%Medium.Voice{file_path: local_path}) do diff --git a/lib/vyasa_web.ex b/lib/vyasa_web.ex index d52574a6..ebecf936 100644 --- a/lib/vyasa_web.ex +++ b/lib/vyasa_web.ex @@ -27,6 +27,9 @@ defmodule VyasaWeb do import Plug.Conn import Phoenix.Controller import Phoenix.LiveView.Router + + # Import Admin Routes + import LiveAdmin.Router end end diff --git a/lib/vyasa_web/admin.ex b/lib/vyasa_web/admin.ex new file mode 100644 index 00000000..78efe114 --- /dev/null +++ b/lib/vyasa_web/admin.ex @@ -0,0 +1,56 @@ +defmodule VyasaWeb.Admin.Written.Verse do + use LiveAdmin.Resource, schema: Vyasa.Written.Verse +end + +defmodule VyasaWeb.Admin.Medium.Event do + use LiveAdmin.Resource, schema: Vyasa.Medium.Event, immutable_fields: [:source_id], actions: [:call], render_with: :render_field + + def render_field(%{voice: v} = e, :phase, _session) do + VyasaWeb.Admin.Renderer.render_link(%{e | voice: Vyasa.Medium.Store.hydrate(v)}) + end + + def render_field(record, field, session) do + VyasaWeb.Admin.Renderer.render_field(record, field, session) + end + + + def call(%{voice: v}, _sess) do + IO.inspect(v) + {:ok, v} + end +end + + +defmodule VyasaWeb.Admin.Renderer do + use Phoenix.Component + + def render_link(%{origin: o, voice: %{file_path: _fp}} = assigns) do + assigns = %{assigns | origin: floor(o/1000)} + ~H""" + <%= @phase %> + + """ + end + + def render_field(record, field, _session) do + record + |> Map.fetch!(field) + |> case do + bool when is_boolean(bool) -> + if bool, do: "Yes", else: "No" + date = %Date{} -> + Calendar.strftime(date, "%a, %B %d %Y") + bin when is_binary(bin) -> bin + _ -> + record + |> Map.fetch!(field) + |> case do + val when is_binary(val) -> val + val -> inspect(val, pretty: true) + end + end + end + +end diff --git a/lib/vyasa_web/router.ex b/lib/vyasa_web/router.ex index 83c711f3..c7ecc55b 100644 --- a/lib/vyasa_web/router.ex +++ b/lib/vyasa_web/router.ex @@ -1,6 +1,7 @@ defmodule VyasaWeb.Router do use VyasaWeb, :router + pipeline :browser do plug :accepts, ["html"] plug CORSPlug, origin: ["https://www.youtube.com/iframe_api"] @@ -33,6 +34,12 @@ defmodule VyasaWeb.Router do live "/explore/:source_title/:chap_no", SourceLive.Chapter.Index, :index live "/explore/:source_title/:chap_no/:verse_no", SourceLive.Chapter.ShowVerse, :show end + + live_admin "/admin" do + admin_resource "/verses", VyasaWeb.Admin.Written.Verse + admin_resource "/events", VyasaWeb.Admin.Medium.Event + end + live "/gita/", GitaLive.Index, :index live "/gita/:chapter_id", GitaLive.Show, :show live "/gita/:chapter_id/:verse_id", GitaLive.ShowVerse, :show_verse diff --git a/mix.exs b/mix.exs index 3ec5ce8f..16c3a58b 100644 --- a/mix.exs +++ b/mix.exs @@ -59,7 +59,8 @@ defmodule Vyasa.MixProject do {:kino, "~> 0.12.0"}, {:cors_plug, "~> 3.0"}, {:ex_aws, "~> 2.0"}, - {:ex_aws_s3, "~> 2.5"} + {:ex_aws_s3, "~> 2.5"}, + {:live_admin, "~> 0.11.4"} ] end diff --git a/mix.lock b/mix.lock index 1296d31e..fe166cc3 100644 --- a/mix.lock +++ b/mix.lock @@ -28,6 +28,7 @@ "image": {:hex, :image, "0.39.0", "c003ce095ee32c40f194ca1a48ffa6d3e67192e567183e70dfd8b77bb7144119", [:mix], [{:bumblebee, "~> 0.3", [hex: :bumblebee, repo: "hexpm", optional: true]}, {:evision, "~> 0.1.33", [hex: :evision, repo: "hexpm", optional: true]}, {:exla, "~> 0.5", [hex: :exla, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: true]}, {:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.14 or ~> 3.2", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: true]}, {:rustler, "> 0.0.0", [hex: :rustler, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: false]}, {:vix, "~> 0.23", [hex: :vix, repo: "hexpm", optional: false]}], "hexpm", "d83de26e009a59cb8c53d2efecaacd7c9b990897dc3c7095a5b8847e73c8f968"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "kino": {:hex, :kino, "0.12.0", "dfebe415f31cf2f54d98600178c08fd017931d9dada6fb59b7716b5431bee4a1", [:mix], [{:fss, "~> 0.1.0", [hex: :fss, repo: "hexpm", optional: false]}, {:nx, "~> 0.1", [hex: :nx, repo: "hexpm", optional: true]}, {:table, "~> 0.1.2", [hex: :table, repo: "hexpm", optional: false]}], "hexpm", "6796a64ae978fc2e50e52cae022ae6a3d28b8e50589af158b8f3904e7f748377"}, + "live_admin": {:hex, :live_admin, "0.11.4", "d4e964ac99d215305d2d9f4af941a334d8e8edd19c53d589ebf3d53b7f079fea", [:mix], [{:ecto, "~> 3.10", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:gettext, "~> 0.22", [hex: :gettext, repo: "hexpm", optional: false]}, {:phoenix_ecto, "~> 4.4", [hex: :phoenix_ecto, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.20.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}], "hexpm", "c09ae7669b6b6e85bb03af19436c035aaf01e49f4ef7966d0e0c994c651252c9"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, @@ -43,6 +44,7 @@ "phoenix_live_view": {:hex, :phoenix_live_view, "0.20.1", "92a37acf07afca67ac98bd326532ba8f44ad7d4bdf3e4361b03f7f02594e5ae9", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "be494fd1215052729298b0e97d5c2ce8e719c00854b82cd8cf15c1cd7fcf6294"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"}, "phoenix_template": {:hex, :phoenix_template, "1.0.3", "32de561eefcefa951aead30a1f94f1b5f0379bc9e340bb5c667f65f1edfa4326", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "16f4b6588a4152f3cc057b9d0c0ba7e82ee23afa65543da535313ad8d25d8e2c"}, + "phoenix_view": {:hex, :phoenix_view, "2.0.3", "4d32c4817fce933693741deeb99ef1392619f942633dde834a5163124813aad3", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}], "hexpm", "cd34049af41be2c627df99cd4eaa71fc52a328c0c3d8e7d4aa28f880c30e7f64"}, "plug": {:hex, :plug, "1.15.1", "b7efd81c1a1286f13efb3f769de343236bd8b7d23b4a9f40d3002fc39ad8f74c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "459497bd94d041d98d948054ec6c0b76feacd28eec38b219ca04c0de13c79d30"}, "plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"}, "plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"}, From aac5821394e0cd370bd8edc40c579e8ba6fd7fe6 Mon Sep 17 00:00:00 2001 From: ks0m1c_dharma Date: Thu, 22 Feb 2024 19:29:47 +0800 Subject: [PATCH 2/4] Update AdminView --- lib/vyasa/medium/store.ex | 2 +- lib/vyasa_web/admin.ex | 33 ++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/vyasa/medium/store.ex b/lib/vyasa/medium/store.ex index 191ccd49..42454612 100644 --- a/lib/vyasa/medium/store.ex +++ b/lib/vyasa/medium/store.ex @@ -70,7 +70,7 @@ defmodule Vyasa.Medium.Store do defp signer(action, path) do ExAws.Config.new(:s3, s3_config()) |> ExAws.S3.presigned_url(action, @bucket, path, - [expires_in: 88888, virtual_host: false, query_params: [{"response-content-disposition", "inline"}]]) + [expires_in: 88888, virtual_host: false, query_params: [{"ContentType", "application/octet-stream"}]]) end defp local_path(%Medium.Voice{file_path: local_path}) do diff --git a/lib/vyasa_web/admin.ex b/lib/vyasa_web/admin.ex index 78efe114..82b0dd92 100644 --- a/lib/vyasa_web/admin.ex +++ b/lib/vyasa_web/admin.ex @@ -3,20 +3,21 @@ defmodule VyasaWeb.Admin.Written.Verse do end defmodule VyasaWeb.Admin.Medium.Event do - use LiveAdmin.Resource, schema: Vyasa.Medium.Event, immutable_fields: [:source_id], actions: [:call], render_with: :render_field + use LiveAdmin.Resource, schema: Vyasa.Medium.Event, + immutable_fields: [:source_id], + actions: [:silence], + render_with: :render_field + - def render_field(%{voice: v} = e, :phase, _session) do - VyasaWeb.Admin.Renderer.render_link(%{e | voice: Vyasa.Medium.Store.hydrate(v)}) - end def render_field(record, field, session) do VyasaWeb.Admin.Renderer.render_field(record, field, session) end - def call(%{voice: v}, _sess) do - IO.inspect(v) - {:ok, v} + def silence(%{voice: _v} = e, _sess) do + e = %{e | voice: nil} + {:ok, e} end end @@ -24,17 +25,27 @@ end defmodule VyasaWeb.Admin.Renderer do use Phoenix.Component - def render_link(%{origin: o, voice: %{file_path: _fp}} = assigns) do - assigns = %{assigns | origin: floor(o/1000)} + def render_field(%{origin: o, voice: %Vyasa.Medium.Voice{} = v} = assigns, :phase, _session) do + assigns = %{assigns | origin: floor(o/1000), voice: Vyasa.Medium.Store.hydrate(v)} ~H""" <%= @phase %> -