2018-07-04 14:29:17 +02:00
|
|
|
import EctoEnum
|
2018-07-27 10:45:35 +02:00
|
|
|
defenum(AddressTypeEnum, :address_type, [:physical, :url, :phone, :other])
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Event do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents an event
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.{Event, Participant, Tag, Category, Session, Track}
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Addresses.Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
schema "events" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:url, :string)
|
|
|
|
field(:local, :boolean, default: true)
|
|
|
|
field(:begins_on, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:description, :string)
|
|
|
|
field(:ends_on, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:title, :string)
|
2018-08-24 11:34:00 +02:00
|
|
|
# ???
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:state, :integer, default: 0)
|
2018-08-24 11:34:00 +02:00
|
|
|
# Event status: TENTATIVE 1, CONFIRMED 2, CANCELLED 3
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:status, :integer, default: 0)
|
2018-08-24 11:34:00 +02:00
|
|
|
# If the event is public or private
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:public, :boolean, default: true)
|
|
|
|
field(:thumbnail, :string)
|
|
|
|
field(:large_image, :string)
|
|
|
|
field(:publish_at, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:uuid, Ecto.UUID, default: Ecto.UUID.generate())
|
|
|
|
field(:address_type, AddressTypeEnum, default: :physical)
|
|
|
|
field(:online_address, :string)
|
|
|
|
field(:phone, :string)
|
|
|
|
belongs_to(:organizer_actor, Actor, foreign_key: :organizer_actor_id)
|
|
|
|
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
|
|
|
|
many_to_many(:tags, Tag, join_through: "events_tags")
|
|
|
|
belongs_to(:category, Category)
|
|
|
|
many_to_many(:participants, Actor, join_through: Participant)
|
|
|
|
has_many(:tracks, Track)
|
|
|
|
has_many(:sessions, Session)
|
|
|
|
belongs_to(:physical_address, Address)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
timestamps(type: :utc_datetime)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Event{} = event, attrs) do
|
2018-08-24 11:34:00 +02:00
|
|
|
# TODO : Change all of this
|
2018-07-27 10:45:35 +02:00
|
|
|
actor_url =
|
|
|
|
if Map.has_key?(attrs, :organizer_actor) do
|
|
|
|
attrs.organizer_actor.preferred_username
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
url =
|
|
|
|
if Map.has_key?(attrs, "url"),
|
|
|
|
do: attrs["url"],
|
2018-10-11 17:37:39 +02:00
|
|
|
else: "#{MobilizonWeb.Endpoint.url()}/@#{actor_url}/#{uuid}"
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
event
|
2018-07-04 14:29:17 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:url,
|
|
|
|
:begins_on,
|
|
|
|
:ends_on,
|
|
|
|
:organizer_actor_id,
|
|
|
|
:category_id,
|
|
|
|
:state,
|
|
|
|
:status,
|
|
|
|
:public,
|
|
|
|
:thumbnail,
|
|
|
|
:large_image,
|
|
|
|
:publish_at,
|
|
|
|
:address_type,
|
|
|
|
:online_address,
|
2018-07-27 10:45:35 +02:00
|
|
|
:phone
|
|
|
|
])
|
2018-01-15 12:17:34 +01:00
|
|
|
|> cast_assoc(:tags)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> cast_assoc(:physical_address)
|
2018-06-14 17:25:55 +02:00
|
|
|
|> put_change(:uuid, uuid)
|
2018-08-24 11:34:00 +02:00
|
|
|
|> put_change(:url, url)
|
2018-07-27 10:45:35 +02:00
|
|
|
|> validate_required([
|
|
|
|
:title,
|
|
|
|
:begins_on,
|
|
|
|
:organizer_actor_id,
|
|
|
|
:category_id,
|
|
|
|
:url,
|
|
|
|
:uuid,
|
|
|
|
:address_type
|
|
|
|
])
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|