moviewyrm/bookwyrm/management/commands/initdb.py

185 lines
5.6 KiB
Python
Raw Normal View History

2021-04-30 17:42:27 +00:00
""" What you need in the database to make it work """
from django.core.management.base import BaseCommand
2020-11-08 18:29:33 +00:00
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
2022-01-10 19:52:00 +00:00
from bookwyrm import models
2020-11-08 18:29:33 +00:00
2021-03-08 16:49:10 +00:00
2020-11-08 18:29:33 +00:00
def init_groups():
2021-04-30 17:42:27 +00:00
"""permission levels"""
2021-03-08 16:49:10 +00:00
groups = ["admin", "moderator", "editor"]
2020-11-08 18:29:33 +00:00
for group in groups:
Group.objects.create(name=group)
2021-03-08 16:49:10 +00:00
2020-11-08 18:29:33 +00:00
def init_permissions():
"""permission types"""
2021-03-08 16:49:10 +00:00
permissions = [
{
"codename": "edit_instance_settings",
"name": "change the instance info",
2022-01-27 21:23:29 +00:00
"groups": ["admin"],
2021-03-08 16:49:10 +00:00
},
{
"codename": "set_user_group",
"name": "change what group a user is in",
"groups": ["admin", "moderator"],
},
{
"codename": "control_federation",
"name": "control who to federate with",
"groups": ["admin", "moderator"],
},
{
"codename": "create_invites",
"name": "issue invitations to join",
"groups": ["admin", "moderator"],
},
{
"codename": "moderate_user",
"name": "deactivate or silence a user",
"groups": ["admin", "moderator"],
},
{
"codename": "moderate_post",
"name": "delete other users' posts",
"groups": ["admin", "moderator"],
},
{
"codename": "edit_book",
"name": "edit book info",
"groups": ["admin", "moderator", "editor"],
},
]
2020-11-08 18:29:33 +00:00
content_type = ContentType.objects.get_for_model(models.User)
2020-11-08 18:29:33 +00:00
for permission in permissions:
permission_obj = Permission.objects.create(
2021-03-08 16:49:10 +00:00
codename=permission["codename"],
name=permission["name"],
2020-11-08 18:29:33 +00:00
content_type=content_type,
)
# add the permission to the appropriate groups
2021-03-08 16:49:10 +00:00
for group_name in permission["groups"]:
2020-11-08 18:29:33 +00:00
Group.objects.get(name=group_name).permissions.add(permission_obj)
def init_connectors():
2021-04-30 17:42:27 +00:00
"""access book data sources"""
2022-01-10 19:52:00 +00:00
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="bookwyrm.social",
2022-01-27 21:23:29 +00:00
name="Bookwyrm.social",
2021-03-08 16:49:10 +00:00
connector_file="bookwyrm_connector",
base_url="https://bookwyrm.social",
books_url="https://bookwyrm.social/book",
2021-03-13 21:55:20 +00:00
covers_url="https://bookwyrm.social/images/",
2021-03-08 16:49:10 +00:00
search_url="https://bookwyrm.social/search?q=",
isbn_search_url="https://bookwyrm.social/isbn/",
2020-11-08 18:29:33 +00:00
priority=2,
)
2022-01-27 21:23:29 +00:00
# pylint: disable=line-too-long
2022-01-10 19:52:00 +00:00
models.Connector.objects.create(
identifier="inventaire.io",
name="Inventaire",
connector_file="inventaire",
2021-04-29 17:30:38 +00:00
base_url="https://inventaire.io",
books_url="https://inventaire.io/api/entities",
covers_url="https://inventaire.io",
search_url="https://inventaire.io/api/search?types=works&types=works&search=",
isbn_search_url="https://inventaire.io/api/entities?action=by-uris&uris=isbn%3A",
priority=3,
)
2022-01-10 19:52:00 +00:00
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="openlibrary.org",
name="OpenLibrary",
connector_file="openlibrary",
base_url="https://openlibrary.org",
books_url="https://openlibrary.org",
covers_url="https://covers.openlibrary.org",
search_url="https://openlibrary.org/search?q=",
isbn_search_url="https://openlibrary.org/api/books?jscmd=data&format=json&bibkeys=ISBN:",
2020-11-08 18:29:33 +00:00
priority=3,
)
2021-03-08 16:49:10 +00:00
2021-04-05 22:26:53 +00:00
def init_federated_servers():
2021-04-26 16:15:42 +00:00
"""big no to nazis"""
2021-04-05 22:26:53 +00:00
built_in_blocks = ["gab.ai", "gab.com"]
for server in built_in_blocks:
2022-01-10 19:52:00 +00:00
models.FederatedServer.objects.create(
2021-04-05 22:26:53 +00:00
server_name=server,
status="blocked",
)
2020-12-13 00:01:43 +00:00
def init_settings():
2021-04-30 18:09:06 +00:00
"""info about the instance"""
2022-01-10 19:52:00 +00:00
models.SiteSettings.objects.create(
2021-04-30 17:42:27 +00:00
support_link="https://www.patreon.com/bookwyrm",
support_title="Patreon",
install_mode=True,
2021-04-30 17:42:27 +00:00
)
2020-12-13 00:01:43 +00:00
2021-03-08 16:49:10 +00:00
2022-01-27 21:23:29 +00:00
def init_link_domains():
2022-01-10 19:52:00 +00:00
"""safe book links"""
domains = [
("standardebooks.org", "Standard EBooks"),
("www.gutenberg.org", "Project Gutenberg"),
("archive.org", "Internet Archive"),
("openlibrary.org", "Open Library"),
("theanarchistlibrary.org", "The Anarchist Library"),
]
for domain, name in domains:
models.LinkDomain.objects.create(
domain=domain,
name=name,
status="approved",
)
2022-01-10 19:52:00 +00:00
2022-01-27 21:23:29 +00:00
# pylint: disable=no-self-use
# pylint: disable=unused-argument
2020-11-08 18:29:33 +00:00
class Command(BaseCommand):
2022-01-27 21:23:29 +00:00
"""command-line options"""
2021-03-08 16:49:10 +00:00
help = "Initializes the database with starter data"
2020-11-08 18:29:33 +00:00
def add_arguments(self, parser):
2022-01-27 21:23:29 +00:00
"""specify which function to run"""
parser.add_argument(
"--limit",
default=None,
help="Limit init to specific table",
)
2020-11-08 18:29:33 +00:00
def handle(self, *args, **options):
2022-01-27 21:23:29 +00:00
"""execute init"""
limit = options.get("limit")
tables = [
"group",
"permission",
"connector",
"federatedserver",
"settings",
"linkdomain",
]
if limit and limit not in tables:
raise Exception("Invalid table limit:", limit)
if not limit or limit == "group":
init_groups()
if not limit or limit == "permission":
init_permissions()
if not limit or limit == "connector":
init_connectors()
if not limit or limit == "federatedserver":
init_federated_servers()
if not limit or limit == "settings":
init_settings()
if not limit or limit == "linkdomain":
init_link_domains()