from django.contrib.auth import get_user_model


def create_user_if_not_exist(username, passwd, superuser=False, extra_fields=None):
    """
    创建一个用户
    """
    User = get_user_model()
    user = User.objects.filter(username=username)
    if not user.exists():
        print(f'--- 初始创建用户: {username}')

        fields_dc = {}
        fields_dc.update({'username': username, "password": passwd})

        if isinstance(extra_fields, dict):
            fields_dc.update(extra_fields)

        if superuser:
            User.objects.create_superuser(**fields_dc)
        else:
            User.objects.create_user(**fields_dc)
        return True
    else:
        return False
