Add a management command to create a new library for a user
This commit is contained in:
parent
f3c6bc03e4
commit
faf8bdcb81
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
Add a management command to create a new library for a user
|
|
@ -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
|
||||
```
|
|
@ -8,6 +8,7 @@ caption: Administration tasks
|
|||
maxdepth: 1
|
||||
---
|
||||
|
||||
create_library
|
||||
Manage users <users>
|
||||
Prune library <library>
|
||||
Clean database <database>
|
||||
|
|
Loading…
Reference in New Issue