From fe9abc6898763c961e45472cfa1d9918b69e26e4 Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Tue, 21 Jan 2020 11:20:53 +0700 Subject: [PATCH 1/3] add import export feature --- lib/chubi/content_file_store.ex | 121 ++++++++++++++++++ lib/chubi_web.ex | 4 +- lib/chubi_web/admin_router.ex | 45 +++++++ .../controllers/admin/.#setting_controller.ex | 1 + .../controllers/admin/category_controller.ex | 6 +- .../controllers/admin/page_controller.ex | 6 +- .../controllers/admin/post_controller.ex | 6 +- .../controllers/admin/setting_controller.ex | 67 +++++++++- .../controllers/admin/tag_controller.ex | 6 +- lib/chubi_web/router.ex | 14 +- .../templates/admin/category/edit.html.eex | 4 +- .../templates/admin/category/index.html.eex | 8 +- .../templates/admin/category/new.html.eex | 4 +- .../templates/admin/category/show.html.eex | 4 +- .../templates/admin/layout/_header.html.eex | 2 +- .../admin/layout/_navigation.html.eex | 14 +- .../templates/admin/page/edit.html.eex | 2 +- .../templates/admin/page/index.html.eex | 8 +- .../templates/admin/page/new.html.eex | 2 +- .../templates/admin/page/show.html.eex | 6 +- .../templates/admin/post/_sidebar.html.eex | 8 +- .../templates/admin/post/edit.html.eex | 2 +- .../templates/admin/post/index.html.eex | 6 +- .../templates/admin/post/new.html.eex | 2 +- .../templates/admin/post/show.html.eex | 6 +- .../templates/admin/tag/edit.html.eex | 4 +- .../templates/admin/tag/index.html.eex | 8 +- .../templates/admin/tag/new.html.eex | 4 +- .../templates/admin/tag/show.html.eex | 4 +- test.zip | Bin 0 -> 121 bytes .../admin/page/page_controller_test.exs | 28 ++-- 31 files changed, 315 insertions(+), 87 deletions(-) create mode 100644 lib/chubi/content_file_store.ex create mode 100644 lib/chubi_web/admin_router.ex create mode 120000 lib/chubi_web/controllers/admin/.#setting_controller.ex create mode 100644 test.zip diff --git a/lib/chubi/content_file_store.ex b/lib/chubi/content_file_store.ex new file mode 100644 index 0000000..3af5cba --- /dev/null +++ b/lib/chubi/content_file_store.ex @@ -0,0 +1,121 @@ +defmodule Chubi.ContentFileStore do + alias Chubi.Content.Post + alias Chubi.Content.Page + @content_dir "./priv/content" + + def content_directory(suffix \\ "") do + @content_dir + |> Path.join(suffix) + |> Path.absname() + end + + def ensure_directory(path) do + if File.dir?(path) do + path + else + File.mkdir_p!(path) + path + end + end + + defp directory(post) do + case post do + %Post{} -> + ensure_directory(content_directory("posts")) + + %Page{} -> + ensure_directory(content_directory("pages")) + + _ -> + nil + end + end + + defp extension(post) do + case post.format do + "markdown" -> "md" + _ -> "json" + end + end + + defp content(%Post{} = post) do + case post.format do + "markdown" -> + post.content + + _ -> + post + |> Map.drop([:__meta__, :__struct__]) + |> Map.merge(%{ + categories: Enum.map(post.categories, & &1.name), + tags: Enum.map(post.tags, & &1.name) + }) + |> Jason.encode!() + end + end + + defp content(%Page{} = post) do + case post.format do + "markdown" -> + post.content + + _ -> + post + |> Map.drop([:__meta__, :__struct__]) + |> Jason.encode!() + end + end + + defp file_name(post) do + date = Timex.format!(post.date || post.inserted_at, "{YYYY}-{0M}-{0D}") + "#{date}-#{post.slug}.#{extension(post)}" + end + + defp file_path(post) do + Path.join(directory(post), file_name(post)) + end + + def write(post) do + File.write(file_path(post), content(post), [:write]) + end + + def write_mem(post) do + {file_name(post), content(post)} + end + + def delete(post) do + File.rm(file_path(post)) + end + + def read_all_posts() do + ensure_directory(content_directory("/posts")) + |> read_directory + end + + def read_all_pages() do + ensure_directory(content_directory("/pages")) + |> read_directory + end + + def read_directory(path) do + with {:ok, files} <- File.ls(path) do + Enum.map(files, fn file -> + Path.join(path, file) + |> read_file + end) + |> Enum.filter(&(not is_nil(&1))) + else + _ -> [] + end + end + + def read_file(file_path) do + with {:ok, content} <- File.read(file_path) do + case Path.extname(file_path) do + ".md" -> %{"content" => content, "format" => "markdown"} + ".json" -> Jason.decode!(content) + _ -> nil + end + end + end +end diff --git a/lib/chubi_web.ex b/lib/chubi_web.ex index e619b37..2bc91d6 100644 --- a/lib/chubi_web.ex +++ b/lib/chubi_web.ex @@ -57,7 +57,7 @@ defmodule ChubiWeb do import Plug.Conn import ChubiWeb.Gettext - alias ChubiWeb.Router.Helpers, as: Routes + alias ChubiWeb.AdminRouter.Helpers, as: Routes end end @@ -86,7 +86,7 @@ defmodule ChubiWeb do import ChubiWeb.Admin.InputHelpers import ChubiWeb.Admin.ViewHelpers import ChubiWeb.Gettext - alias ChubiWeb.Router.Helpers, as: Routes + alias ChubiWeb.AdminRouter.Helpers, as: Routes end end diff --git a/lib/chubi_web/admin_router.ex b/lib/chubi_web/admin_router.ex new file mode 100644 index 0000000..a38509a --- /dev/null +++ b/lib/chubi_web/admin_router.ex @@ -0,0 +1,45 @@ +defmodule ChubiWeb.AdminRouter do + use ChubiWeb, :router + + pipeline :browser do + plug(:accepts, ["html"]) + plug(:fetch_session) + plug(:fetch_flash) + plug(:protect_from_forgery) + plug(:put_secure_browser_headers) + end + + pipeline :admin do + plug(:put_layout, {ChubiWeb.Admin.LayoutView, "app.html"}) + plug(BasicAuth, use_config: {:chubi, :auth_config}) + plug(ChubiWeb.Plugs.PutSiteSetting) + plug(ChubiWeb.Plugs.LoadSiteParams) + end + + pipeline :preview do + plug(BasicAuth, use_config: {:chubi, :auth_config}) + end + + pipeline :app do + plug(ChubiWeb.Plugs.PutSiteSetting) + plug(ChubiWeb.Plugs.LoadSiteParams) + # put this at end of pipeline + plug(ChubiWeb.Plugs.PutSiteLayout) + end + + scope "/", ChubiWeb.Admin, path: "/admin" do + pipe_through([:browser, :admin]) + get("/", PostController, :index) + resources("/tags", TagController) + resources("/categories", CategoryController) + resources("/posts", PostController) + resources("/pages", PageController) + + post("/upload", UploadController, :create) + delete("/upload", UploadController, :delete) + + get("/settings/set_theme", SettingController, :set_theme) + get("/settings/export", SettingController, :export_content) + post("/settings/import", SettingController, :import_content) + end +end diff --git a/lib/chubi_web/controllers/admin/.#setting_controller.ex b/lib/chubi_web/controllers/admin/.#setting_controller.ex new file mode 120000 index 0000000..fa2b3dd --- /dev/null +++ b/lib/chubi_web/controllers/admin/.#setting_controller.ex @@ -0,0 +1 @@ +flex@Thes-MacBook-Pro.local.769 \ No newline at end of file diff --git a/lib/chubi_web/controllers/admin/category_controller.ex b/lib/chubi_web/controllers/admin/category_controller.ex index 2ecbf67..a8a7bec 100644 --- a/lib/chubi_web/controllers/admin/category_controller.ex +++ b/lib/chubi_web/controllers/admin/category_controller.ex @@ -19,7 +19,7 @@ defmodule ChubiWeb.Admin.CategoryController do {:ok, category} -> conn |> put_flash(:info, "Category created successfully.") - |> redirect(to: Routes.admin_category_path(conn, :show, category)) + |> redirect(to: Routes.category_path(conn, :show, category)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) @@ -44,7 +44,7 @@ defmodule ChubiWeb.Admin.CategoryController do {:ok, category} -> conn |> put_flash(:info, "Category updated successfully.") - |> redirect(to: Routes.admin_category_path(conn, :show, category)) + |> redirect(to: Routes.category_path(conn, :show, category)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "edit.html", category: category, changeset: changeset) @@ -57,6 +57,6 @@ defmodule ChubiWeb.Admin.CategoryController do conn |> put_flash(:info, "Category deleted successfully.") - |> redirect(to: Routes.admin_category_path(conn, :index)) + |> redirect(to: Routes.category_path(conn, :index)) end end diff --git a/lib/chubi_web/controllers/admin/page_controller.ex b/lib/chubi_web/controllers/admin/page_controller.ex index 6824758..4602130 100644 --- a/lib/chubi_web/controllers/admin/page_controller.ex +++ b/lib/chubi_web/controllers/admin/page_controller.ex @@ -27,7 +27,7 @@ defmodule ChubiWeb.Admin.PageController do {:ok, page} -> conn |> put_flash(:info, "Page created successfully.") - |> redirect(to: Routes.admin_page_path(conn, :show, page)) + |> redirect(to: Routes.page_path(conn, :show, page)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) @@ -54,7 +54,7 @@ defmodule ChubiWeb.Admin.PageController do {:ok, page} -> conn |> put_flash(:info, "Page updated successfully.") - |> redirect(to: Routes.admin_page_path(conn, :show, page)) + |> redirect(to: Routes.page_path(conn, :show, page)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "edit.html", page: page, changeset: changeset) @@ -67,6 +67,6 @@ defmodule ChubiWeb.Admin.PageController do conn |> put_flash(:info, "Page deleted successfully.") - |> redirect(to: Routes.admin_page_path(conn, :index)) + |> redirect(to: Routes.page_path(conn, :index)) end end diff --git a/lib/chubi_web/controllers/admin/post_controller.ex b/lib/chubi_web/controllers/admin/post_controller.ex index a8cf3a8..7977ed6 100644 --- a/lib/chubi_web/controllers/admin/post_controller.ex +++ b/lib/chubi_web/controllers/admin/post_controller.ex @@ -37,7 +37,7 @@ defmodule ChubiWeb.Admin.PostController do {:ok, post} -> conn |> put_flash(:info, "Post created successfully.") - |> redirect(to: Routes.admin_post_path(conn, :show, post)) + |> redirect(to: Routes.post_path(conn, :show, post)) {:error, %Ecto.Changeset{} = changeset} -> conn @@ -75,7 +75,7 @@ defmodule ChubiWeb.Admin.PostController do {:ok, post} -> conn |> put_flash(:info, "Post updated successfully.") - |> redirect(to: Routes.admin_post_path(conn, :show, post)) + |> redirect(to: Routes.post_path(conn, :show, post)) {:error, %Ecto.Changeset{} = changeset} -> conn @@ -90,7 +90,7 @@ defmodule ChubiWeb.Admin.PostController do conn |> put_flash(:info, "Post deleted successfully.") - |> redirect(to: Routes.admin_post_path(conn, :index)) + |> redirect(to: Routes.post_path(conn, :index)) end defp put_meta(conn) do diff --git a/lib/chubi_web/controllers/admin/setting_controller.ex b/lib/chubi_web/controllers/admin/setting_controller.ex index f7cce3f..8b3ea91 100644 --- a/lib/chubi_web/controllers/admin/setting_controller.ex +++ b/lib/chubi_web/controllers/admin/setting_controller.ex @@ -1,9 +1,74 @@ defmodule ChubiWeb.Admin.SettingController do use ChubiWeb, :controller + alias Chubi.Content def set_theme(conn, %{"theme" => theme}) do conn |> put_session(:theme, theme) - |> redirect(to: Routes.admin_post_path(conn, :index)) + |> redirect(to: Routes.post_path(conn, :index)) + end + + def export_content(conn, _params) do + posts = + Content.list_posts() + |> Enum.map(&Chubi.ContentFileStore.write_mem(&1)) + |> Enum.map(fn {file_name, content} -> + {String.to_charlist(Path.join("posts/", file_name)), content} + end) + + pages = + Content.list_pages() + |> Enum.map(&Chubi.ContentFileStore.write_mem(&1)) + |> Enum.map(fn {file_name, content} -> + {String.to_charlist(Path.join("pages/", file_name)), content} + end) + + with {:ok, {filename, data}} = :zip.create("blog-content.zip", posts ++ pages, [:memory]) do + # download file + conn + |> Plug.Conn.put_resp_content_type("application/octet-stream") + |> Plug.Conn.put_resp_header( + "content-disposition", + "attachment; filename=\"#{filename}\"" + ) + |> Plug.Conn.send_resp(:ok, data) + else + _ -> + conn + |> put_flash(:error, gettext("Cannot generate template")) + |> redirect(to: Routes.post_path(conn, :index)) + end + end + + def select_import(conn, _) do + end + + def import_content(conn, %{%{path: path} => file}) do + with {:ok, files} <- :zip.extract(path, [:memory]) do + rs = + Enum.map(files, fn {_filename, content} -> + attrs = + case Path.extname(file_path) do + ".md" -> %{"content" => content, "format" => "markdown"} + _ -> Jason.decode!(content) + end + + if String.starts_with?(file_path, "posts") do + Content.create_post(attrs) + else + Content.create_page(attrs) + end + end) + + success = Enum.filter(rs, &(elem(&1, 0) == :ok)) + + conn + |> put_flash(:error, gettext("Cannot import content")) + |> redirect(to: Routes.setting_path(conn, :select_import)) + else + conn + |> put_flash(:error, gettext("Cannot import content")) + |> redirect(to: Routes.setting_path(conn, :select_import)) + end end end diff --git a/lib/chubi_web/controllers/admin/tag_controller.ex b/lib/chubi_web/controllers/admin/tag_controller.ex index 004270e..236d20f 100644 --- a/lib/chubi_web/controllers/admin/tag_controller.ex +++ b/lib/chubi_web/controllers/admin/tag_controller.ex @@ -19,7 +19,7 @@ defmodule ChubiWeb.Admin.TagController do {:ok, tag} -> conn |> put_flash(:info, "Tag created successfully.") - |> redirect(to: Routes.admin_tag_path(conn, :show, tag)) + |> redirect(to: Routes.tag_path(conn, :show, tag)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) @@ -44,7 +44,7 @@ defmodule ChubiWeb.Admin.TagController do {:ok, tag} -> conn |> put_flash(:info, "Tag updated successfully.") - |> redirect(to: Routes.admin_tag_path(conn, :show, tag)) + |> redirect(to: Routes.tag_path(conn, :show, tag)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "edit.html", tag: tag, changeset: changeset) @@ -57,6 +57,6 @@ defmodule ChubiWeb.Admin.TagController do conn |> put_flash(:info, "Tag deleted successfully.") - |> redirect(to: Routes.admin_tag_path(conn, :index)) + |> redirect(to: Routes.tag_path(conn, :index)) end end diff --git a/lib/chubi_web/router.ex b/lib/chubi_web/router.ex index 753d373..4291497 100644 --- a/lib/chubi_web/router.ex +++ b/lib/chubi_web/router.ex @@ -46,17 +46,5 @@ defmodule ChubiWeb.Router do get("/pages/:id", PreviewController, :show_page) end - scope "/admin", ChubiWeb.Admin, as: "admin" do - pipe_through([:browser, :admin]) - get("/", PostController, :index) - resources("/tags", TagController) - resources("/categories", CategoryController) - resources("/posts", PostController) - resources("/pages", PageController) - - post("/upload", UploadController, :create) - delete("/upload", UploadController, :delete) - - get("/settings/set_theme", SettingController, :set_theme) - end + forward("/admin", ChubiWeb.AdminRouter) end diff --git a/lib/chubi_web/templates/admin/category/edit.html.eex b/lib/chubi_web/templates/admin/category/edit.html.eex index e6c5b47..e025e6f 100644 --- a/lib/chubi_web/templates/admin/category/edit.html.eex +++ b/lib/chubi_web/templates/admin/category/edit.html.eex @@ -1,5 +1,5 @@

Edit Category

-<%= render "form.html", Map.put(assigns, :action, Routes.admin_category_path(@conn, :update, @category)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.category_path(@conn, :update, @category)) %> -<%= link "Back", to: Routes.admin_category_path(@conn, :index) %> +<%= link "Back", to: Routes.category_path(@conn, :index) %> diff --git a/lib/chubi_web/templates/admin/category/index.html.eex b/lib/chubi_web/templates/admin/category/index.html.eex index c7f92df..6b5a1ae 100644 --- a/lib/chubi_web/templates/admin/category/index.html.eex +++ b/lib/chubi_web/templates/admin/category/index.html.eex @@ -16,13 +16,13 @@ <%= category.slug %> - <%= link "Show", to: Routes.admin_category_path(@conn, :show, category) %> - <%= link "Edit", to: Routes.admin_category_path(@conn, :edit, category) %> - <%= link "Delete", to: Routes.admin_category_path(@conn, :delete, category), method: :delete, data: [confirm: "Are you sure?"] %> + <%= link "Show", to: Routes.category_path(@conn, :show, category) %> + <%= link "Edit", to: Routes.category_path(@conn, :edit, category) %> + <%= link "Delete", to: Routes.category_path(@conn, :delete, category), method: :delete, data: [confirm: "Are you sure?"] %> <% end %> -<%= link "New Category", to: Routes.admin_category_path(@conn, :new) %> +<%= link "New Category", to: Routes.category_path(@conn, :new) %> diff --git a/lib/chubi_web/templates/admin/category/new.html.eex b/lib/chubi_web/templates/admin/category/new.html.eex index 7e5b4df..3d6fe28 100644 --- a/lib/chubi_web/templates/admin/category/new.html.eex +++ b/lib/chubi_web/templates/admin/category/new.html.eex @@ -1,5 +1,5 @@

New Category

-<%= render "form.html", Map.put(assigns, :action, Routes.admin_category_path(@conn, :create)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.category_path(@conn, :create)) %> -<%= link "Back", to: Routes.admin_category_path(@conn, :index) %> +<%= link "Back", to: Routes.category_path(@conn, :index) %> diff --git a/lib/chubi_web/templates/admin/category/show.html.eex b/lib/chubi_web/templates/admin/category/show.html.eex index 933de48..8fee934 100644 --- a/lib/chubi_web/templates/admin/category/show.html.eex +++ b/lib/chubi_web/templates/admin/category/show.html.eex @@ -14,5 +14,5 @@ -<%= link "Edit", to: Routes.admin_category_path(@conn, :edit, @category) %> -<%= link "Back", to: Routes.admin_category_path(@conn, :index) %> +<%= link "Edit", to: Routes.category_path(@conn, :edit, @category) %> +<%= link "Back", to: Routes.category_path(@conn, :index) %> diff --git a/lib/chubi_web/templates/admin/layout/_header.html.eex b/lib/chubi_web/templates/admin/layout/_header.html.eex index f05ea74..332f233 100644 --- a/lib/chubi_web/templates/admin/layout/_header.html.eex +++ b/lib/chubi_web/templates/admin/layout/_header.html.eex @@ -1,7 +1,7 @@
- + Chubi
diff --git a/lib/chubi_web/templates/admin/layout/_navigation.html.eex b/lib/chubi_web/templates/admin/layout/_navigation.html.eex index 7e2827a..05e93d7 100644 --- a/lib/chubi_web/templates/admin/layout/_navigation.html.eex +++ b/lib/chubi_web/templates/admin/layout/_navigation.html.eex @@ -14,11 +14,11 @@
diff --git a/lib/chubi_web/templates/admin/page/edit.html.eex b/lib/chubi_web/templates/admin/page/edit.html.eex index ca14fa5..4e8dfec 100644 --- a/lib/chubi_web/templates/admin/page/edit.html.eex +++ b/lib/chubi_web/templates/admin/page/edit.html.eex @@ -1,6 +1,6 @@
- <%= render "form.html", Map.put(assigns, :action, Routes.admin_page_path(@conn, :update, @page)) %> + <%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @page)) %>
diff --git a/lib/chubi_web/templates/admin/page/index.html.eex b/lib/chubi_web/templates/admin/page/index.html.eex index fa7d2c9..3ecaa5c 100644 --- a/lib/chubi_web/templates/admin/page/index.html.eex +++ b/lib/chubi_web/templates/admin/page/index.html.eex @@ -2,7 +2,7 @@
@@ -23,7 +23,7 @@ <%= for page <- @pages do %> - <%= page.title %> + <%= page.title %> <%= Routes.page_path(@conn, :show, page) %> @@ -36,8 +36,8 @@ - Edit - <%= link to: Routes.admin_page_path(@conn, :delete, page), class: "btn btn-danger btn-sm", method: "delete", data: [confirm: gettext("Are you sure?")] do %> + Edit + <%= link to: Routes.page_path(@conn, :delete, page), class: "btn btn-danger btn-sm", method: "delete", data: [confirm: gettext("Are you sure?")] do %> <%= gettext "Delete" %> <% end %> diff --git a/lib/chubi_web/templates/admin/page/new.html.eex b/lib/chubi_web/templates/admin/page/new.html.eex index 009fbc3..22ed2c0 100644 --- a/lib/chubi_web/templates/admin/page/new.html.eex +++ b/lib/chubi_web/templates/admin/page/new.html.eex @@ -1,5 +1,5 @@
- <%= render "form.html", Map.put(assigns, :action, Routes.admin_page_path(@conn, :create)) %> + <%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :create)) %>
diff --git a/lib/chubi_web/templates/admin/page/show.html.eex b/lib/chubi_web/templates/admin/page/show.html.eex index 616e18c..f810ab7 100644 --- a/lib/chubi_web/templates/admin/page/show.html.eex +++ b/lib/chubi_web/templates/admin/page/show.html.eex @@ -4,8 +4,8 @@ diff --git a/lib/chubi_web/templates/admin/post/_sidebar.html.eex b/lib/chubi_web/templates/admin/post/_sidebar.html.eex index bcc35ff..29a3eb7 100644 --- a/lib/chubi_web/templates/admin/post/_sidebar.html.eex +++ b/lib/chubi_web/templates/admin/post/_sidebar.html.eex @@ -1,18 +1,18 @@ diff --git a/lib/chubi_web/templates/admin/post/edit.html.eex b/lib/chubi_web/templates/admin/post/edit.html.eex index f7a18de..c713c9d 100644 --- a/lib/chubi_web/templates/admin/post/edit.html.eex +++ b/lib/chubi_web/templates/admin/post/edit.html.eex @@ -1,5 +1,5 @@
- <%= render "_form.html", Map.put(assigns, :action, Routes.admin_post_path(@conn, :update, @post)) %> + <%= render "_form.html", Map.put(assigns, :action, Routes.post_path(@conn, :update, @post)) %>
diff --git a/lib/chubi_web/templates/admin/post/index.html.eex b/lib/chubi_web/templates/admin/post/index.html.eex index 2e6e1f3..47f4e93 100644 --- a/lib/chubi_web/templates/admin/post/index.html.eex +++ b/lib/chubi_web/templates/admin/post/index.html.eex @@ -21,7 +21,7 @@ <%= for post <- @posts do %> - <%= post.title %> + <%= post.title %> @@ -44,8 +44,8 @@ diff --git a/lib/chubi_web/templates/admin/post/new.html.eex b/lib/chubi_web/templates/admin/post/new.html.eex index eda874b..51bf4c4 100644 --- a/lib/chubi_web/templates/admin/post/new.html.eex +++ b/lib/chubi_web/templates/admin/post/new.html.eex @@ -1,5 +1,5 @@
- <%= render "_form.html", Map.put(assigns, :action, Routes.admin_post_path(@conn, :create)) %> + <%= render "_form.html", Map.put(assigns, :action, Routes.post_path(@conn, :create)) %>
diff --git a/lib/chubi_web/templates/admin/post/show.html.eex b/lib/chubi_web/templates/admin/post/show.html.eex index ac429be..389f434 100644 --- a/lib/chubi_web/templates/admin/post/show.html.eex +++ b/lib/chubi_web/templates/admin/post/show.html.eex @@ -9,8 +9,8 @@ diff --git a/lib/chubi_web/templates/admin/tag/edit.html.eex b/lib/chubi_web/templates/admin/tag/edit.html.eex index c0d9bf5..f2f4987 100644 --- a/lib/chubi_web/templates/admin/tag/edit.html.eex +++ b/lib/chubi_web/templates/admin/tag/edit.html.eex @@ -1,5 +1,5 @@

Edit Tag

-<%= render "form.html", Map.put(assigns, :action, Routes.admin_tag_path(@conn, :update, @tag)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.tag_path(@conn, :update, @tag)) %> -<%= link "Back", to: Routes.admin_tag_path(@conn, :index) %> +<%= link "Back", to: Routes.tag_path(@conn, :index) %> diff --git a/lib/chubi_web/templates/admin/tag/index.html.eex b/lib/chubi_web/templates/admin/tag/index.html.eex index 6922414..d64e1bc 100644 --- a/lib/chubi_web/templates/admin/tag/index.html.eex +++ b/lib/chubi_web/templates/admin/tag/index.html.eex @@ -16,13 +16,13 @@ <%= tag.slug %> - <%= link "Show", to: Routes.admin_tag_path(@conn, :show, tag) %> - <%= link "Edit", to: Routes.admin_tag_path(@conn, :edit, tag) %> - <%= link "Delete", to: Routes.admin_tag_path(@conn, :delete, tag), method: :delete, data: [confirm: "Are you sure?"] %> + <%= link "Show", to: Routes.tag_path(@conn, :show, tag) %> + <%= link "Edit", to: Routes.tag_path(@conn, :edit, tag) %> + <%= link "Delete", to: Routes.tag_path(@conn, :delete, tag), method: :delete, data: [confirm: "Are you sure?"] %> <% end %> -<%= link "New Tag", to: Routes.admin_tag_path(@conn, :new) %> +<%= link "New Tag", to: Routes.tag_path(@conn, :new) %> diff --git a/lib/chubi_web/templates/admin/tag/new.html.eex b/lib/chubi_web/templates/admin/tag/new.html.eex index 8892391..51af2aa 100644 --- a/lib/chubi_web/templates/admin/tag/new.html.eex +++ b/lib/chubi_web/templates/admin/tag/new.html.eex @@ -1,5 +1,5 @@

New Tag

-<%= render "form.html", Map.put(assigns, :action, Routes.admin_tag_path(@conn, :create)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.tag_path(@conn, :create)) %> -<%= link "Back", to: Routes.admin_tag_path(@conn, :index) %> +<%= link "Back", to: Routes.tag_path(@conn, :index) %> diff --git a/lib/chubi_web/templates/admin/tag/show.html.eex b/lib/chubi_web/templates/admin/tag/show.html.eex index 30684d3..69c0403 100644 --- a/lib/chubi_web/templates/admin/tag/show.html.eex +++ b/lib/chubi_web/templates/admin/tag/show.html.eex @@ -14,5 +14,5 @@ -<%= link "Edit", to: Routes.admin_tag_path(@conn, :edit, @tag) %> -<%= link "Back", to: Routes.admin_tag_path(@conn, :index) %> +<%= link "Edit", to: Routes.tag_path(@conn, :edit, @tag) %> +<%= link "Back", to: Routes.tag_path(@conn, :index) %> diff --git a/test.zip b/test.zip new file mode 100644 index 0000000000000000000000000000000000000000..dad59b2193ac8ab6b7760c9c0b5a39b8ac7e761b GIT binary patch literal 121 zcmWIWW@Zs#00IAclYpY8=S{3YHVAV9aav|hs-a#18&to4M+g4 TH^7^f4J6J8gyuk62doGH6eJQ{ literal 0 HcmV?d00001 diff --git a/test/chubi_web/controllers/admin/page/page_controller_test.exs b/test/chubi_web/controllers/admin/page/page_controller_test.exs index ebe77c3..afb01ab 100644 --- a/test/chubi_web/controllers/admin/page/page_controller_test.exs +++ b/test/chubi_web/controllers/admin/page/page_controller_test.exs @@ -38,31 +38,31 @@ defmodule ChubiWeb.Admin.Page.PageControllerTest do describe "index" do test "lists all pages", %{conn: conn} do - conn = get(conn, Routes.admin_page_path(conn, :index)) + conn = get(conn, Routes.page_path(conn, :index)) assert html_response(conn, 200) =~ "Listing Pages" end end describe "new page" do test "renders form", %{conn: conn} do - conn = get(conn, Routes.admin_page_path(conn, :new)) + conn = get(conn, Routes.page_path(conn, :new)) assert html_response(conn, 200) =~ "New Page" end end describe "create page" do test "redirects to show when data is valid", %{conn: conn} do - conn = post(conn, Routes.admin_page_path(conn, :create), page: @create_attrs) + conn = post(conn, Routes.page_path(conn, :create), page: @create_attrs) assert %{id: id} = redirected_params(conn) - assert redirected_to(conn) == Routes.admin_page_path(conn, :show, id) + assert redirected_to(conn) == Routes.page_path(conn, :show, id) - conn = get(conn, Routes.admin_page_path(conn, :show, id)) + conn = get(conn, Routes.page_path(conn, :show, id)) assert html_response(conn, 200) =~ "Show Page" end test "renders errors when data is invalid", %{conn: conn} do - conn = post(conn, Routes.admin_page_path(conn, :create), page: @invalid_attrs) + conn = post(conn, Routes.page_path(conn, :create), page: @invalid_attrs) assert html_response(conn, 200) =~ "New Page" end end @@ -71,7 +71,7 @@ defmodule ChubiWeb.Admin.Page.PageControllerTest do setup [:create_page] test "renders form for editing chosen page", %{conn: conn, page: page} do - conn = get(conn, Routes.admin_page_path(conn, :edit, page)) + conn = get(conn, Routes.page_path(conn, :edit, page)) assert html_response(conn, 200) =~ "Edit Page" end end @@ -80,15 +80,15 @@ defmodule ChubiWeb.Admin.Page.PageControllerTest do setup [:create_page] test "redirects when data is valid", %{conn: conn, page: page} do - conn = put(conn, Routes.admin_page_path(conn, :update, page), page: @update_attrs) - assert redirected_to(conn) == Routes.admin_page_path(conn, :show, page) + conn = put(conn, Routes.page_path(conn, :update, page), page: @update_attrs) + assert redirected_to(conn) == Routes.page_path(conn, :show, page) - conn = get(conn, Routes.admin_page_path(conn, :show, page)) + conn = get(conn, Routes.page_path(conn, :show, page)) assert html_response(conn, 200) =~ "some updated content" end test "renders errors when data is invalid", %{conn: conn, page: page} do - conn = put(conn, Routes.admin_page_path(conn, :update, page), page: @invalid_attrs) + conn = put(conn, Routes.page_path(conn, :update, page), page: @invalid_attrs) assert html_response(conn, 200) =~ "Edit Page" end end @@ -97,11 +97,11 @@ defmodule ChubiWeb.Admin.Page.PageControllerTest do setup [:create_page] test "deletes chosen page", %{conn: conn, page: page} do - conn = delete(conn, Routes.admin_page_path(conn, :delete, page)) - assert redirected_to(conn) == Routes.admin_page_path(conn, :index) + conn = delete(conn, Routes.page_path(conn, :delete, page)) + assert redirected_to(conn) == Routes.page_path(conn, :index) assert_error_sent(404, fn -> - get(conn, Routes.admin_page_path(conn, :show, page)) + get(conn, Routes.page_path(conn, :show, page)) end) end end From d717f1059e6e05a3f775c55ba38f3cf689179122 Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Tue, 21 Jan 2020 11:12:58 +0700 Subject: [PATCH 2/3] fix import content --- lib/chubi_web/admin_router.ex | 1 + .../controllers/admin/.#setting_controller.ex | 1 - .../controllers/admin/setting_controller.ex | 25 ++++++++++++------- .../templates/admin/layout/_alert.html.eex | 24 ++++++++++++++++++ .../admin/layout/_navigation.html.eex | 2 +- .../templates/admin/layout/app.html.eex | 3 +++ .../admin/setting/select_import.html.eex | 22 ++++++++++++++++ lib/chubi_web/views/admin/setting_view.ex | 3 +++ 8 files changed, 70 insertions(+), 11 deletions(-) delete mode 120000 lib/chubi_web/controllers/admin/.#setting_controller.ex create mode 100644 lib/chubi_web/templates/admin/layout/_alert.html.eex create mode 100644 lib/chubi_web/templates/admin/setting/select_import.html.eex create mode 100644 lib/chubi_web/views/admin/setting_view.ex diff --git a/lib/chubi_web/admin_router.ex b/lib/chubi_web/admin_router.ex index a38509a..28c683f 100644 --- a/lib/chubi_web/admin_router.ex +++ b/lib/chubi_web/admin_router.ex @@ -40,6 +40,7 @@ defmodule ChubiWeb.AdminRouter do get("/settings/set_theme", SettingController, :set_theme) get("/settings/export", SettingController, :export_content) + get("/settings/select_import", SettingController, :select_import) post("/settings/import", SettingController, :import_content) end end diff --git a/lib/chubi_web/controllers/admin/.#setting_controller.ex b/lib/chubi_web/controllers/admin/.#setting_controller.ex deleted file mode 120000 index fa2b3dd..0000000 --- a/lib/chubi_web/controllers/admin/.#setting_controller.ex +++ /dev/null @@ -1 +0,0 @@ -flex@Thes-MacBook-Pro.local.769 \ No newline at end of file diff --git a/lib/chubi_web/controllers/admin/setting_controller.ex b/lib/chubi_web/controllers/admin/setting_controller.ex index 8b3ea91..7fd33f1 100644 --- a/lib/chubi_web/controllers/admin/setting_controller.ex +++ b/lib/chubi_web/controllers/admin/setting_controller.ex @@ -1,5 +1,5 @@ defmodule ChubiWeb.Admin.SettingController do - use ChubiWeb, :controller + use ChubiWeb, :admin_controller alias Chubi.Content def set_theme(conn, %{"theme" => theme}) do @@ -41,12 +41,15 @@ defmodule ChubiWeb.Admin.SettingController do end def select_import(conn, _) do + render(conn, "select_import.html") end - def import_content(conn, %{%{path: path} => file}) do - with {:ok, files} <- :zip.extract(path, [:memory]) do + def import_content(conn, %{"file" => %{path: path}}) do + with {:ok, files} <- :zip.extract(String.to_charlist(path), [:memory]) do rs = - Enum.map(files, fn {_filename, content} -> + Enum.map(files, fn {file_path, content} -> + file_path = to_string(file_path) + attrs = case Path.extname(file_path) do ".md" -> %{"content" => content, "format" => "markdown"} @@ -63,12 +66,16 @@ defmodule ChubiWeb.Admin.SettingController do success = Enum.filter(rs, &(elem(&1, 0) == :ok)) conn - |> put_flash(:error, gettext("Cannot import content")) - |> redirect(to: Routes.setting_path(conn, :select_import)) + |> put_flash( + :info, + gettext("%{success}/%{total} is imported", %{success: length(success), total: length(rs)}) + ) + |> redirect(to: Routes.post_path(conn, :index)) else - conn - |> put_flash(:error, gettext("Cannot import content")) - |> redirect(to: Routes.setting_path(conn, :select_import)) + err -> + conn + |> put_flash(:error, gettext("Cannot import content")) + |> redirect(to: Routes.setting_path(conn, :select_import)) end end end diff --git a/lib/chubi_web/templates/admin/layout/_alert.html.eex b/lib/chubi_web/templates/admin/layout/_alert.html.eex new file mode 100644 index 0000000..86b2872 --- /dev/null +++ b/lib/chubi_web/templates/admin/layout/_alert.html.eex @@ -0,0 +1,24 @@ +<%= if message = get_flash(@conn, :error) do %> + +<% end %> + +<%= if message = get_flash(@conn, :info) do %> + +<% end %> + +<%= if message = get_flash(@conn, :success) do %> + +<% end %> + +<%= if message = get_flash(@conn, :warning) do %> + +<% end %> diff --git a/lib/chubi_web/templates/admin/layout/_navigation.html.eex b/lib/chubi_web/templates/admin/layout/_navigation.html.eex index 05e93d7..fc893ce 100644 --- a/lib/chubi_web/templates/admin/layout/_navigation.html.eex +++ b/lib/chubi_web/templates/admin/layout/_navigation.html.eex @@ -37,7 +37,7 @@ <%= gettext "Settings" %> diff --git a/lib/chubi_web/templates/admin/layout/app.html.eex b/lib/chubi_web/templates/admin/layout/app.html.eex index 3bf88d5..524d090 100644 --- a/lib/chubi_web/templates/admin/layout/app.html.eex +++ b/lib/chubi_web/templates/admin/layout/app.html.eex @@ -35,6 +35,9 @@
+
+ <%= render "_alert.html", assigns %> +
<%= render @view_module, @view_template, assigns %>
diff --git a/lib/chubi_web/templates/admin/setting/select_import.html.eex b/lib/chubi_web/templates/admin/setting/select_import.html.eex new file mode 100644 index 0000000..e25b217 --- /dev/null +++ b/lib/chubi_web/templates/admin/setting/select_import.html.eex @@ -0,0 +1,22 @@ +
+
+
+
+
+

<%= gettext("Select ZIP file content") %>

+ +
+
+ + +
+ +
+ +
+
+
+ +
+
+ diff --git a/lib/chubi_web/views/admin/setting_view.ex b/lib/chubi_web/views/admin/setting_view.ex new file mode 100644 index 0000000..812fdf5 --- /dev/null +++ b/lib/chubi_web/views/admin/setting_view.ex @@ -0,0 +1,3 @@ +defmodule ChubiWeb.Admin.SettingView do + use ChubiWeb, :admin_view +end From 58d52fcfff33a4088c8d5a76681dd8cefde559bc Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Tue, 21 Jan 2020 11:34:22 +0700 Subject: [PATCH 3/3] Fix import function select wrong parser --- lib/chubi/content.ex | 2 +- lib/chubi_web/controllers/admin/setting_controller.ex | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/chubi/content.ex b/lib/chubi/content.ex index 11b8533..67605ae 100644 --- a/lib/chubi/content.ex +++ b/lib/chubi/content.ex @@ -211,7 +211,7 @@ defmodule Chubi.Content do end defp parse_content(%{"content" => content} = params) do - format = Application.get_env(:chubi, :post_format) || "html" + format = params["format"] || Application.get_env(:chubi, :post_format) || "markdown" parser = Keyword.get(@content_parser_map, String.to_atom(format)) attrs = parser.parse(content, params) Map.merge(params, attrs) diff --git a/lib/chubi_web/controllers/admin/setting_controller.ex b/lib/chubi_web/controllers/admin/setting_controller.ex index 7fd33f1..9e4b79f 100644 --- a/lib/chubi_web/controllers/admin/setting_controller.ex +++ b/lib/chubi_web/controllers/admin/setting_controller.ex @@ -48,12 +48,17 @@ defmodule ChubiWeb.Admin.SettingController do with {:ok, files} <- :zip.extract(String.to_charlist(path), [:memory]) do rs = Enum.map(files, fn {file_path, content} -> - file_path = to_string(file_path) + file_path = + to_string(file_path) + |> IO.inspect() attrs = case Path.extname(file_path) do - ".md" -> %{"content" => content, "format" => "markdown"} - _ -> Jason.decode!(content) + ".md" -> + %{"content" => content, "format" => "markdown"} + + _ext -> + Jason.decode!(content) end if String.starts_with?(file_path, "posts") do