Applied the user manager to create the first user in the accounting sample command in the accounting application.

This commit is contained in:
依瑪貓 2020-08-20 21:00:14 +08:00
parent e692de556e
commit 50bc6fb0b1

View File

@ -24,13 +24,12 @@ import sys
from typing import Optional from typing import Optional
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.base_user import BaseUserManager
from django.core.management import BaseCommand, CommandParser from django.core.management import BaseCommand, CommandParser
from django.db import transaction from django.db import transaction
from django.db.models import PositiveIntegerField
from django.utils import timezone from django.utils import timezone
from accounting.utils import DataFiller from accounting.utils import DataFiller
from mia_core.utils import new_pk
class Command(BaseCommand): class Command(BaseCommand):
@ -63,21 +62,11 @@ class Command(BaseCommand):
return return
with transaction.atomic(): with transaction.atomic():
user = user_model() user_manager: BaseUserManager = user_model.objects
setattr(user, user_model.USERNAME_FIELD, "admin") user_manager.create_superuser("admin", password="12345")
for field in user_model.REQUIRED_FIELDS: user = user_model.objects.first()
setattr(user, field, "admin") print("Created the user \"admin\" with password \"12345\".",
if getattr(user_model, "EMAIL_FIELD", None) is not None: file=sys.stderr)
setattr(user, user_model.EMAIL_FIELD, "guest@example.com")
if getattr(user_model, "created_by", None) is not None:
user.created_by = user
if getattr(user_model, "updated_by", None) is not None:
user.updated_by = user
if isinstance(user_model._meta.pk, PositiveIntegerField):
user.pk = new_pk(user_model)
if getattr(user_model, "set_digest_password", None) is not None:
user.set_digest_password("admin", "12345")
user.save()
self._filler = DataFiller(user) self._filler = DataFiller(user)
self._filler.add_accounts([ self._filler.add_accounts([