import random import string from django.core.management.base import BaseCommand from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth import get_user_model from django.conf import settings from uncloud_pay.models import BillingAddress, RecurringPeriod, Product from uncloud.models import UncloudProvider, UncloudNetwork class Command(BaseCommand): help = 'Add standard uncloud values' def add_arguments(self, parser): pass def handle(self, *args, **options): # Order matters, objects can be dependent on each other admin_username="uncloud-admin" pw_length = 32 # Only set password if the user did not exist before try: admin_user = get_user_model().objects.get(username=settings.UNCLOUD_ADMIN_NAME) except ObjectDoesNotExist: random_password = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(pw_length)) admin_user = get_user_model().objects.create_user(username=settings.UNCLOUD_ADMIN_NAME, password=random_password) admin_user.is_superuser=True admin_user.is_staff=True admin_user.save() print(f"Created admin user '{admin_username}' with password '{random_password}'") BillingAddress.populate_db_defaults() RecurringPeriod.populate_db_defaults() Product.populate_db_defaults() UncloudNetwork.populate_db_defaults() UncloudProvider.populate_db_defaults()