mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
admin donations
This commit is contained in:
parent
a3e4706597
commit
c45a2a16d9
10 changed files with 144 additions and 2 deletions
|
@ -11,7 +11,7 @@ defmodule Philomena.Donations.Donation do
|
||||||
field :amount, :decimal
|
field :amount, :decimal
|
||||||
field :fee, :decimal
|
field :fee, :decimal
|
||||||
field :txn_id, :string
|
field :txn_id, :string
|
||||||
field :reciept_id, :string
|
field :receipt_id, :string
|
||||||
field :note, :string
|
field :note, :string
|
||||||
|
|
||||||
timestamps(inserted_at: :created_at)
|
timestamps(inserted_at: :created_at)
|
||||||
|
@ -20,7 +20,8 @@ defmodule Philomena.Donations.Donation do
|
||||||
@doc false
|
@doc false
|
||||||
def changeset(donation, attrs) do
|
def changeset(donation, attrs) do
|
||||||
donation
|
donation
|
||||||
|> cast(attrs, [])
|
|> cast(attrs, [:email, :amount, :note, :user_id])
|
||||||
|> validate_required([])
|
|> validate_required([])
|
||||||
|
|> foreign_key_constraint(:user_id, name: :fk_rails_5470822a00)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,6 +26,7 @@ defmodule Philomena.Users.User do
|
||||||
alias Philomena.UserFingerprints.UserFingerprint
|
alias Philomena.UserFingerprints.UserFingerprint
|
||||||
alias Philomena.UserIps.UserIp
|
alias Philomena.UserIps.UserIp
|
||||||
alias Philomena.Bans.User, as: UserBan
|
alias Philomena.Bans.User, as: UserBan
|
||||||
|
alias Philomena.Donations.Donation
|
||||||
|
|
||||||
@derive {Phoenix.Param, key: :slug}
|
@derive {Phoenix.Param, key: :slug}
|
||||||
|
|
||||||
|
@ -41,6 +42,7 @@ defmodule Philomena.Users.User do
|
||||||
has_many :user_ips, UserIp
|
has_many :user_ips, UserIp
|
||||||
has_many :user_fingerprints, UserFingerprint
|
has_many :user_fingerprints, UserFingerprint
|
||||||
has_many :bans, UserBan
|
has_many :bans, UserBan
|
||||||
|
has_many :donations, Donation
|
||||||
has_one :commission, Commission
|
has_one :commission, Commission
|
||||||
many_to_many :roles, Role, join_through: "users_roles", on_replace: :delete
|
many_to_many :roles, Role, join_through: "users_roles", on_replace: :delete
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
defmodule PhilomenaWeb.Admin.Donation.UserController do
|
||||||
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
|
alias Philomena.Users.User
|
||||||
|
alias Philomena.Donations.Donation
|
||||||
|
alias Philomena.Donations
|
||||||
|
|
||||||
|
plug :verify_authorized
|
||||||
|
plug :load_resource, model: User, id_field: "slug", persisted: true, preload: [donations: :user]
|
||||||
|
|
||||||
|
def show(conn, _params) do
|
||||||
|
user = conn.assigns.user
|
||||||
|
changeset = Donations.change_donation(%Donation{})
|
||||||
|
render(conn, "index.html", title: "Donations for User `#{user.name}'", donations: user.donations, changeset: changeset)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp verify_authorized(conn, _opts) do
|
||||||
|
case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do
|
||||||
|
true -> conn
|
||||||
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
41
lib/philomena_web/controllers/admin/donation_controller.ex
Normal file
41
lib/philomena_web/controllers/admin/donation_controller.ex
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
defmodule PhilomenaWeb.Admin.DonationController do
|
||||||
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
|
alias Philomena.Donations.Donation
|
||||||
|
alias Philomena.Donations
|
||||||
|
alias Philomena.Repo
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
plug :verify_authorized
|
||||||
|
|
||||||
|
def index(conn, _params) do
|
||||||
|
donations =
|
||||||
|
Donation
|
||||||
|
|> order_by(desc: :created_at, asc: :user_id)
|
||||||
|
|> preload(:user)
|
||||||
|
|> Repo.paginate(conn.assigns.scrivener)
|
||||||
|
|
||||||
|
render(conn, "index.html", title: "Admin - Donations", donations: donations)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(conn, %{"donation" => donation_params}) do
|
||||||
|
case Donations.create_donation(donation_params) do
|
||||||
|
{:ok, _donation} ->
|
||||||
|
conn
|
||||||
|
|> put_flash(:info, "Donation successfully created.")
|
||||||
|
|> redirect(to: Routes.admin_donation_path(conn, :index))
|
||||||
|
|
||||||
|
_error ->
|
||||||
|
conn
|
||||||
|
|> put_flash(:error, "Error creating donation!")
|
||||||
|
|> redirect(to: Routes.admin_donation_path(conn, :index))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp verify_authorized(conn, _opts) do
|
||||||
|
case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do
|
||||||
|
true -> conn
|
||||||
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -227,6 +227,8 @@ defmodule PhilomenaWeb.Router do
|
||||||
end
|
end
|
||||||
|
|
||||||
resources "/batch/tags", Batch.TagController, only: [:update], singleton: true
|
resources "/batch/tags", Batch.TagController, only: [:update], singleton: true
|
||||||
|
resources "/donations", DonationController, only: [:index, :create]
|
||||||
|
resources "/donations/user", Donation.UserController, only: [:show]
|
||||||
end
|
end
|
||||||
|
|
||||||
resources "/duplicate_reports", DuplicateReportController, only: [] do
|
resources "/duplicate_reports", DuplicateReportController, only: [] do
|
||||||
|
|
27
lib/philomena_web/templates/admin/donation/_table.html.slime
Normal file
27
lib/philomena_web/templates/admin/donation/_table.html.slime
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
table.table
|
||||||
|
thead
|
||||||
|
tr
|
||||||
|
th User
|
||||||
|
th Email
|
||||||
|
th Amount
|
||||||
|
th Note
|
||||||
|
th At
|
||||||
|
|
||||||
|
tbody
|
||||||
|
= for donation <- @donations do
|
||||||
|
tr
|
||||||
|
td
|
||||||
|
= if donation.user do
|
||||||
|
= link donation.user.name, to: Routes.profile_path(@conn, :show, donation.user)
|
||||||
|
|
||||||
|
td
|
||||||
|
= donation.email
|
||||||
|
|
||||||
|
td
|
||||||
|
= donation.amount
|
||||||
|
|
||||||
|
td title=donation.note
|
||||||
|
= String.slice(donation.note, 0, 30)
|
||||||
|
|
||||||
|
td
|
||||||
|
= pretty_time donation.created_at
|
14
lib/philomena_web/templates/admin/donation/index.html.slime
Normal file
14
lib/philomena_web/templates/admin/donation/index.html.slime
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
- route = fn p -> Routes.admin_donation_path(@conn, :index, p) end
|
||||||
|
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @donations, route: route, conn: @conn
|
||||||
|
|
||||||
|
h1 Donations
|
||||||
|
|
||||||
|
.block
|
||||||
|
.block__header
|
||||||
|
= pagination
|
||||||
|
|
||||||
|
.block__content
|
||||||
|
= render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn
|
||||||
|
|
||||||
|
.block__header.block__header--light
|
||||||
|
= pagination
|
|
@ -0,0 +1,26 @@
|
||||||
|
.walloftext
|
||||||
|
h1
|
||||||
|
= @user.name
|
||||||
|
| 's Donations
|
||||||
|
|
||||||
|
= render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn
|
||||||
|
|
||||||
|
h1 Add Donation
|
||||||
|
= form_for @changeset, Routes.admin_donation_path(@conn, :create), fn f ->
|
||||||
|
.field
|
||||||
|
=> label f, :user_id, "User ID:"
|
||||||
|
= number_input f, :user_id, class: "input input--short", value: @user.id
|
||||||
|
|
||||||
|
.field
|
||||||
|
=> label f, :email, "Email:"
|
||||||
|
= text_input f, :email, class: "input input--wide", value: @user.email
|
||||||
|
|
||||||
|
.field
|
||||||
|
=> label f, :amount, "Amount:"
|
||||||
|
= number_input f, :amount, class: "input input--wide", min: 0, step: 0.01
|
||||||
|
|
||||||
|
.field
|
||||||
|
=> label f, :note, "Note:"
|
||||||
|
= textarea f, :note, class: "input input--wide"
|
||||||
|
|
||||||
|
= submit "Create Donation", class: "button"
|
3
lib/philomena_web/views/admin/donation/user_view.ex
Normal file
3
lib/philomena_web/views/admin/donation/user_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
defmodule PhilomenaWeb.Admin.Donation.UserView do
|
||||||
|
use PhilomenaWeb, :view
|
||||||
|
end
|
3
lib/philomena_web/views/admin/donation_view.ex
Normal file
3
lib/philomena_web/views/admin/donation_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
defmodule PhilomenaWeb.Admin.DonationView do
|
||||||
|
use PhilomenaWeb, :view
|
||||||
|
end
|
Loading…
Reference in a new issue