add standard seeds

This commit is contained in:
byte[] 2019-12-02 10:58:12 -05:00
parent 80c8b744a4
commit bf7dab33b7
8 changed files with 241 additions and 10 deletions

View file

@ -25,7 +25,8 @@ defmodule Philomena.Forums.Forum do
@doc false
def changeset(forum, attrs) do
forum
|> cast(attrs, [])
|> validate_required([])
|> cast(attrs, [:name, :short_name, :description, :access_level])
|> validate_required([:name, :short_name, :description, :access_level])
|> validate_inclusion(:access_level, ~W(normal assistant staff))
end
end

View file

@ -179,7 +179,9 @@ defmodule Philomena.Tags.Tag do
defp put_namespace_category(changeset) do
namespace = changeset |> get_field(:namespace)
changeset
|> change(category: @namespace_categories[namespace])
case @namespace_categories[namespace] do
nil -> changeset
category -> change(changeset, category: @namespace_categories[namespace])
end
end
end

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.CurrentFilterPlug do
import Plug.Conn
alias Philomena.{Filters, Filters.Filter}
alias Philomena.{Filters, Filters.Filter, Users.User}
alias Philomena.Repo
alias Pow.Plug
# No options
@ -14,8 +14,10 @@ defmodule PhilomenaWeb.CurrentFilterPlug do
filter =
if user do
user = user |> Repo.preload(:current_filter)
user.current_filter
user
|> Repo.preload(:current_filter)
|> maybe_set_default_filter()
|> Map.get(:current_filter)
else
filter_id = conn |> get_session(:filter_id)
@ -27,4 +29,16 @@ defmodule PhilomenaWeb.CurrentFilterPlug do
conn
|> assign(:current_filter, filter)
end
defp maybe_set_default_filter(%{current_filter: nil} = user) do
filter = Filters.default_filter()
{:ok, user} =
user
|> User.filter_changeset(filter)
|> Repo.update()
Map.put(user, :current_filter, filter)
end
defp maybe_set_default_filter(user), do: user
end

View file

@ -71,11 +71,11 @@ defmodule Philomena.MixProject do
# See the documentation for `Mix` for more info on aliases.
defp aliases do
[
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.setup": ["ecto.create", "ecto.load", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"ecto.migrate": ["ecto.migrate", "ecto.dump"],
"ecto.rollback": ["ecto.rollback", "ecto.dump"],
test: ["ecto.create --quiet", "ecto.migrate", "test"]
test: ["ecto.create --quiet", "ecto.load", "test"]
]
end
end

View file

@ -9,3 +9,61 @@
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias Philomena.{Repo, Comments.Comment, Filters.Filter, Forums.Forum, Galleries.Gallery, Posts.Post, Images.Image, Tags.Tag, Users.User}
alias Philomena.Tags
import Ecto.Query
IO.puts "---- Creating Elasticsearch indices"
Image.create_index!
Comment.create_index!
Gallery.create_index!
Tag.create_index!
Post.create_index!
# Report.create_index!
resources =
"priv/repo/seeds.json"
|> File.read!()
|> Jason.decode!()
IO.puts "---- Generating rating tags"
for tag_name <- resources["rating_tags"] do
%Tag{category: "rating"}
|> Tag.creation_changeset(%{name: tag_name})
|> Repo.insert(on_conflict: :nothing)
end
IO.puts "---- Generating system filters"
for filter_def <- resources["system_filters"] do
spoilered_tag_list = Enum.join(filter_def["spoilered"], ",")
hidden_tag_list = Enum.join(filter_def["hidden"], ",")
%Filter{system: true}
|> Filter.changeset(%{
name: filter_def["name"],
description: filter_def["description"],
spoilered_tag_list: spoilered_tag_list,
hidden_tag_list: hidden_tag_list
})
|> Repo.insert(on_conflict: :nothing)
end
IO.puts "---- Generating forums"
for forum_def <- resources["forums"] do
%Forum{}
|> Forum.changeset(forum_def)
|> Repo.insert(on_conflict: :nothing)
end
IO.puts "---- Generating users"
for user_def <- resources["users"] do
%User{}
|> User.creation_changeset(user_def)
|> Repo.insert(on_conflict: :nothing)
end
IO.puts "---- Indexing content"
Tag.reindex(Tag |> preload(^Tags.indexing_preloads()))
IO.puts "---- Done."

79
priv/repo/seeds.json Normal file
View file

@ -0,0 +1,79 @@
{
"system_filters": [{
"name": "Default",
"description": "The site's default filter.",
"hidden": [
"explicit",
"grotesque"
],
"spoilered": [
"questionable"
]
},
{
"name": "Everything",
"description": "This filter won't filter out anything at all.",
"hidden": [],
"spoilered": []
}
],
"forums": [{
"name": "General Discussion",
"short_name": "dis",
"description": "This is a discussion forum for everything unrelated to the show or other forums",
"access_level": "normal"
},
{
"name": "Episode Discussion",
"short_name": "pony",
"description": "Discuss the show, characters, and theories",
"access_level": "normal"
},
{
"name": "Site and Policy",
"short_name": "meta",
"description": "For site discussion and policy discussion",
"access_level": "normal"
},
{
"name": "Art Chat",
"short_name": "art",
"description": "Discuss art of any form, and share techniques and tips",
"access_level": "normal"
},
{
"name": "Roleplaying",
"short_name": "rp",
"description": "Roleplaying forum, in which people play roles",
"access_level": "normal"
},
{
"name": "Site Assistant Discussion",
"short_name": "helper",
"description": "Restricted - Assistants and Staff",
"access_level": "assistant"
},
{
"name": "Moderation Discussion",
"short_name": "mod",
"description": "Restricted - Staff only",
"access_level": "staff"
}
],
"users": [{
"name": "Administrator",
"email": "admin@example.com",
"password": "trixieisbestpony",
"confirm_password": "trixieisbestpony",
"role": "admin"
}],
"rating_tags": [
"safe",
"suggestive",
"questionable",
"explicit",
"semi-grimdark",
"grimdark",
"grotesque"
]
}

View file

@ -0,0 +1,63 @@
{
"users": [{
"name": "Hot Pocket Consumer",
"email": "moderator@example.com",
"password": "willdeleteglimmerposts4hotpockets",
"role": "moderator"
},
{
"name": "Hoping For a Promotion",
"email": "assistant@example.com",
"password": "hotpocketfetchingass",
"role": "assistant"
},
{
"name": "Pleb",
"email": "user@example.com",
"password": "glimmerpostingplebeian",
"role": "user"
}
],
"tags": [
"spacebar heating",
"fair dice roll",
"correcthorsebatterystaple",
"spaghetti",
"piñata",
"dot.dot"
],
"remote_images": [{
"url": "http://orig14.deviantart.net/c2d0/f/2015/269/9/b/tirek_battle_12exp_by_equumamici-d9ax5yd.gif",
"description": "Fairly large GIF (~23MB), use to test WebM stuff.",
"tags": "artist:equum_amici, safe, large gif"
}],
"comments": [
"bold is *bold*, italic is _italic_, spoiler is [spoiler]spoiler[/spoiler], code is @code@, underline is +underline+, strike is -strike-, sup is ^sup^, sub is ~sub~.",
"inline embedded thumbnails (tsp): >>1t >>1s >>1p",
"buggy embedded image inside a spoiler: [spoiler]who needs it anyway >>1s[/spoiler]"
],
"forum_posts": [{
"dis": [{
"Example topic": [
"example post",
"yet another example post"
]
},
{
"Second example topic": [
"post"
]
}
]
},
{
"art": [{
"Embedded images": [
">>1t >>1s >>1p",
">>1",
"non-existent: >>1000t >>1000s >>1000p >>1000"
]
}]
}
]
}

View file

@ -3047,7 +3047,7 @@ CREATE INDEX index_forums_on_last_topic_id ON public.forums USING btree (last_to
-- Name: index_forums_on_short_name; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_forums_on_short_name ON public.forums USING btree (short_name);
CREATE UNIQUE INDEX index_forums_on_short_name ON public.forums USING btree (short_name);
--
@ -3834,6 +3834,20 @@ CREATE INDEX index_vpns_on_ip ON public.vpns USING gist (ip inet_ops);
CREATE INDEX intensities_index ON public.images USING btree (se_intensity, sw_intensity, ne_intensity, nw_intensity, average_intensity);
--
-- Name: temp_unique_index_tags_on_name; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX temp_unique_index_tags_on_name ON public.tags USING btree (name);
--
-- Name: temp_unique_index_tags_on_slug; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX temp_unique_index_tags_on_slug ON public.tags USING btree (slug);
--
-- Name: channels fk_rails_021c624081; Type: FK CONSTRAINT; Schema: public; Owner: -
--