def add_user(self, request, tenant_name): phone = request.data.get("phone", None) user_name = request.data.get("user_name", None) email = request.data.get("email", None) password = request.data.get("password", None) tenant = team_services.get_tenant(tenant_name) if Users.objects.filter(nick_name=user_name).exists(): raise UserExistError("用户名已存在") if Users.objects.filter(email=email).exists(): raise EmailExistError("邮箱已存在") if Users.objects.filter(phone=phone).exists(): raise PhoneExistError("手机号已存在") user = Users(email=email, nick_name=user_name, phone=phone, client_ip=self.get_client_ip(request), rf="backend") user.set_password(password) user.save() PermRelTenant.objects.create(user_id=user.pk, tenant_id=tenant.pk, identity='admin') codeRepositoriesService.createUser(user, email, password, user_name, user_name)
def create(self, data): # check nick name try: user_repo.get_by_username(data["nick_name"]) raise UserExistError("{} already exists.".format( data["nick_name"])) except Users.DoesNotExist: pass if data.get("email", ""): user = user_repo.get_user_by_email(data["email"]) if user is not None: raise EmailExistError("{} already exists.".format( data["email"])) if data.get("phone", ""): user = user_repo.get_user_by_phone(data["phone"]) if user is not None: raise PhoneExistError("{} already exists.".format( data["phone"])) user = { "nick_name": data["nick_name"], "password": encrypt_passwd(data["password"]), "email": data.get("email", ""), "phone": data.get("phone", ""), "enterprise_id": data["eid"], "is_active": data.get("is_active", True), "create_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), } return Users.objects.create(**user)