django-for-runners_ynh/conf/create_superuser.py

47 lines
1.1 KiB
Python
Raw Normal View History

2020-12-12 19:37:20 +01:00
#!/usr/bin/env python3
import argparse
import os
import sys
def main():
os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_for_runners_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()
2020-12-22 18:51:40 +01:00
user = User.objects.filter(username=username).first()
if user:
print('Update existing user and set his password.', file=sys.stderr)
user.is_active = True
user.is_staff = True
user.is_superuser = True
user.set_password(password)
user.email = email
user.save()
2020-12-12 19:37:20 +01:00
else:
print('Create new super user', file=sys.stderr)
User.objects.create_superuser(
username=username,
email=email,
password=password
)
if __name__ == '__main__':
main()