mirror of
https://github.com/YunoHost-Apps/django_example_ynh.git
synced 2024-09-03 18:26:21 +02:00
Add "django_ynh" to INSTALLED_APPS and migrate "create_superuser" to a manage command
This commit is contained in:
parent
57bbf9880f
commit
867f3aac2b
8 changed files with 56 additions and 72 deletions
|
@ -20,6 +20,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'axes', # https://github.com/jazzband/django-axes
|
||||
'django_ynh',
|
||||
]
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Can be called e.g.:
|
||||
|
||||
poetry run create_superuser --ds="foo.settings" --username="bar" \
|
||||
--email="foo@bar.tld" --password="no-password"
|
||||
|
||||
or, e.g.:
|
||||
|
||||
python3 -m django_ynh.create_superuser --ds="foo.settings" --username="bar" \
|
||||
--email="foo@bar.tld" \
|
||||
--password="no-password"
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Create or update Django super user.')
|
||||
parser.add_argument('--ds', help='The "DJANGO_SETTINGS_MODULE" string')
|
||||
parser.add_argument('--username')
|
||||
parser.add_argument('--email')
|
||||
parser.add_argument('--password')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = args.ds
|
||||
|
||||
username = args.username
|
||||
email = args.email or ''
|
||||
password = args.password
|
||||
|
||||
import django
|
||||
|
||||
django.setup()
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
User = get_user_model()
|
||||
user = User.objects.filter(username=username).first()
|
||||
if user:
|
||||
print(f'Update existing user "{user}" and set his password.', file=sys.stderr)
|
||||
print(repr(password))
|
||||
user.is_active = True
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.set_password(password)
|
||||
user.email = email
|
||||
user.save()
|
||||
else:
|
||||
print(f'Create new super user "{username}"', file=sys.stderr)
|
||||
User.objects.create_superuser(username=username, email=email, password=password)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
0
django_ynh/management/__init__.py
Normal file
0
django_ynh/management/__init__.py
Normal file
0
django_ynh/management/commands/__init__.py
Normal file
0
django_ynh/management/commands/__init__.py
Normal file
49
django_ynh/management/commands/create_superuser.py
Normal file
49
django_ynh/management/commands/create_superuser.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
"""
|
||||
Create or update Django super user with a unusable password
|
||||
|
||||
A "unusable password" because it's not needed while auth via SSOwat ;)
|
||||
|
||||
Can be called e.g.:
|
||||
./manage.py create_superuser --username="bar" --email="foo@bar.tld"
|
||||
"""
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Create or update Django super user with a unusable password (auth via SSOwat)'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--username",
|
||||
action="store",
|
||||
required=True,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--email",
|
||||
action="store",
|
||||
required=True,
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
username = options['username']
|
||||
email = options['email']
|
||||
|
||||
User = get_user_model()
|
||||
user = User.objects.filter(username=username).first()
|
||||
if user:
|
||||
self.stderr.write(f'Update existing user "{user}" and set his password.')
|
||||
user.is_active = True
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.email = email
|
||||
else:
|
||||
print(f'Create new super user "{username}"', file=sys.stderr)
|
||||
user = User.objects.create_superuser(username=username, email=email, password=None)
|
||||
|
||||
user.set_unusable_password()
|
||||
user.save()
|
|
@ -37,9 +37,6 @@ pyupgrade = "*"
|
|||
requires = ["poetry-core>=1.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
create_superuser = "django_ynh.create_superuser:main"
|
||||
|
||||
[tool.isort]
|
||||
# https://pycqa.github.io/isort/docs/configuration/config_files/#pyprojecttoml-preferred-format
|
||||
atomic=true
|
||||
|
|
|
@ -122,9 +122,6 @@ chown -R "$app" "$final_path"
|
|||
# ================================================
|
||||
ynh_script_progression --message="Create django_ynh configuration file..."
|
||||
|
||||
cp ../conf/create_superuser.py "$final_path/create_superuser.py"
|
||||
chmod +x "$final_path/create_superuser.py"
|
||||
|
||||
gunicorn_conf="$final_path/gunicorn.conf.py"
|
||||
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
|
||||
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
|
||||
|
@ -178,7 +175,8 @@ ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight
|
|||
|
||||
./manage.py migrate --no-input
|
||||
./manage.py collectstatic --no-input
|
||||
./create_superuser.py --username="$admin" --email="$admin_mail" --password="django_ynh"
|
||||
|
||||
python -m django_ynh.create_superuser --ds="django_ynh_demo_settings" --username="$admin" --email="$admin_mail" --password="$app"
|
||||
|
||||
# Check the configuration
|
||||
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
|
||||
|
|
|
@ -105,11 +105,7 @@ chown -R "$app" "$final_path"
|
|||
#=================================================
|
||||
# copy config files
|
||||
# ================================================
|
||||
ynh_script_progression --message="Create django_ynh configuration file..."
|
||||
|
||||
ynh_backup_if_checksum_is_different --file="$final_path/create_superuser.py"
|
||||
cp ../conf/create_superuser.py "$final_path/create_superuser.py"
|
||||
chmod +x "$final_path/create_superuser.py"
|
||||
ynh_script_progression --message="Create project configuration files..."
|
||||
|
||||
gunicorn_conf="$final_path/gunicorn.conf.py"
|
||||
ynh_backup_if_checksum_is_different --file="$gunicorn_conf"
|
||||
|
@ -168,7 +164,9 @@ ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight
|
|||
|
||||
./manage.py migrate --no-input
|
||||
./manage.py collectstatic --no-input
|
||||
./create_superuser.py --username="$admin" --email="$admin_mail" --password="django_ynh"
|
||||
|
||||
# Create/update Django superuser (set unusable password, because auth done via SSOwat):
|
||||
./manage.py create_superuser --username="$admin" --email="$admin_mail"
|
||||
|
||||
# Check the configuration
|
||||
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
|
||||
|
|
Loading…
Add table
Reference in a new issue