mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
parent
ac12462bec
commit
21df6bc307
12 changed files with 145 additions and 8 deletions
|
@ -140,6 +140,23 @@ defmodule Philomena.Adverts do
|
|||
|> Repo.update()
|
||||
end
|
||||
|
||||
def update_advert_image(%Advert{} = advert, attrs) do
|
||||
advert =
|
||||
advert
|
||||
|> Advert.changeset(attrs)
|
||||
|> Uploader.analyze_upload(attrs)
|
||||
|
||||
Multi.new()
|
||||
|> Multi.update(:advert, advert)
|
||||
|> Multi.run(:after, fn _repo, %{advert: advert} ->
|
||||
Uploader.persist_upload(advert)
|
||||
Uploader.unpersist_old_upload(advert)
|
||||
|
||||
{:ok, nil}
|
||||
end)
|
||||
|> Repo.isolated_transaction(:serializable)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a Advert.
|
||||
|
||||
|
|
|
@ -81,6 +81,12 @@ defmodule Philomena.Badges do
|
|||
|
||||
"""
|
||||
def update_badge(%Badge{} = badge, attrs) do
|
||||
badge
|
||||
|> Badge.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def update_badge_image(%Badge{} = badge, attrs) do
|
||||
badge =
|
||||
badge
|
||||
|> Badge.changeset(attrs)
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
defmodule PhilomenaWeb.Admin.Advert.ImageController do
|
||||
use PhilomenaWeb, :controller
|
||||
|
||||
alias Philomena.Adverts.Advert
|
||||
alias Philomena.Adverts
|
||||
|
||||
plug :verify_authorized
|
||||
plug :load_and_authorize_resource, model: Advert, id_name: "advert_id", persisted: true, only: [:edit, :update, :delete]
|
||||
|
||||
def edit(conn, _params) do
|
||||
changeset = Adverts.change_advert(conn.assigns.advert)
|
||||
render(conn, "edit.html", title: "Editing Advert", changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"advert" => advert_params}) do
|
||||
case Adverts.update_advert_image(conn.assigns.advert, advert_params) do
|
||||
{:ok, _advert} ->
|
||||
conn
|
||||
|> put_flash(:info, "Advert was successfully updated.")
|
||||
|> redirect(to: Routes.admin_advert_path(conn, :index))
|
||||
|
||||
{:error, changeset} ->
|
||||
render(conn, "edit.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
defp verify_authorized(conn, _opts) do
|
||||
case Canada.Can.can?(conn.assigns.current_user, :index, Advert) do
|
||||
true -> conn
|
||||
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
defmodule PhilomenaWeb.Admin.Badge.ImageController do
|
||||
use PhilomenaWeb, :controller
|
||||
|
||||
alias Philomena.Badges.Badge
|
||||
alias Philomena.Badges
|
||||
|
||||
plug :verify_authorized
|
||||
plug :load_resource, model: Badge, id_name: "badge_id", persisted: true, only: [:edit, :update]
|
||||
|
||||
def edit(conn, _params) do
|
||||
changeset = Badges.change_badge(conn.assigns.badge)
|
||||
render(conn, "edit.html", title: "Editing Badge", changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"badge" => badge_params}) do
|
||||
case Badges.update_badge_image(conn.assigns.badge, badge_params) do
|
||||
{:ok, %{badge: _badge}} ->
|
||||
conn
|
||||
|> put_flash(:info, "Badge updated successfully.")
|
||||
|> redirect(to: Routes.admin_badge_path(conn, :index))
|
||||
|
||||
{:error, :badge, changeset, _changes} ->
|
||||
render(conn, "edit.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
defp verify_authorized(conn, _opts) do
|
||||
case Canada.Can.can?(conn.assigns.current_user, :index, Badge) do
|
||||
true -> conn
|
||||
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -42,7 +42,7 @@ defmodule PhilomenaWeb.Admin.BadgeController do
|
|||
|
||||
def update(conn, %{"badge" => badge_params}) do
|
||||
case Badges.update_badge(conn.assigns.badge, badge_params) do
|
||||
{:ok, %{badge: _badge}} ->
|
||||
{:ok, _badge} ->
|
||||
conn
|
||||
|> put_flash(:info, "Badge updated successfully.")
|
||||
|> redirect(to: Routes.admin_badge_path(conn, :index))
|
||||
|
|
|
@ -320,12 +320,15 @@ defmodule PhilomenaWeb.Router do
|
|||
only: [:index, :new, :create, :edit, :update, :delete]
|
||||
|
||||
resources "/site_notices", SiteNoticeController, except: [:show]
|
||||
resources "/adverts", AdvertController, except: [:show]
|
||||
resources "/adverts", AdvertController, except: [:show] do
|
||||
resources "/image", Advert.ImageController, only: [:edit, :update], singleton: true
|
||||
end
|
||||
|
||||
resources "/forums", ForumController, except: [:show, :delete]
|
||||
|
||||
resources "/badges", BadgeController, except: [:show, :delete] do
|
||||
resources "/users", Badge.UserController, only: [:index]
|
||||
resources "/image", Badge.ImageController, only: [:edit, :update], singleton: true
|
||||
end
|
||||
|
||||
resources "/mod_notes", ModNoteController, except: [:show]
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
= error_tag f, :image_size
|
||||
= error_tag f, :image_width
|
||||
= error_tag f, :image_height
|
||||
- else
|
||||
.field
|
||||
= link "Change image", to: Routes.admin_advert_image_path(@conn, :edit, @changeset.data), class: "button"
|
||||
|
||||
.field
|
||||
=> label f, :link, "Link which the advert should take users to:"
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
h2 Edit Advert
|
||||
|
||||
= form_for @changeset, Routes.admin_advert_image_path(@conn, :update, @advert), [multipart: true], fn f ->
|
||||
= if @changeset.action do
|
||||
.alert.alert-danger
|
||||
p Oops, something went wrong! Please check the errors below.
|
||||
|
||||
.field
|
||||
=> label f, :image, "Upload image:"
|
||||
= file_input f, :image, class: "input input--wide"
|
||||
= error_tag f, :image
|
||||
= error_tag f, :image_mime_type
|
||||
= error_tag f, :image_size
|
||||
= error_tag f, :image_width
|
||||
= error_tag f, :image_height
|
||||
|
||||
= submit "Save Advert", class: "button"
|
|
@ -21,11 +21,15 @@
|
|||
=> checkbox f, :priority, class: "checkbox"
|
||||
= label f, :priority, "Displays before badges that don't have this checkbox checked"
|
||||
|
||||
= if @changeset.data.__meta__.state != :loaded do
|
||||
h4 Image
|
||||
.field
|
||||
=> label f, :image, "Upload SVG image:"
|
||||
= file_input f, :image, class: "input input--wide"
|
||||
= error_tag f, :image
|
||||
= error_tag f, :image_mime_type
|
||||
- else
|
||||
.field
|
||||
= link "Change image", to: Routes.admin_badge_image_path(@conn, :edit, @changeset.data), class: "button"
|
||||
|
||||
= submit "Save Badge", class: "button", data: [disable_with: raw("Saving…")]
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
h2 Edit Badge
|
||||
|
||||
= form_for @changeset, Routes.admin_badge_image_path(@conn, :update, @badge), [multipart: true], fn f ->
|
||||
= if @changeset.action do
|
||||
.alert.alert-danger
|
||||
p Oops, something went wrong! Please check the errors below.
|
||||
|
||||
h4 Image
|
||||
.field
|
||||
=> label f, :image, "Upload SVG image:"
|
||||
= file_input f, :image, class: "input input--wide"
|
||||
= error_tag f, :image
|
||||
= error_tag f, :image_mime_type
|
||||
|
||||
= submit "Save Badge", class: "button", data: [disable_with: raw("Saving…")]
|
3
lib/philomena_web/views/admin/advert/image_view.ex
Normal file
3
lib/philomena_web/views/admin/advert/image_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
|||
defmodule PhilomenaWeb.Admin.Advert.ImageView do
|
||||
use PhilomenaWeb, :view
|
||||
end
|
3
lib/philomena_web/views/admin/badge/image_view.ex
Normal file
3
lib/philomena_web/views/admin/badge/image_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
|||
defmodule PhilomenaWeb.Admin.Badge.ImageView do
|
||||
use PhilomenaWeb, :view
|
||||
end
|
Loading…
Reference in a new issue