add reindex and delete buttons

This commit is contained in:
byte[] 2019-12-16 13:49:53 -05:00
parent a28d233751
commit 70b077e4e7
6 changed files with 61 additions and 39 deletions

View file

@ -163,7 +163,9 @@ defmodule Philomena.Tags do
|> select([i, _t], i.id) |> select([i, _t], i.id)
|> Repo.all() |> Repo.all()
{:ok, _tag} = Repo.delete(tag) {:ok, tag} = Repo.delete(tag)
Tag.delete_document(tag.id)
Image Image
|> where([i], i.id in ^image_ids) |> where([i], i.id in ^image_ids)
@ -205,25 +207,32 @@ defmodule Philomena.Tags do
|> where(tag_id: ^tag.id) |> where(tag_id: ^tag.id)
|> Repo.update_all(set: [tag_id: target_tag.id]) |> Repo.update_all(set: [tag_id: target_tag.id])
# Update counters # Update counter
new_count =
Image
|> join(:inner, [i], _ in assoc(i, :tags))
|> where([i, t], i.hidden_from_users == false and t.id == ^target_tag.id)
|> Repo.aggregate(:count, :id)
Tag
|> where(id: ^target_tag.id)
|> Repo.update_all(set: [images_count: new_count])
Tag Tag
|> where(id: ^tag.id) |> where(id: ^tag.id)
|> Repo.update_all(set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()]) |> Repo.update_all(set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()])
# Finally, update images # Finally, reindex
reindex_tag_images(target_tag)
reindex_tags([tag, target_tag])
end
def reindex_tag_images(%Tag{} = tag) do
# First recount the tag
image_count =
Image Image
|> join(:inner, [i], _ in assoc(i, :tags)) |> join(:inner, [i], _ in assoc(i, :tags))
|> where([_i, t], t.id == ^target_tag.id) |> where([i, t], i.hidden_from_users == false and t.id == ^tag.id)
|> Repo.aggregate(:count, :id)
Tag
|> where(id: ^tag.id)
|> Repo.update_all(set: [images_count: image_count])
# Then reindex
Image
|> join(:inner, [i], _ in assoc(i, :tags))
|> where([_i, t], t.id == ^tag.id)
|> preload(^Images.indexing_preloads()) |> preload(^Images.indexing_preloads())
|> Image.reindex() |> Image.reindex()
end end

View file

@ -0,0 +1,19 @@
defmodule PhilomenaWeb.Tag.ReindexController do
use PhilomenaWeb, :controller
alias Philomena.Tags.Tag
alias Philomena.Tags
plug PhilomenaWeb.CanaryMapPlug, create: :alias
plug :load_and_authorize_resource, model: Tag, id_name: "tag_id", id_field: "slug", preload: [:implied_tags, :aliased_tag], persisted: true
def create(conn, _params) do
spawn fn ->
Tags.reindex_tag_images(conn.assigns.tag)
end
conn
|> put_flash(:info, "Tag reindex started.")
|> redirect(to: Routes.tag_path(conn, :edit, conn.assigns.tag))
end
end

View file

@ -9,7 +9,7 @@ defmodule PhilomenaWeb.TagController do
plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show] plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show]
plug PhilomenaWeb.CanaryMapPlug, update: :edit plug PhilomenaWeb.CanaryMapPlug, update: :edit
plug :load_and_authorize_resource, model: Tag, id_field: "slug", only: [:show, :edit, :update, :delete], preload: [:aliases, :aliased_tag, :implied_tags, :implied_by_tags, :dnp_entries, public_links: :user] plug :load_and_authorize_resource, model: Tag, id_field: "slug", only: [:show, :edit, :update, :delete], preload: [:aliases, :aliased_tag, :implied_tags, :implied_by_tags, :dnp_entries, public_links: :user]
plug :redirect_alias when action in [:show, :edit, :update, :delete] plug :redirect_alias when action in [:show]
def index(conn, params) do def index(conn, params) do
query_string = params["tq"] || "*" query_string = params["tq"] || "*"

View file

@ -232,6 +232,7 @@ defmodule PhilomenaWeb.Router do
resources "/tags", TagController, only: [:edit, :update, :delete] do resources "/tags", TagController, only: [:edit, :update, :delete] do
resources "/image", Tag.ImageController, only: [:edit, :update, :delete], singleton: true resources "/image", Tag.ImageController, only: [:edit, :update, :delete], singleton: true
resources "/alias", Tag.AliasController, only: [:edit, :update, :delete], singleton: true resources "/alias", Tag.AliasController, only: [:edit, :update, :delete], singleton: true
resources "/reindex", Tag.ReindexController, only: [:create], singleton: true
end end
resources "/pages", PageController, only: [:index, :new, :create, :edit, :update] resources "/pages", PageController, only: [:index, :new, :create, :edit, :update]

View file

@ -15,7 +15,7 @@ h1
=> submit "Alias tag", class: "button" => submit "Alias tag", class: "button"
br br
= button_to "Remove tag alias", Routes.tag_alias_path(@conn, :delete, @tag), method: "delete", class: "button", data: [confirm: "Are you really, really sure?"] = button_to "Remove tag alias", Routes.tag_alias_path(@conn, :delete, @tag), method: "delete", class: "button", data: [confirm: "Are you really, really sure?", disable_with: raw("Saving…")]
br br
= link "Back", to: Routes.tag_path(@conn, :show, @tag) = link "Back", to: Routes.tag_path(@conn, :show, @tag)

View file

@ -36,26 +36,19 @@ p = link "Edit aliases", to: Routes.tag_alias_path(@conn, :edit, @tag)
br br
= submit "Save Tag", class: "button button--state-primary" = submit "Save Tag", class: "button button--state-primary"
/ not ready yet
br br
br br
input.toggle-box#tag-management checked="false" type="checkbox" input.toggle-box#tag-management checked="false" type="checkbox"
label for="tag-management" Tag Processing label for="tag-management" Tag Processing
.toggle-box-container .toggle-box-container
.toggle-box-container__content .toggle-box-container__content
= link_to "Rebuild index", admin_tag_reindex_path(@tag), class: "button", data: { confirm: t("are_you_sure") }, method: :post = button_to "Rebuild index", Routes.tag_reindex_path(@conn, :create, @tag), method: "post", class: "button", data: [confirm: "Are you really, really sure?", disable_with: raw("Reindexing…")]
p Use this if the tag displays the wrong number of images or returns the wrong search results. p Use this if the tag displays the wrong number of images or returns the wrong search results.
= link_to "Recreate slug", admin_tag_slug_path(@tag), class: "button", method: :post
p = button_to "Destroy tag", Routes.tag_path(@conn, :delete, @tag), method: "delete", class: "button button--state-danger", data: [confirm: "Are you really, really sure?", disable_with: raw("Deleting…")]
| Use this for old tags with invalid slugs (
code
' /mlp/ →
= Tag.generate_slug "/mlp/"
| )
= link_to "Destroy tag", admin_tag_path(@tag), class: "button button--state-danger", data: { confirm: t("are_you_sure") }, method: :delete
p p
strong Irreversible. Use with extreme caution! strong Irreversible. Use with extreme caution!
ul ul
li Intended use is removing garbage tags. li Intended use is removing garbage tags.
li Will remove tag changes on the tag, but not on images or profiles. li Will remove tag changes on the tag, but not on images or profiles.
li Will fail if the tag is the target of an alias, is implied by other tags, or is a rating tag. li Will unset the alias if this tag is an alias target, and will automatically remove any implications that exist to it.