Removed the dependency in the accounting_sample command to the user data model in the accounting application.

This commit is contained in:
依瑪貓 2020-08-18 03:05:47 +08:00
parent d4961f9e25
commit 1f14e0deea

View File

@ -21,12 +21,14 @@
import random import random
import sys import sys
from django.contrib.auth import get_user_model
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 Populator from accounting.utils import Populator
from mia_womb.models import User from mia_core.utils import new_pk
class Command(BaseCommand): class Command(BaseCommand):
@ -48,17 +50,27 @@ class Command(BaseCommand):
*args (list[str]): The command line arguments. *args (list[str]): The command line arguments.
**options (dict[str,str]): The command line switches. **options (dict[str,str]): The command line switches.
""" """
if User.objects.first() is not None: user_model = get_user_model()
if user_model.objects.first() is not None:
print("Refused to fill in sample data with existing data.", print("Refused to fill in sample data with existing data.",
file=sys.stderr) file=sys.stderr)
return return
with transaction.atomic(): with transaction.atomic():
user = User(pk=923153018, login_id="imacat", user = user_model()
password="5486b64881adaf7bc1485cc26e57e51e", setattr(user, user_model.USERNAME_FIELD, "admin")
name="依瑪貓", is_disabled=False, is_deleted=False) for field in user_model.REQUIRED_FIELDS:
user.created_by = user setattr(user, field, "admin")
user.updated_by = user if getattr(user_model, "EMAIL_FIELD", None) is not None:
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() user.save()
p = Populator(user) p = Populator(user)