from django.contrib.auth.models import UserManager from django.utils.translation import gettext_lazy as _ from log_manager.trace_log import trace_log class CustomUserManager(UserManager): """カスタムユーザー管理""" @trace_log def create_user(self, login_id, _email, password=None, **extra_fields): """ユーザーを作成します。 Arguments: login_ir: ログインID _email: メールアドレス password: パスワード extra_field: その他フィールド Return: 作成したユーザー """ if not login_id: raise ValueError(_("Login ID is required")) if not _email: raise ValueError(_("email is required")) # ユーザーを作成 email = self.normalize_email(_email) user = self.model(login_id=login_id, **extra_fields) user.set_password(password) user.save(using=self._db) # メインのメールアドレスを登録する from .email_address import EmailAddress EmailAddress.objects.create(user=user, email=email, is_primary=True) return user @trace_log def create_superuser(self, login_id, _email, password=None, **extra_fields): """スーパーユーザーを作成します。 Arguments: login_ir: ログインID _email: メールアドレス password: パスワード extra_field: その他フィールド Return: 作成したスーパーユーザー """ extra_fields.setdefault("is_staff", True) extra_fields.setdefault("is_superuser", True) if extra_fields.get("is_staff") is not True: raise ValueError(_("Superuser must have is_staff=True.")) if extra_fields.get("is_superuser") is not True: raise ValueError(_("Superuser must have is_superuser=True.")) return self.create_user(login_id, _email, password, **extra_fields)