mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +01:00
81 lines
2 KiB
Elixir
81 lines
2 KiB
Elixir
defmodule Philomena.Commissions.Commission do
|
|
use Ecto.Schema
|
|
import Ecto.Changeset
|
|
|
|
alias Philomena.Commissions.Item
|
|
alias Philomena.Images.Image
|
|
alias Philomena.Users.User
|
|
|
|
schema "commissions" do
|
|
belongs_to :user, User
|
|
belongs_to :sheet_image, Image
|
|
has_many :items, Item
|
|
|
|
field :open, :boolean
|
|
field :categories, {:array, :string}, default: []
|
|
field :information, :string
|
|
field :contact, :string
|
|
field :will_create, :string
|
|
field :will_not_create, :string
|
|
field :commission_items_count, :integer, default: 0
|
|
|
|
timestamps(inserted_at: :created_at)
|
|
end
|
|
|
|
@doc false
|
|
def changeset(commission, attrs) do
|
|
commission
|
|
|> cast(attrs, [:information, :contact, :will_create, :will_not_create, :open, :sheet_image_id, :categories])
|
|
|> drop_blank_categories()
|
|
|> validate_required([:user_id, :information, :contact, :open])
|
|
|> validate_length(:information, max: 700, count: :bytes)
|
|
|> validate_length(:contact, max: 700, count: :bytes)
|
|
|> validate_subset(:categories, Keyword.values(categories()))
|
|
end
|
|
|
|
defp drop_blank_categories(changeset) do
|
|
categories =
|
|
changeset
|
|
|> get_field(:categories)
|
|
|> Enum.filter(& &1 not in [nil, ""])
|
|
|
|
change(changeset, categories: categories)
|
|
end
|
|
|
|
def categories do
|
|
[
|
|
"Anthro": "Anthro",
|
|
"Canon Characters": "Canon Characters",
|
|
"Comics": "Comics",
|
|
"Fetish Art": "Fetish Art",
|
|
"Human and EqG": "Human and EqG",
|
|
"NSFW": "NSFW",
|
|
"Original Characters": "Original Characters",
|
|
"Original Species": "Original Species",
|
|
"Pony": "Pony",
|
|
"Requests": "Requests",
|
|
"Safe": "Safe",
|
|
"Shipping": "Shipping",
|
|
"Violence and Gore": "Violence and Gore"
|
|
]
|
|
end
|
|
|
|
def types do
|
|
[
|
|
"Sketch",
|
|
"Colored Sketch",
|
|
"Inked",
|
|
"Flat Color",
|
|
"Vector",
|
|
"Cel Shaded",
|
|
"Fully Shaded",
|
|
"Traditional",
|
|
"Pixel Art",
|
|
"Animation",
|
|
"Crafted Item",
|
|
"Sculpture",
|
|
"Plushie",
|
|
"Other"
|
|
]
|
|
end
|
|
end
|