1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pyinventory_ynh.git synced 2024-09-03 20:16:09 +02:00
pyinventory_ynh/conf/create_superuser.py
JensDiemer 76785f5f78 Bugfix update superuser
The "update superuser" part in "scripts/upgrade" doesn't work. Because it tries to create a new
superuser and doesn't handle a existing one.

We have conf/create_superuser.py that handle this: Use it in install/upgrade
2020-12-09 20:25:01 +01:00

43 lines
1.1 KiB
Python

#!/usr/bin/env python3
import argparse
import os
import sys
def main():
os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_pyinventory_settings'
parser = argparse.ArgumentParser(
description='Create or update Django super user.'
)
parser.add_argument('--username')
parser.add_argument('--email')
parser.add_argument('--password')
args = parser.parse_args()
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()
super_user = User.objects.filter(username=username).first()
if super_user:
print('Update existing super user and set his password.', file=sys.stderr)
super_user.set_password(password)
super_user.email=email
super_user.save()
else:
print('Create new super user', file=sys.stderr)
User.objects.create_superuser(
username=username,
email=email,
password=password
)
if __name__ == '__main__':
main()