diff --git a/api/funkwhale_api/common/management/commands/gitpod.py b/api/funkwhale_api/common/management/commands/gitpod.py index 46561220c..b1014c7bb 100644 --- a/api/funkwhale_api/common/management/commands/gitpod.py +++ b/api/funkwhale_api/common/management/commands/gitpod.py @@ -50,8 +50,13 @@ class Command(BaseCommand): os.system("mv -f /tmp/catalog/music /workspace/funkwhale/data") os.system("rm -rf /tmp/catalog/music") - # # Import music catalog into library - call_command("script", "migrate_to_user_libraries", no_input=False) + # Import music catalog into library + call_command( + "create_library", + "gitpod", + name="funkwhale/catalog", + privacy_level="everyone" + ) call_command( "import_files", Library.objects.get(actor=user.actor).uuid, diff --git a/api/funkwhale_api/music/management/commands/create_library.py b/api/funkwhale_api/music/management/commands/create_library.py new file mode 100644 index 000000000..861100cd5 --- /dev/null +++ b/api/funkwhale_api/music/management/commands/create_library.py @@ -0,0 +1,51 @@ +from funkwhale_api.federation.models import Actor +from funkwhale_api.music.models import Library +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = """ + Create a new library for a given user. + """ + + def add_arguments(self, parser): + parser.add_argument( + "username", + type=str, + help=("Specify the owner of the library to be created."), + ) + parser.add_argument( + "--name", + type=str, + help=("Specify a name for the library."), + default="default", + ) + parser.add_argument( + "--privacy-level", + type=str.lower, + choices=["me", "instance", "everyone"], + help=("Specify the privacy level for the library."), + default="me", + ) + + def handle(self, *args, **kwargs): + actor, actor_created = Actor.objects.get_or_create(name=kwargs["username"]) + + if actor_created: + self.stdout.write("No existing actor found. New actor created.") + + library, created = Library.objects.get_or_create( + name=kwargs["name"], actor=actor, privacy_level=kwargs["privacy_level"] + ) + if created: + self.stdout.write( + "Created library {} for user {} with UUID {}".format( + library.pk, actor.user.pk, library.uuid + ) + ) + else: + self.stdout.write( + "Found existing library {} for user {} with UUID {}".format( + library.pk, actor.user.pk, library.uuid + ) + ) diff --git a/changes/changelog.d/create_lib_cmd.feature b/changes/changelog.d/create_lib_cmd.feature new file mode 100644 index 000000000..da1144529 --- /dev/null +++ b/changes/changelog.d/create_lib_cmd.feature @@ -0,0 +1 @@ +Add a management command to create a new library for a user diff --git a/docs/administrator_documentation/manage_script/create_library.md b/docs/administrator_documentation/manage_script/create_library.md new file mode 100644 index 000000000..85c167a6c --- /dev/null +++ b/docs/administrator_documentation/manage_script/create_library.md @@ -0,0 +1,125 @@ +# Create a library + +Use the `create_library` command to create new libraries for a given user. + +```{list-table} +:header-rows: 1 + +* - Parameter + - Data type + - Description + +* - `username`* + - String + - The user you want to create the library for. + +* - `--name` + - String + - The name of the library. Defaults to "default". + +* - `--privacy-level` + - Enum (String) + - The [privacy level](../../user_documentation/libraries/create_library.md) of the library + - `"me"` (default) + - `"instance"` + - `"everyone"` + +``` + +## Examples + +### Create a new library + +Use the following command to create a new library with a custom name and privacy level. + +::::{tab-set} + +:::{tab-item} Debian +:sync: debian + +```{code-block} bash +poetry run python manage.py create_library username1 --name="Library 1" --privacy-level="everyone" +``` + +::: + +:::{tab-item} Docker +:sync: docker + +```{code-block} bash +docker-compose run --rm api python manage.py create_library username1 --name="Library 1" --privacy-level="everyone" +``` + +::: + +:::: + +### Returns + +```{code-block} text-output +Created library Library 1 for user username1 with UUID 436da05b-8cb1-4a4d-b870-4a3b235d8517 +``` + +### Create a new library wth no name or privacy level + +You can create a library using only a username. The script substitutes default values for the library name and privacy level. + +::::{tab-set} + +:::{tab-item} Debian +:sync: debian + +```{code-block} bash +poetry run python manage.py create_library username1 +``` + +::: + +:::{tab-item} Docker +:sync: docker + +```{code-block} bash +docker-compose run --rm api python manage.py create_library username1 +``` + +::: + +:::: + +### Returns + +```{code-block} text-output +Created library default for user username1 with UUID 436da05b-8cb1-4a4d-b870-4a3b235d8517 +``` + +### Library with the same name already exists + +If a library with the same name already exists for the given user, the script will __not__ create a new library. + +::::{tab-set} + +:::{tab-item} Debian +:sync: debian + +```{code-block} bash +poetry run python manage.py create_library username1 --name="Library 1" --privacy-level="everyone" +``` + +::: + +:::{tab-item} Docker +:sync: docker + +```{code-block} bash +docker-compose run --rm api python manage.py create_library username1 --name="Library 1" --privacy-level="everyone" +``` + +::: + +:::: + +### Returns + +```{code-block} text-output +Found existing library Library 1 for user username1 with UUID 436da05b-8cb1-4a4d-b870-4a3b235d8517 +``` diff --git a/docs/administrator_documentation/manage_script/index.md b/docs/administrator_documentation/manage_script/index.md index 00ae0b5a8..5cbeeb245 100644 --- a/docs/administrator_documentation/manage_script/index.md +++ b/docs/administrator_documentation/manage_script/index.md @@ -8,6 +8,7 @@ caption: Administration tasks maxdepth: 1 --- +create_library Manage users Prune library Clean database