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)
|> Repo.all()
{:ok, _tag} = Repo.delete(tag)
{:ok, tag} = Repo.delete(tag)
Tag.delete_document(tag.id)
Image
|> where([i], i.id in ^image_ids)
@ -205,25 +207,32 @@ defmodule Philomena.Tags do
|> where(tag_id: ^tag.id)
|> Repo.update_all(set: [tag_id: target_tag.id])
# Update counters
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])
# Update counter
Tag
|> where(id: ^tag.id)
|> 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
|> join(:inner, [i], _ in assoc(i, :tags))
|> 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 == ^target_tag.id)
|> where([_i, t], t.id == ^tag.id)
|> preload(^Images.indexing_preloads())
|> Image.reindex()
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.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 :redirect_alias when action in [:show, :edit, :update, :delete]
plug :redirect_alias when action in [:show]
def index(conn, params) do
query_string = params["tq"] || "*"

View file

@ -232,6 +232,7 @@ defmodule PhilomenaWeb.Router do
resources "/tags", TagController, only: [:edit, :update, :delete] do
resources "/image", Tag.ImageController, 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
resources "/pages", PageController, only: [:index, :new, :create, :edit, :update]

View file

@ -15,7 +15,7 @@ h1
=> submit "Alias tag", class: "button"
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
= 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
= submit "Save Tag", class: "button button--state-primary"
/ not ready yet
br
br
input.toggle-box#tag-management checked="false" type="checkbox"
label for="tag-management" Tag Processing
.toggle-box-container
.toggle-box-container__content
= link_to "Rebuild index", admin_tag_reindex_path(@tag), class: "button", data: { confirm: t("are_you_sure") }, method: :post
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
| 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
strong Irreversible. Use with extreme caution!
ul
li Intended use is removing garbage tags.
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.
br
br
input.toggle-box#tag-management checked="false" type="checkbox"
label for="tag-management" Tag Processing
.toggle-box-container
.toggle-box-container__content
= 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.
= 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…")]
p
strong Irreversible. Use with extreme caution!
ul
li Intended use is removing garbage tags.
li Will remove tag changes on the tag, but not on images or profiles.
li Will unset the alias if this tag is an alias target, and will automatically remove any implications that exist to it.